Removing elements from an array in a document


(Jayant Kerai) #1

Hi, I'm very new to ES and am evaluating it for my search needs

http://www.elasticsearch.org/guide/reference/api/update.html

shows how to add elements to an array in an existing indexed document.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags += tag",
"params" : {
    "tag" : "blue"
}

}'
The above should add a new element to the list.

Is it possible to remove elements from an array in a similar fashion?
e.g. the equivalent of

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags -= tag",
"params" : {
    "tag" : "blue"
}

}'

Aside from reading all the element entries and removing the one 1 want to
remove and updating the entire element using the _update endpoint.

--


Working with fields containing underline in painless scripts
(Igor Motov) #2

Tags in your example is an ArrayListhttp://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove(java.lang.Object).
So, you can do something like this:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : "ctx._source.tags.remove(tag)",
"params" : {
"tag" : "blue"
}
}'

On Friday, December 21, 2012 10:54:09 AM UTC-5, jkerai wrote:

Hi, I'm very new to ES and am evaluating it for my search needs

http://www.elasticsearch.org/guide/reference/api/update.html

shows how to add elements to an array in an existing indexed document.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags += tag",
"params" : {
    "tag" : "blue"
}

}'
The above should add a new element to the list.

Is it possible to remove elements from an array in a similar fashion?
e.g. the equivalent of

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags -= tag",
"params" : {
    "tag" : "blue"
}

}'

Aside from reading all the element entries and removing the one 1 want to
remove and updating the entire element using the _update endpoint.

--


(Jayant Kerai) #3

Excellent. That works like a dream.
And I appreciate you correcting my terminology. Thanks Igor!

Just to elaborate, my actual example is a little different from what I
posted but your solution works.

My example:

curl -XPUT 'http://localhost:9200/myindex/mytype/12' -d '{
"Name": "Entity 1",
"Description": "Jay Desc 1",
"Entities": [
{
"CategoryID": "1",
"CategoryName": "Office",
"EntityID": "1",
"EntityName": "UK"
},
{
"CategoryID": "1",
"CategoryName": "Office",
"EntityID": "2",
"EntityName": "South Africa"
},
{
"CategoryID": "2",
"CategoryName": "Team",
"EntityID": "3",
"EntityName": "Accounts"
},
{
"CategoryID": "2",
"CategoryName": "Team",
"EntityID": "4",
"EntityName": "Development"
}
]
}'

curl -XPOST 'http://localhost:9200/myindex/mytype/12/_update' -d
'{
"script" : "ctx._source.Entities.remove(Entities)",
"params" : {
"Entities":
{
"CategoryID": "2",
"CategoryName": "Team",
"EntityID": "4",
"EntityName": "Development"
}
}
}'

On Friday, 21 December 2012 15:54:09 UTC, jkerai wrote:

Hi, I'm very new to ES and am evaluating it for my search needs

http://www.elasticsearch.org/guide/reference/api/update.html

shows how to add elements to an array in an existing indexed document.

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags += tag",
"params" : {
    "tag" : "blue"
}

}'
The above should add a new element to the list.

Is it possible to remove elements from an array in a similar fashion?
e.g. the equivalent of

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{

"script" : "ctx._source.tags -= tag",
"params" : {
    "tag" : "blue"
}

}'

Aside from reading all the element entries and removing the one 1 want to
remove and updating the entire element using the _update endpoint.

--


(system) #4