# Advanced scripts on nested entities

**URL:** <https://discuss.elastic.co/t/advanced-scripts-on-nested-entities/114709>\
**Category:** Elasticsearch\
**Created:** [January 9, 2018, 1:33pm UTC](https://discuss.elastic.co/t/advanced-scripts-on-nested-entities/114709 "2018-01-09T13:33:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![franck.lefebure](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/franck.lefebure/32/24332_2.png) [@franck.lefebure](https://discuss.elastic.co/u/franck.lefebure)\
**Post date:** [January 9, 2018, 1:33pm UTC](https://discuss.elastic.co/t/advanced-scripts-on-nested-entities/114709/1 "2018-01-09T13:33:37Z")

</div>

Hi,  
Regarding some mapping like this one :

```
{"root_entity": {"properties": {
  "root_long": {"type": "long"},
  "root_int": {"type": "integer"},
 "nested_entity": {
    "type": "nested",
    "properties": {
      "nested_long": {"type": "long"},
      "nested_int": {"type": "integer"},
      "nested_date": {"type": "date"}
}}}

```

What we need is to do scripting on the nested entities.  
The scripts need to select several entities and do cross-computation on this selection.  
I managed to write search, fields and aggregation scripts, using the document source

For example, I need to select 2 nested\_entities, based on some criterias, then compute the duration between the "nested\_date", so I can write some script fields like this one :

```
"script_fields" : {
  "test" : {
     "script" : "
        long d1=0;
        long d2=0;
        for(e in params._source['nested_entity']) {
           if (e['nested_int'] == 1 } d1 = e['nested_date'];
           if (e['nested_int'] == 2 } d2 = e['nested_date'];
        }
        return d2-d1"
    }
 }

```

I can search and aggregate with the same method.  
The problem with my approach is I use the document source, so the performance is poor.

I tried to write equivalent scripts using the doc values but I didn't succeed.  
Are there any other, and better, ways to fit my need ?

Franck

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [January 9, 2018, 7:48pm UTC](https://discuss.elastic.co/t/advanced-scripts-on-nested-entities/114709/2 "2018-01-09T19:48:56Z")

</div>

Doc values are not exposed for the nested docs in the parent (the parent is a distinct doc, inside lucene, from the nested children). Source is the only way to access them.

---

<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:** [February 6, 2018, 7:49pm UTC](https://discuss.elastic.co/t/advanced-scripts-on-nested-entities/114709/3 "2018-02-06T19:49:05Z")

</div>

Closed on February 6, 2018, 7:49pm UTC

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