Mapping transform with nested field

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ec6d65b7-82dd-465c-8b00-98127b2666ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ab59dfd7-3002-4f3e-9571-8ec3dc5cd70b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.