# Help needed scripting update to list element (bulk request)

**URL:** <https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524>\
**Category:** Elasticsearch\
**Created:** [August 29, 2014, 9:33am UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524 "2014-08-29T09:33:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [August 29, 2014, 9:33am UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/1 "2014-08-29T09:33:23Z")

</div>

Hi  
Say I have a list of elements like this:

PUT twitter/twit/1  
{  
"list": [  
{  
"a": "b",  
"c": "d",

- 

```
         "e": "f"*
      },
      {
        "1": "2",
        "3": "4"
      }
  ]

```

}

And I want to change the value of "e" (currently "f") to say "new\_f " such  
that the document looks like:

{  
"list": [  
{  
"a": "b",  
"c": "d",

- 

```
         "e": "new_f"*
      },
      {
        "1": "2",
        "3": "4"
      }
  ]

```

}

Is there a way to do this ? Maybe in MVEL ?

Do I match on document  
{  
"a": "b",  
"c": "d",  
"e": "f"  
}

ie if list.contains(document) { some kind of update; } // is this possible  
?

I know MVEL is being deprecated in 1.4 however it will do for now.

I want to use bulk request.

I know it's possible to remove the element like this:  
bulkRequestBuilder.setScript("if (ctx.\_source.list.contains(document))  
{ctx.\_source.list.remove(document)} }").setScriptParams etc

but is it possible to "update" a field in the document also ?

Thanks.

--  
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/da238495-7cf3-4215-a77e-2144499b8859%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/da238495-7cf3-4215-a77e-2144499b8859%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [August 31, 2014, 2:59pm UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/2 "2014-08-31T14:59:56Z")

</div>

Any takers?  
David Pilato?

On Friday, August 29, 2014 10:33:23 AM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:

> Hi  
> Say I have a list of elements like this:
> 
> PUT twitter/twit/1  
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",
> 
> - 
> 
> ```
> "e": "f"*
> },
> {
> "1": "2",
> "3": "4"
> }
> ]
> 
> ```
> 
> }
> 
> And I want to change the value of "e" (currently "f") to say "new\_f " such  
> that the document looks like:
> 
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",
> 
> - 
> 
> ```
> "e": "new_f"*
> },
> {
> "1": "2",
> "3": "4"
> }
> ]
> 
> ```
> 
> }
> 
> Is there a way to do this ? Maybe in MVEL ?
> 
> Do I match on document  
> {  
> "a": "b",  
> "c": "d",  
> "e": "f"  
> }
> 
> ie if list.contains(document) { some kind of update; } // is this  
> possible ?
> 
> I know MVEL is being deprecated in 1.4 however it will do for now.
> 
> I want to use bulk request.
> 
> I know it's possible to remove the element like this:  
> bulkRequestBuilder.setScript("if (ctx.\_source.list.contains(document))  
> {ctx.\_source.list.remove(document)} }").setScriptParams etc
> 
> but is it possible to "update" a field in the document also ?
> 
> Thanks.

--  
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/571fa34d-f7fa-4f17-81c2-1288a0357d68%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/571fa34d-f7fa-4f17-81c2-1288a0357d68%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [August 31, 2014, 6:22pm UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/3 "2014-08-31T18:22:18Z")

</div>

Hey

I figured it out, but it leads me on to another question.

So the way to execute a script over a list of elements is as follows:

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"  
}  
}

My next question: how can I query the index to find all matching documents  
with say "tweet\_id": "123" and then execute the script on only those  
documents?

When I tried  
.. query..  
{  
"filter": {  
"script": "foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==  
tweet\_id) { item['new\_field'] = 'ghi'; } }",  
"params": {  
"tweet\_id": "123"  
}  
}  
}  
I got an error saying property : ctx : unresolvable

Thanks.

On Friday, August 29, 2014 10:33:23 AM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:

> Hi  
> Say I have a list of elements like this:
> 
> PUT twitter/twit/1  
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",
> 
> - 
> 
> ```
> "e": "f"*
> },
> {
> "1": "2",
> "3": "4"
> }
> ]
> 
> ```
> 
> }
> 
> And I want to change the value of "e" (currently "f") to say "new\_f " such  
> that the document looks like:
> 
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",
> 
> - 
> 
> ```
> "e": "new_f"*
> },
> {
> "1": "2",
> "3": "4"
> }
> ]
> 
> ```
> 
> }
> 
> Is there a way to do this ? Maybe in MVEL ?
> 
> Do I match on document  
> {  
> "a": "b",  
> "c": "d",  
> "e": "f"  
> }
> 
> ie if list.contains(document) { some kind of update; } // is this  
> possible ?
> 
> I know MVEL is being deprecated in 1.4 however it will do for now.
> 
> I want to use bulk request.
> 
> I know it's possible to remove the element like this:  
> bulkRequestBuilder.setScript("if (ctx.\_source.list.contains(document))  
> {ctx.\_source.list.remove(document)} }").setScriptParams etc
> 
> but is it possible to "update" a field in the document also ?
> 
> Thanks.

--  
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/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [August 31, 2014, 6:48pm UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/4 "2014-08-31T18:48:27Z")

</div>

You can't do it with elasticsearch but you could try this plugin: [GitHub - yakaz/elasticsearch-action-updatebyquery: ElasticSearch Update By Query action plugin](https://github.com/yakaz/elasticsearch-action-updatebyquery)

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 31 août 2014 à 20:22, [eunever32@gmail.com](mailto:eunever32@gmail.com) a écrit :

