Append to existing field


(bruskie24) #1

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.


(Zachary Tong) #2

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.


(bruskie24) #3

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.


(Alex G) #4

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


#5

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?


(David Pilato) #6

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


#7

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.


(Supreeth J) #8

Hi team,

How to do with NEST same operation

am using BulkAsync()


(Madhur Gor) #9

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