# Append to existing field

**URL:** <https://discuss.elastic.co/t/append-to-existing-field/16423>\
**Category:** Elasticsearch\
**Created:** [March 18, 2014, 12:46pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423 "2014-03-18T12:46:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruskie24](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bruskie24](https://discuss.elastic.co/u/bruskie24)\
**Post date:** [March 18, 2014, 12:46pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/1 "2014-03-18T12:46:27Z")

</div>

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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/cb539256-a22f-446b-8f4a-47954fa3bf3f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [March 18, 2014, 1:03pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/2 "2014-03-18T13:03:50Z")

</div>

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

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

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

curl -XGET "[http://localhost:9200/t/t/1](http://localhost:9200/t/t/1)"

You can even append an array to the array:

curl -XPOST "[http://localhost:9200/t/t/1/\_update](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/3a2f8db5-1b45-48be-b641-b41a1728d55b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bruskie24](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bruskie24](https://discuss.elastic.co/u/bruskie24)\
**Post date:** [March 18, 2014, 1:50pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/3 "2014-03-18T13:50:29Z")

</div>

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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/f0a15ca6-9a1d-472d-929f-febe5761c5d2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![moren](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@moren](https://discuss.elastic.co/u/moren)\
**Post date:** [July 12, 2016, 4:13pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/4 "2016-07-12T16:13:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kaushah](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kaushah](https://discuss.elastic.co/u/kaushah)\
**Post date:** [June 15, 2017, 1:21am UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/5 "2017-06-15T01:21:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 15, 2017, 7:44am UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/6 "2017-06-15T07:44:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kaushah](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@kaushah](https://discuss.elastic.co/u/kaushah)\
**Post date:** [June 15, 2017, 4:42pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/7 "2017-06-15T16:42:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SupreethJ](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@SupreethJ](https://discuss.elastic.co/u/SupreethJ)\
**Post date:** [July 30, 2018, 6:33am UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/8 "2018-07-30T06:33:32Z")

</div>

Hi team,

How to do with NEST same operation

am using BulkAsync()

---

<div class="post-metadata">

**Author:** ![madhurgor](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@madhurgor](https://discuss.elastic.co/u/madhurgor)\
**Post date:** [November 7, 2018, 11:17pm UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/9 "2018-11-07T23:17:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ashutoshningot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashutoshningot/32/26031_2.png) [@ashutoshningot](https://discuss.elastic.co/u/ashutoshningot)\
**Post date:** [April 10, 2020, 6:54am UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/10 "2020-04-10T06:54:27Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 4:01am UTC](https://discuss.elastic.co/t/append-to-existing-field/16423/11 "2022-11-04T04:01:25Z")

</div>


