# Inline script to loop through unknown field names

**URL:** <https://discuss.elastic.co/t/inline-script-to-loop-through-unknown-field-names/221915>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 3, 2020, 3:15pm UTC](https://discuss.elastic.co/t/inline-script-to-loop-through-unknown-field-names/221915 "2020-03-03T15:15:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marb/32/19403_2.png) [@marb](https://discuss.elastic.co/u/marb)\
**Post date:** [March 3, 2020, 3:15pm UTC](https://discuss.elastic.co/t/inline-script-to-loop-through-unknown-field-names/221915/1 "2020-03-03T15:15:19Z")

</div>

Hi,  
I have in my document this construct  
"data": {  
"group\_randomId1": {  
"date1": "ISO8601",  
"date2": "ISO8601"  
},  
"group\_randomId2": {  
"date1": "ISO8601",  
"date2": "ISO8601"  
},  
"group\_randomIdN": {  
"date1": "ISO8601",  
"date2": "ISO8601"  
}  
}

What I want to do is use a percentile aggregation to compute the percentiles on date2-date1.  
I'm blocked at this point because I actually would likle to have something like:  
{  
"size": 0,  
"aggs": {  
"toMove": {  
"percentiles": {  
"script": {  
"lang": "painless",  
"source": "doc['data.\*.date2'].value.getMillis() - doc['data.\*.date1'].value.getMillis()"  
}  
}  
}  
}  
}

The '\*' notation is not supported so how can I write my script to loop through the different groups?  
The data groups are added by upsert operations (and a pipeline processor).

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![marb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marb/32/19403_2.png) [@marb](https://discuss.elastic.co/u/marb)\
**Post date:** [March 5, 2020, 5:37pm UTC](https://discuss.elastic.co/t/inline-script-to-loop-through-unknown-field-names/221915/2 "2020-03-05T17:37:02Z")

</div>

bumping this message.

Thinking more about it, even if I use an array representation  
"data": [  
{  
"date1": "ISO8601",  
"date2": "ISO8601"  
},  
{  
"date1": "ISO8601",  
"date2": "ISO8601"  
},  
{  
"date1": "ISO8601",  
"date2": "ISO8601"  
}  
]  
that would simplify the iteration part but

1. I'm not sure I can use an ingest script pipeline to update an existing data field incrementally?
2. I will probably have to use an upsert + a script from my client app to update this document data field
3. not sure I can keep the correlation between a date1 and date2 field in my search query percentile query?

I really needed to use an ingest pipeline to update this data field because one of the date I need to systematically inject is the indexation timestamp : {{\_ingest.timestamp}}

---

<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:** [April 2, 2020, 5:37pm UTC](https://discuss.elastic.co/t/inline-script-to-loop-through-unknown-field-names/221915/3 "2020-04-02T17:37:03Z")

</div>

Closed on April 2, 2020, 5:37pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
