# Aggregations along with painless scripted field

**URL:** <https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389>\
**Category:** Kibana\
**Created:** [December 28, 2017, 5:51am UTC](https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389 "2017-12-28T05:51:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 28, 2017, 5:51am UTC](https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389/1 "2017-12-28T05:51:59Z")

</div>

Hi ...  
can we use aggregations and painless in a scripted field ?

i want to achieve below.

// **Query**  
},  
"size": 0,  
"\_source": {  
"excludes": []  
},  
"aggs": {  
"3": {  
"terms": {  
"field": "alert.keyword",  
"size": 2,  
"order": {  
"1": "desc"  
}  
},  
"aggs": {  
"1": {  
"cardinality": {  
"field": "alert.keyword"  
}  
}  
}  
}  
}  
**//Result**

{  
"took": 140,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 684223,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"3": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"1": {  
"value": 1  
},  
"key": " Anomaly behavior",  
"doc\_count": 128  
},  
{  
"1": {  
"value": 1  
},  
"key": " Capability Definitions",  
"doc\_count": 10  
},  
{  
i want to use **key** and **doc\_count** in 'if, else' construct to return value through scripted field for visualization it in kibana.

example if [key] == 'Anomaly behavior' && [doc\_count] \> 50 ;return 10;  
else 2;

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [December 28, 2017, 8:13pm UTC](https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389/2 "2017-12-28T20:13:01Z")

</div>

You can't use `doc_count` from the terms aggregation in a scripted field, only properties on the object.

---

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 29, 2017, 4:33am UTC](https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389/3 "2017-12-29T04:33:02Z")

</div>

My requirements is on the basis of distinct count on a document field create a scripted field. Is there any function or method like distinct, count, sort and dedupe which can be directly used on a document field in painless script. Please shed some light, some example code...

Thank you

---

<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:** [January 26, 2018, 4:33am UTC](https://discuss.elastic.co/t/aggregations-along-with-painless-scripted-field/113389/4 "2018-01-26T04:33:40Z")

</div>

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