# How to use a script field inside Aggregate?

**URL:** <https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190>\
**Category:** Elasticsearch\
**Created:** [July 18, 2019, 11:58am UTC](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190 "2019-07-18T11:58:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![StarLancer](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@StarLancer](https://discuss.elastic.co/u/StarLancer)\
**Post date:** [July 18, 2019, 11:58am UTC](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190/1 "2019-07-18T11:58:00Z")

</div>

Hi!  
I want to use script fields inside Aggregate, I wrote this query:  
GET test/\_search  
{  
"size":0,  
"query": {  
"bool": {  
"filter": [  
{  
"term": {  
"eventType": 5  
}  
},  
{  
"range": {  
"createdDate": {  
"gte": "2018-01-01",  
"lt": "2019-01-01"  
}  
}  
}  
]  
}  
},  
"script\_fields": {  
"script\_sum": {  
"script": {  
"lang": "painless",  
"source": " doc['price'].value \* doc['audrate'].value"  
}  
}  
},  
"aggs": {  
"months": {  
"date\_histogram": {  
"field": "createdDate",  
"calendar\_interval": "month",  
"format": "MM-MMMM"  
},  
"aggs": {  
"test": {  
"sum": {  
"field": "script\_sum"  
}  
}  
}  
}  
}  
}

but the output result does not have any value for the script field "script\_sum".  
Is it possible to use script fields in aggregates or not?

I am using this version of ES 7.2

---

<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:** [July 26, 2019, 5:04pm UTC](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190/2 "2019-07-26T17:04:49Z")

</div>

Script fields are a feature which produce field values on the fly for the top N documents that match the given query. They are not useable in other parts of the API. In particular, since they are only calculated for top N, it would not be possible to use them for aggregations which operate on every document the query matches.

You will need to duplicate your script inside your sum aggregation. There is a documented example [using a script for a sum aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html#_script_10) which may help. To avoid duplication, you could also [store the script](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#modules-scripting-stored-scripts) and reference in both locations by name.

---

<div class="post-metadata">

**Author:** ![StarLancer](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@StarLancer](https://discuss.elastic.co/u/StarLancer)\
**Post date:** [August 19, 2019, 10:50am UTC](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190/3 "2019-08-19T10:50:23Z")

</div>

Thanks for the answer.  
We tried to do this, use scripts inside the aggregation, it turned out to be too slow, in the end we abandoned this idea, and transferred the calculation logic to the application.

---

<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 16, 2019, 10:50am UTC](https://discuss.elastic.co/t/how-to-use-a-script-field-inside-aggregate/191190/4 "2019-09-16T10:50:31Z")

</div>

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