Hey

I figured it out, but it leads me on to another question.

So the way to execute a script over a list of elements is as follows:

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"  
}  
}

My next question: how can I query the index to find all matching documents with say "tweet\_id": "123" and then execute the script on only those documents?

When I tried  
.. query..  
{  
"filter": {  
"script": "foreach (item : ctx.\_source.list) {if (item['tweet\_id'] == tweet\_id) { item['new\_field'] = 'ghi'; } }",  
"params": {  
"tweet\_id": "123"  
}  
}  
}  
I got an error saying property : ctx : unresolvable

Thanks.

> On Friday, August 29, 2014 10:33:23 AM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:  
> Hi  
> Say I have a list of elements like this:
> 
> PUT twitter/twit/1  
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",  
> "e": "f"  
> },  
> {  
> "1": "2",  
> "3": "4"  
> }  
> ]  
> }
> 
> And I want to change the value of "e" (currently "f") to say "new\_f " such that the document looks like:
> 
> {  
> "list": [  
> {  
> "a": "b",  
> "c": "d",  
> "e": "new\_f"  
> },  
> {  
> "1": "2",  
> "3": "4"  
> }  
> ]  
> }
> 
> Is there a way to do this ? Maybe in MVEL ?
> 
> Do I match on document  
> {  
> "a": "b",  
> "c": "d",  
> "e": "f"  
> }
> 
> ie if list.contains(document) { some kind of update; } // is this possible ?
> 
> I know MVEL is being deprecated in 1.4 however it will do for now.
> 
> I want to use bulk request.
> 
> I know it's possible to remove the element like this:  
> bulkRequestBuilder.setScript("if (ctx.\_source.list.contains(document)) {ctx.\_source.list.remove(document)} }").setScriptParams etc
> 
> but is it possible to "update" a field in the document also ?
> 
> Thanks.

--  
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/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/234C2E37-8860-4015-84A3-6326C27DA17F%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/234C2E37-8860-4015-84A3-6326C27DA17F%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![eunever32](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@eunever32](https://discuss.elastic.co/u/eunever32)\
**Post date:** [August 31, 2014, 7:43pm UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/5 "2014-08-31T19:43:56Z")

</div>

Thanks David

On Sunday, August 31, 2014 7:48:38 PM UTC+1, David Pilato wrote:

> You can't do it with elasticsearch but you could try this plugin:  
> [GitHub - yakaz/elasticsearch-action-updatebyquery: ElasticSearch Update By Query action plugin](https://github.com/yakaz/elasticsearch-action-updatebyquery)
> 
> HTH
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 31 août 2014 à 20:22, [eune...@gmail.com](mailto:eune...@gmail.com) \<javascript:\> a écrit :
> 
> Hey
> 
> I figured it out, but it leads me on to another question.
> 
> So the way to execute a script over a list of elements is as follows:
> 
> 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"  
> }  
> }
> 
> My next question: how can I query the index to find all matching documents  
> with say "tweet\_id": "123" and then execute the script on only those  
> documents?
> 
> When I tried  
> .. query..  
> {  
> "filter": {  
> "script": "foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==  
> tweet\_id) { item['new\_field'] = 'ghi'; } }",  
> "params": {  
> "tweet\_id": "123"  
> }  
> }  
> }  
> I got an error saying property : ctx : unresolvable
> 
> Thanks.
> 
> On Friday, August 29, 2014 10:33:23 AM UTC+1, [eune...@gmail.com](mailto:eune...@gmail.com) wrote:
> 
> > Hi  
> > Say I have a list of elements like this:
> > 
> > PUT twitter/twit/1  
> > {  
> > "list": [  
> > {  
> > "a": "b",  
> > "c": "d",
> > 
> > - 
> > 
> > ```
> > "e": "f"*
> > },
> > {
> > "1": "2",
> > "3": "4"
> > }
> > ]
> > 
> > ```
> > 
> > }
> > 
> > And I want to change the value of "e" (currently "f") to say "new\_f "  
> > such that the document looks like:
> > 
> > {  
> > "list": [  
> > {  
> > "a": "b",  
> > "c": "d",
> > 
> > - 
> > 
> > ```
> > "e": "new_f"*
> > },
> > {
> > "1": "2",
> > "3": "4"
> > }
> > ]
> > 
> > ```
> > 
> > }
> > 
> > Is there a way to do this ? Maybe in MVEL ?
> > 
> > Do I match on document  
> > {  
> > "a": "b",  
> > "c": "d",  
> > "e": "f"  
> > }
> > 
> > ie if list.contains(document) { some kind of update; } // is this  
> > possible ?
> > 
> > I know MVEL is being deprecated in 1.4 however it will do for now.
> > 
> > I want to use bulk request.
> > 
> > I know it's possible to remove the element like this:  
> > bulkRequestBuilder.setScript("if (ctx.\_source.list.contains(document))  
> > {ctx.\_source.list.remove(document)} }").setScriptParams etc
> > 
> > but is it possible to "update" a field in the document also ?
> > 
> > Thanks.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5c641a4e-a02f-4ebe-972f-cd2a320d15d4%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/03702904-fd4a-4fed-8bb7-4204dccebfd8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/03702904-fd4a-4fed-8bb7-4204dccebfd8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:05am UTC](https://discuss.elastic.co/t/help-needed-scripting-update-to-list-element-bulk-request/19524/6 "2017-07-06T01:05:16Z")

</div>


