# Script fields in nested query

**URL:** <https://discuss.elastic.co/t/script-fields-in-nested-query/144235>\
**Category:** Elasticsearch\
**Created:** [August 13, 2018, 8:31pm UTC](https://discuss.elastic.co/t/script-fields-in-nested-query/144235 "2018-08-13T20:31:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreacfm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreacfm/32/32243_2.png) [@andreacfm](https://discuss.elastic.co/u/andreacfm)\
**Post date:** [August 13, 2018, 8:31pm UTC](https://discuss.elastic.co/t/script-fields-in-nested-query/144235/1 "2018-08-13T20:31:39Z")

</div>

Is there a way to run a "script\_fields" adding fields to the returned inner\_hits? Example:

```
{
   "nested": {
      "inner_hits": {},
      "path": "companies",
      "score_mode": "sum",
      "query": {},
      "script_fields": {
         "overlap" : {
            "script": {
               "source": "................................",
               "params": {
                   "from": "2012-01-01",
                    "to": "2015-06-30"
                }
            }
         }                
      }         
}

```

I execute n nested queries passing to each one a set of specific params. The idea is having the source script to assign a value to the overlap field for each one of the inner hits based on the provided params.

---

<div class="post-metadata">

**Author:** ![andreacfm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andreacfm/32/32243_2.png) [@andreacfm](https://discuss.elastic.co/u/andreacfm)\
**Post date:** [August 14, 2018, 8:09pm UTC](https://discuss.elastic.co/t/script-fields-in-nested-query/144235/2 "2018-08-14T20:09:49Z")

</div>

Any suggestion?

---

<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:** [September 11, 2018, 8:09pm UTC](https://discuss.elastic.co/t/script-fields-in-nested-query/144235/3 "2018-09-11T20:09:50Z")

</div>

Closed on September 11, 2018, 8:09pm UTC

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