Append to existing field

In looking at the Elasticsearch documentation, specifically the Update API,
it doesn't appear that Elasticsearch has the functionality to append to an
array, it can "update" it with new values, but not preserve the existing
values. Am I correct in saying this?

For an example of what I'm trying to figure out, I borrowed an example from
Exploring Elasticsearch. If I have data that looks like the following:

"_id": 1,
"handle": "ron",
"age": 28,
"hobbies": ["hacking", "the great outdoors"],
"computer": {"cpu": "pentium pro", "mhz": 200}

Is there any way to add "cooking" to the list of hobbies without deleting "hacking" and "the great outdoors" within Elasticsearch itself?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/cb539256-a22f-446b-8f4a-47954fa3bf3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

2 Likes

You can use the Update API with a script to append to the array:

curl -XPOST "http://localhost:9200/t/t/1" -d'
{
"hobbies" : ["a", "b"]
}'

curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : "ctx._source.hobbies += hobby",
"params" : {
"hobby" : "c"
}
}'

curl -XGET "http://localhost:9200/t/t/1"

You can even append an array to the array:

curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : "ctx._source.hobbies += hobby",
"params" : {
"hobby" : ["d", "e"]
}
}'

-Zach

On Tuesday, March 18, 2014 8:46:27 AM UTC-4, James Massey wrote:

In looking at the Elasticsearch documentation, specifically the Update
API, it doesn't appear that Elasticsearch has the functionality to append
to an array, it can "update" it with new values, but not preserve the
existing values. Am I correct in saying this?

For an example of what I'm trying to figure out, I borrowed an example
from Exploring Elasticsearch. If I have data that looks like the following:

"_id": 1,
"handle": "ron",
"age": 28,
"hobbies": ["hacking", "the great outdoors"],
"computer": {"cpu": "pentium pro", "mhz": 200}

Is there any way to add "cooking" to the list of hobbies without deleting "hacking" and "the great outdoors" within Elasticsearch itself?

On Tuesday, March 18, 2014 8:46:27 AM UTC-4, James Massey wrote:

In looking at the Elasticsearch documentation, specifically the Update
API, it doesn't appear that Elasticsearch has the functionality to append
to an array, it can "update" it with new values, but not preserve the
existing values. Am I correct in saying this?

For an example of what I'm trying to figure out, I borrowed an example
from Exploring Elasticsearch. If I have data that looks like the following:

"_id": 1,
"handle": "ron",
"age": 28,
"hobbies": ["hacking", "the great outdoors"],
"computer": {"cpu": "pentium pro", "mhz": 200}

Is there any way to add "cooking" to the list of hobbies without deleting "hacking" and "the great outdoors" within Elasticsearch itself?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/3a2f8db5-1b45-48be-b641-b41a1728d55b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

1 Like

Thanks for the reply, this works fine for me, I think I had the script set
up improperly so it wouldn't point to an existing field and would make a
new one instead.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f0a15ca6-9a1d-472d-929f-febe5761c5d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

But how can I do the opposite action like "script" : "ctx._source.hobbies -= hobby" ?

2 Likes

What should I do if I don't know the id of the doc?

Here is my case:
I have a nested doc like:
{
"Name" : "ABC",
"Tag" : {
"Tag1" : "DEF",
"Tag2" : "GHI"
}
}

and another doc like:
{
"Name" : "XYZ",
"Tag" : {
"Tag1" : "DEF",
"Tag2" : "GHI"
}
}

I want to append them since they have same Tag1 and Tag2 values.
My final doc should be:
{
"Name" : "ABC", "XYZ"
"Tag" : {
"Tag1" : "DEF",
"Tag2" : "GHI"
}
}

I do not have the ID of the docs.
How to I append?

Open your own thread please. This one is too old. And please follow the guidelines and format the code. Thanks!

Opened a new thread. I thought, having all the connected discussion under 1 thread would help future visitors!
Yes. I did format the code. Sorry.

2 Likes

Hi team,

How to do with NEST same operation

am using BulkAsync()

What if I want to append multiple values in an array in one API call?

This is a simple way you can append multiple objects to Array

Note: it is arrayList so you can use add,addAll etc methods of ArrayList

Cheers guys :elasticheart:

PUT abc/doc/1
{
  "entities1": [
    {
      "name": "ashu",
      "status": "N",
      "id": "entity-59"
    },
    {
      "name": "abhi",
      "status": "Y",
      "id": "entity-60"
    }
  ]
}
GET abc/_search
POST abc/doc/1/_update
{
  "script": {
    "inline": "ctx._source.entities1.addAll(params.val)",
    "lang": "painless",
    "params": {
      "val": [{
        "name": "sabhi",
        "status": "sY",
        "id": "entity-s60"
      },{
        "name": "1sabhi",
        "status": "1Y",
        "id": "1entity-s60"
      },{
        "name": "2sabhi",
        "status": "2Y",
        "id": "2entity-s60"
      }]
    }
  }
}
GET abc/_search
1 Like