2

I wanted to delete an element from the Array, as shown below I inserted a testindiex with type test and document with an Array inside, in elasticsearch using elastic4s.

I wanted to delete a value ex: tony from members - how can I do this

   val insert = client.execute {
  indexInto("testindex"/"test") id 1 fields(
      "members" -> Array(
      "tony",
      "salvidor",
      "bobby"
    ),
    "crews" -> Seq(
      "gualtieri",
      "baccalieri",
      "barese",
      "moltisanti"
    )
  )
}.await

Here what i did:

 val deleted=client.execute {
    deleteIn("testindex"/"test").by(termQuery("crews","gualtieri"))
  }.await

but this deletes entire document instead of single record, help me on this.

1 Answer 1

1

Element delete/update from a nested/Array JSON from Elasticsearch

Input: JSON Document

{
  "members" -> Array(
    "tony",
    "salvidor",
    "bobby"
  ),
  "crews" ->
    "gualtieri"

)

Below commands for adding and removing the elements from an Array

add Command: Adding elements to an Array in a JSON document

 val resp = client.execute { updateIn(indexname,doctype).query("crews", "gualtieri")  script {
  script("ctx._source.members.add(params.key)").params(Map("key"->"space"))
} }.await

remove Command: Removing elements from an Array in a JSON document

val resp1 = client.execute { updateIn(indexname,doctype).query("crews", "gualtieri")  script {
  script("ctx._source.members.remove(ctx._source.members.indexOf('salvidor'))")
    //.params(Map("key"->"abc"))
} }.await

removeAll Command: Removing the all same elements from an Array.

val resp2 = client.execute { updateIn("testindex","test").query("crews", "gualtieri")  script {
  script("ctx._source.members.removeAll(Collections.singleton('space'))")
} }.await
Sign up to request clarification or add additional context in comments.

1 Comment

How to create template using elastic4s from json haveing setting and mapping details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.