How to update a object within array in elastic search

I have got my answer, and its solution is

POST /twitter/twit/1/_update

{
  "script": "item_to_remove = nil; foreach (item : ctx._source.list) { 

if (item['tweet_id'] == tweet_id) { item_to_remove=item; } } if
(item_to_remove != nil) ctx._source.list.remove(item_to_remove);",
"params": {"tweet_id": "123"}
}

if you have more than one item that matches the criteria, use a list
instead:

POST /twitter/twit/1/_update
{
  "script": "items_to_remove = []; foreach (item : ctx._source.list) { 

if (item['tweet_id'] == tweet_id) { items_to_remove.add(item); } } foreach
(item : items_to_remove) {ctx._source.list.remove(item);}",
"params": {"tweet_id": "123"}
}

On Tuesday, June 24, 2014 12:48:17 PM UTC+5:30, Madhumita Sadhukhan wrote:

I have a requirement where I need to update(not append) existing values
within a list or array in Elastic Search.
Is this feature supported in Elasticsearch?

For eg:

I have a field called jobs as part of my document

"jobs": [
{
"status": "InProgress",
"runId": 1,
"start_date": 2101112,
"orderId": "undefined"
},
{
"status": "InProgress",
"runId": 2,
"start_date": 2101112,
"orderId": "undefined"
},
],
and I am required to update the orderId for each job run to different
values.
Currently I am only able to append a job but I cannot update the
attributes of each job later.
Is this usecase supported and possible in Elastic Search?

On Wednesday, October 8, 2014 4:44:45 PM UTC+5:30, Rajit Garg wrote:

*Hey *

I figured it out, I am able to update but I am not able to remove object
that satisfies the condition

PUT twitter/twit/1
{
"list": [
{
"tweet_id": "1",
"a": "b"
},
{
"tweet_id": "123",
"a": "f"
}
]
}

POST /twitter/twit/1/_update
{
"script": "foreach (item : ctx._source.list) {if (item['tweet_id'] ==
tweet_id) { item['new_field'] = 'ghi'; } }",
"params": {
"tweet_id": "123"
}
}
this is working

for remove i am doing this

POST /twitter/twit/1/_update
{
"script": "foreach (item : ctx._source.list) {if (item['tweet_id'] ==
tweet_id) {ctx._source.list.remove(item); } }",
"params": {
"tweet_id": "123"
}
}

*but this is not working and giving this error, *

ElasticsearchIllegalArgumentException[failed to execute script]; nested:
ConcurrentModificationException;
Error: ElasticsearchIllegalArgumentException[failed to execute script];
nested: ConcurrentModificationException;
..................................
...............................

please help........

On Wednesday, October 8, 2014 12:42:11 PM UTC+5:30, Rajit Garg wrote:

I have read the nested docs and create mapping and get mapping as follow

"fields": {
"type": "nested",
"properties": {
"_id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}

where fields is my array contain the following

"fields": [
{
"_id": "1",
"name": "naveen"
},
{
"_id": "2",
"name": "rajit"
},
{
"_id": "3",
"name": "ashu"
}
]

I want to update and remove object from this array, and I am doing this,

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {
"script" : "for (int i = 0; i < ctx._source.fields.size(); i++){
if(ctx._source.fields[i]_id == id){
ctx._source.fields[i].name = 'Updated John';
}
}",
"params" : {
"id" : "1"
}
}}

and I am getting this error

VerifyError[Bad type on operand stack
Exception Details:
Location:

ASMAccessorImpl_18539991071412750271920.getValue(Ljava/lang/Object;Ljava/lang/Object;Lorg/elasticsearch/common/mvel2/integration/VariableResolverFactory;)Ljava/lang/Object;
@49: invokeinterface
Reason:
Type 'java/lang/Object' (current frame, stack[1]) is not assignable
to integer
Current Frame:
bci: @49
flags: { }
locals: { 'ASMAccessorImpl_18539991071412750271920',
'java/lang/Object', 'java/lang/Object',
'org/elasticsearch/common/mvel2/integration/VariableResolverFactory' }
stack: { 'java/util/List', 'java/lang/Object' }............
.............................................................
.........................................................

then I have tried this

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {
"script" : "for (int i = 0; i < ctx._source.fields.size(); i++){
if(ctx._source.fields[i]_id == id){
ctx._source.fields.remove(i);
}
}",
"params" : {
"id" : "1"
}
}}

and this is giving the same type of error

then I have tried

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {
"script" : "for (element : doc['fields'].values){
if(element._id == id){
element.name = 'Updated John';
}
}",
"params" : {
"id" : "1"
}
}}

which gives this error

elasticsearchIllegalArgumentException[failed to execute script]; nested:
PropertyAccessException[[Error: unresolvable property or identifier: doc]

please help....

On Tuesday, October 7, 2014 7:08:52 PM UTC+5:30, Rajit Garg wrote:

Here I have a array type field in my document of Elasticsearch, like
below:-

"prog_lists": [
{
"description": "engineer",
"age": 25,
"name": "ashu"
},
{
"description": "programmer",
"age": 26,
"name": "rajit"
},
{
"description": "designer",
"age": 27,
"name": "naveen"
}
]

I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.

please suggest something....

Thanks in advance....

--
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/50df0c6f-e570-4f5b-b3f3-a47e74fdc8c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.