# How to access a specific array object via object property in mvel?

**URL:** <https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003>\
**Category:** Elasticsearch\
**Created:** [March 14, 2012, 4:47pm UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003 "2012-03-14T16:47:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [March 14, 2012, 4:47pm UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003/1 "2012-03-14T16:47:46Z")

</div>

I'm attempting to use the new update function and attachment plugin to  
update a specific file object with the contents of the attachment. My  
mapping looks like this:

> <https://gist.github.com/digitalsanctum/2037762>

where "files" is an array of file objects. I've verified the following  
works:

UpdateResponse updateResponse = new UpdateRequestBuilder(transportClient,  
Index.THREAD.getName(),  
Index.THREAD.getType(),  
String.valueOf(threadID))  
.setScript("ctx.\_source.files[0].content = content")  
.addScriptParam("content", attachment)  
.execute().actionGet();

But I'd like to do something like ctx.\_source.files[fileID].content =  
content to update a specific file object by it's fileID property. I'm  
flexible on changing my mapping at this point in order to get this to work.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 16, 2012, 4:59pm UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003/2 "2012-03-16T16:59:27Z")

</div>

The best way I can think about is with a more complex script, where you  
have the file id on each file array element (based on the mappings, it  
looks like you do), iterate through the files, find the ones that match the  
file id, and then update its content.

On Wed, Mar 14, 2012 at 6:47 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:

> I'm attempting to use the new update function and attachment plugin to  
> update a specific file object with the contents of the attachment. My  
> mapping looks like this:
> 
> [thread.json · GitHub](https://gist.github.com/2037762)
> 
> where "files" is an array of file objects. I've verified the following  
> works:
> 
> UpdateResponse updateResponse = new UpdateRequestBuilder(transportClient,  
> Index.THREAD.getName(),  
> Index.THREAD.getType(),  
> String.valueOf(threadID))  
> .setScript("ctx.\_source.files[0].content = content")  
> .addScriptParam("content", attachment)  
> .execute().actionGet();
> 
> But I'd like to do something like ctx.\_source.files[fileID].content =  
> content to update a specific file object by it's fileID property. I'm  
> flexible on changing my mapping at this point in order to get this to work.

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [March 16, 2012, 8:47pm UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003/3 "2012-03-16T20:47:40Z")

</div>

Great. Is there an example of what that looks like?

On Friday, March 16, 2012 12:59:27 PM UTC-4, kimchy wrote:

> The best way I can think about is with a more complex script, where you  
> have the file id on each file array element (based on the mappings, it  
> looks like you do), iterate through the files, find the ones that match the  
> file id, and then update its content.
> 
> On Wed, Mar 14, 2012 at 6:47 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:
> 
> > I'm attempting to use the new update function and attachment plugin to  
> > update a specific file object with the contents of the attachment. My  
> > mapping looks like this:
> > 
> > [thread.json · GitHub](https://gist.github.com/2037762)
> > 
> > where "files" is an array of file objects. I've verified the following  
> > works:
> > 
> > UpdateResponse updateResponse = new UpdateRequestBuilder(transportClient,  
> > Index.THREAD.getName(),  
> > Index.THREAD.getType(),  
> > String.valueOf(threadID))  
> > .setScript("ctx.\_source.files[0].content = content")  
> > .addScriptParam("content", attachment)  
> > .execute().actionGet();
> > 
> > But I'd like to do something like ctx.\_source.files[fileID].content =  
> > content to update a specific file object by it's fileID property. I'm  
> > flexible on changing my mapping at this point in order to get this to work.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 17, 2012, 11:29am UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003/4 "2012-03-17T11:29:56Z")

</div>

No..., though you can use javascript as well as the scripting instead of  
mvel if it makes things simpler (using the lang-javascript plugin).

On Fri, Mar 16, 2012 at 10:47 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:

> Great. Is there an example of what that looks like?
> 
> On Friday, March 16, 2012 12:59:27 PM UTC-4, kimchy wrote:
> 
> > The best way I can think about is with a more complex script, where you  
> > have the file id on each file array element (based on the mappings, it  
> > looks like you do), iterate through the files, find the ones that match the  
> > file id, and then update its content.
> > 
> > On Wed, Mar 14, 2012 at 6:47 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:
> > 
> > > I'm attempting to use the new update function and attachment plugin to  
> > > update a specific file object with the contents of the attachment. My  
> > > mapping looks like this:
> > > 
> > > [https://gist.github.com/\*\*2037762](https://gist.github.com/**2037762) [https://gist.github.com/2037762](https://gist.github.com/2037762)
> > > 
> > > where "files" is an array of file objects. I've verified the following  
> > > works:
> > > 
> > > UpdateResponse updateResponse = new UpdateRequestBuilder(\*\*  
> > > transportClient,  
> > > Index.THREAD.getName(),  
> > > Index.THREAD.getType(),  
> > > String.valueOf(threadID))  
> > > .setScript("ctx.\_source.files[\*\*0].content = content")  
> > > .addScriptParam("content", attachment)  
> > > .execute().actionGet();
> > > 
> > > But I'd like to do something like ctx.\_source.files[fileID].\*\*content =  
> > > content to update a specific file object by it's fileID property. I'm  
> > > flexible on changing my mapping at this point in order to get this to work.

---

<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, 3:35am UTC](https://discuss.elastic.co/t/how-to-access-a-specific-array-object-via-object-property-in-mvel/7003/5 "2017-07-06T03:35:45Z")

</div>


