# Scripted Metric not considering the data from all shards . showing in different blocks

**URL:** <https://discuss.elastic.co/t/scripted-metric-not-considering-the-data-from-all-shards-showing-in-different-blocks/228896>\
**Category:** Elasticsearch\
**Created:** [April 20, 2020, 3:29pm UTC](https://discuss.elastic.co/t/scripted-metric-not-considering-the-data-from-all-shards-showing-in-different-blocks/228896 "2020-04-20T15:29:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjilinga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anjilinga/32/86010_2.png) [@anjilinga](https://discuss.elastic.co/u/anjilinga)\
**Post date:** [April 20, 2020, 3:29pm UTC](https://discuss.elastic.co/t/scripted-metric-not-considering-the-data-from-all-shards-showing-in-different-blocks/228896/1 "2020-04-20T15:29:09Z")

</div>

Hi All,  
I have used the below query to get the scripted metric data. but the data is displaying in different shards. Is it possible to get the data in single Array insted of multiple arrays.

Query: GET act\*/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"starttime": {  
"gte": "2019-07-14 23:00:00.000",  
"lte": "2019-07-15 22:59:59.000"  
}  
}  
}  
],  
"filter": [  
{  
"bool": {  
"should": [  
{  
"match\_phrase": {  
"trunkgroupname": "100078012\_testingPPIsite/PPIheadertrunk"  
}  
}  
]  
}  
}  
]  
}  
},  
"sort": [  
{ "starttime": { "order": "desc" }}  
],  
"size": 0,  
"aggs": {  
"max\_outgoing": {  
"scripted\_metric": {  
"init\_script" : "state.transactions = ; state.endtrans=",  
"map\_script" : "if(doc['direction'] !=0 && doc['direction'].value == 'Originating') {state.transactions.add(doc['starttime'].value.hour_60_60+doc['starttime'].value.minute_60+doc['starttime'].value.second);state.endtrans.add(doc['releasetime'].value.hour_60_60+doc['releasetime'].value.minute_60+doc['releasetime'].value.second);}",  
"combine\_script" : "return state.transactions",  
"reduce\_script" : " return states"  
}  
}  
}  
}

Result:

{  
"took" : 157,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"skipped" : 0,  
"failed" : 0  
},  
"hits" : {  
"total" : {  
"value" : 31,  
"relation" : "eq"  
},  
"max\_score" : null,  
"hits" :   
},  
"aggregations" : {  
"max\_outgoing" : {  
"value" : [  
[  
36715,  
39328,  
53360,  
37958,  
36715,  
37958,  
53360,  
36715,  
39328,  
53360  
],  
[  
38148,  
36715,  
38148,  
39328,  
53360,  
38148,  
39328,  
37958,  
38148  
],  
[  
37958,  
53360,  
37958,  
36715,  
38148,  
39328  
]  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![anjilinga](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anjilinga/32/86010_2.png) [@anjilinga](https://discuss.elastic.co/u/anjilinga)\
**Post date:** [April 28, 2020, 9:09am UTC](https://discuss.elastic.co/t/scripted-metric-not-considering-the-data-from-all-shards-showing-in-different-blocks/228896/2 "2020-04-28T09:09:41Z")

</div>

It is working fine.  
Add the count from all the shards in reduce script

---

<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:** [May 26, 2020, 9:09am UTC](https://discuss.elastic.co/t/scripted-metric-not-considering-the-data-from-all-shards-showing-in-different-blocks/228896/3 "2020-05-26T09:09:46Z")

</div>

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