# Mapping transform with nested field

**URL:** <https://discuss.elastic.co/t/mapping-transform-with-nested-field/20800>\
**Category:** Elasticsearch\
**Created:** [November 17, 2014, 11:37pm UTC](https://discuss.elastic.co/t/mapping-transform-with-nested-field/20800 "2014-11-17T23:37:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benmccann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benmccann/32/420_2.png) [@benmccann](https://discuss.elastic.co/u/benmccann)\
**Post date:** [November 17, 2014, 11:37pm UTC](https://discuss.elastic.co/t/mapping-transform-with-nested-field/20800/1 "2014-11-17T23:37:00Z")

</div>

Hi,

I have a document which has an array of objects as a field. I'd like to  
count the length of this array filtering on an attribute of the objects,  
but am having a really hard time.

E.g. I'd like to use a script to create a computed field with a value of 3  
for this document:  
{  
myfield: [{  
value: 'a'  
}, {  
value: 'b'  
exclude: true  
}, {  
value: 'c'  
}, {  
value: 'd'  
}]  
}

There are two general approaches I've thought of. The first would be:

ctx.\_source['newfield'] = ctx.\_source['myfield'].values.size() -  
ctx.\_source['myfield.exclude'].values.size();

The second approach could be something like:

i = 0; for (value in ctx.\_source['myfield']?.values) { if(!value.exclude) {  
i++; } }; ctx.\_source['newfield'] = i

Do either of these sound like an approach I should focus on over the other?  
Is one likely to be much faster or impossible? I've not had any luck with  
either route thus far.

Thanks,  
Ben

--  
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/ec6d65b7-82dd-465c-8b00-98127b2666ef%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ec6d65b7-82dd-465c-8b00-98127b2666ef%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![benmccann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benmccann/32/420_2.png) [@benmccann](https://discuss.elastic.co/u/benmccann)\
**Post date:** [November 18, 2014, 12:57am UTC](https://discuss.elastic.co/t/mapping-transform-with-nested-field/20800/2 "2014-11-18T00:57:07Z")

</div>

I got it figured out. There's no .values. What was really helpful was to  
debug using script\_fields

{  
"query": {  
"ids": {  
"values": ["myid"]  
}  
},  
"script\_fields" : {  
"test1" : {  
"script": "\_source?.myfield",  
"lang": "groovy"  
},  
"test2" : {  
"script": "int i = 0; for (value in \_source['myfield']) { if  
(!value.exclude) { i++; } }; i",  
"lang": "groovy"  
}  
}  
}

On Monday, November 17, 2014 3:37:00 PM UTC-8, Ben McCann wrote:

> Hi,
> 
> I have a document which has an array of objects as a field. I'd like to  
> count the length of this array filtering on an attribute of the objects,  
> but am having a really hard time.
> 
> E.g. I'd like to use a script to create a computed field with a value of 3  
> for this document:  
> {  
> myfield: [{  
> value: 'a'  
> }, {  
> value: 'b'  
> exclude: true  
> }, {  
> value: 'c'  
> }, {  
> value: 'd'  
> }]  
> }
> 
> There are two general approaches I've thought of. The first would be:
> 
> ctx.\_source['newfield'] = ctx.\_source['myfield'].values.size() -  
> ctx.\_source['myfield.exclude'].values.size();
> 
> The second approach could be something like:
> 
> i = 0; for (value in ctx.\_source['myfield']?.values) { if(!value.exclude)  
> { i++; } }; ctx.\_source['newfield'] = i
> 
> Do either of these sound like an approach I should focus on over the  
> other? Is one likely to be much faster or impossible? I've not had any luck  
> with either route thus far.
> 
> Thanks,  
> Ben

--  
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/ab59dfd7-3002-4f3e-9571-8ec3dc5cd70b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ab59dfd7-3002-4f3e-9571-8ec3dc5cd70b%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, 12:49am UTC](https://discuss.elastic.co/t/mapping-transform-with-nested-field/20800/3 "2017-07-06T00:49:21Z")

</div>


