# Using inner\_hits inside aggregation script

**URL:** <https://discuss.elastic.co/t/using-inner-hits-inside-aggregation-script/97575>\
**Category:** Elasticsearch\
**Created:** [August 18, 2017, 12:09pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-aggregation-script/97575 "2017-08-18T12:09:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akash\_Bhatia](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@Akash\_Bhatia](https://discuss.elastic.co/u/Akash_Bhatia)\
**Post date:** [August 18, 2017, 12:09pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-aggregation-script/97575/1 "2017-08-18T12:09:50Z")

</div>

in aggs, can we query(using script) the inner\_hits instead of \_source?  
{  
"sort": [  
{  
"date": {  
"order": "desc"  
},  
"\_score": {}  
}  
],  
"\_source": ["user\_id", "topic", "date", "\_id"],  
"query": {  
"bool": {  
"must": [  
{  
"bool": {  
"should": [  
{  
"match": {  
"user\_id": "ctAAzCHSAuurshFch"  
}  
},  
{  
"match": {  
"user\_id": "MuyrXaniHQ2qD7zrw"  
}  
}  
]  
}  
},  
{  
"range": {  
"date": {  
"gte": "now-20d",  
"lte": "now"  
}  
}  
},  
{  
"nested": {  
"path": "transcription",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"transcription.text": "this is it"  
}  
}  
],  
"filter": {  
"bool": {  
"must": [  
{  
"match": {  
"transcription.speaker": "Rep"  
}  
}  
]  
}  
}  
}  
},  
"inner\_hits": {  
"sort": [  
{  
"transcription.conv\_no": {  
"order": "asc"  
}  
}  
],  
"highlight": {  
"pre\_tags": [""],  
"post\_tags": [""],  
"fields": {  
"transcription.text": {  
"fragment\_size": 2000  
}  
}  
}  
}  
}  
}  
]  
}  
},  
"aggs" : {  
"segments" : {  
"date\_histogram" : {  
"field" : "date",  
"interval" : "week",  
"format" : "yyyy-MM-dd"  
}  
},  
"avg\_grade" : {  
"stats" : {  
"script" : {  
_"inline" : "if(params. **inner\_hits**!= null) { return 5} else {return -1}"_  
}  
}  
}  
}  
}

---

<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 15, 2017, 12:10pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-aggregation-script/97575/2 "2017-09-15T12:10:21Z")

</div>

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