# Get inner hits for nested query inside bool query

**URL:** <https://discuss.elastic.co/t/get-inner-hits-for-nested-query-inside-bool-query/169023>\
**Category:** Elasticsearch\
**Created:** [February 19, 2019, 1:02pm UTC](https://discuss.elastic.co/t/get-inner-hits-for-nested-query-inside-bool-query/169023 "2019-02-19T13:02:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaibhavnegi](https://avatars.discourse-cdn.com/v4/letter/v/fbc32d/32.png) [@vaibhavnegi](https://discuss.elastic.co/u/vaibhavnegi)\
**Post date:** [February 19, 2019, 1:02pm UTC](https://discuss.elastic.co/t/get-inner-hits-for-nested-query-inside-bool-query/169023/1 "2019-02-19T13:02:52Z")

</div>

In the index, i have a task which has nested segments which further have nested labels.  
problem - Search tasks and segments which have label LBL\_A and label LBL\_B.  
and return matching tasks , matching segments within task, matching labels within matching segment.  
By nature of problem , user is allowed to search for any combinations of labels. so i do a nested query for segments which has a bool query with must, should operators and multiple nested label queries inside it.  
I am able to search and return matching segments. but i could not figure out how to return matching labels.  
if i wrap bool query inside a nested query . must operator does not work because i need segments where there are two labels with respective label names and nested bool query looks for the two label names in the same label which does not match.  
If i use inner hits with nested label query and then post process to compile all inner hits into one array, it does not for corner cases. example search for segments where lblA and lblB are present or lblC and lblD are present. if segment has lblA and lblB and lblC, it will match but it will also return lblC as inner hit.

the solution that i think would have worked is making bool query return inner hits. but bool query does not have inner hits option.

I hope i am able to explain the problem.

Mapping -

{  
"mappings": {  
"\_doc": {  
"dynamic": "strict",  
"properties": {  
"task\_id": { "type": "integer" },  
"created": {  
"type": "date",  
"format": "strict\_date\_optional\_time||epoch\_millis"  
},  
"segments": {  
"type": "nested",  
"dynamic": "strict",  
"properties": {  
"segment\_id": { "type": "integer"},  
"labels" : {  
"type": "nested",  
"dynamic": "strict",  
"properties": {  
"name": { "type": "keyword"}  
}  
}  
}  
}  
}  
}  
}  
}

---

<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:** [March 19, 2019, 1:02pm UTC](https://discuss.elastic.co/t/get-inner-hits-for-nested-query-inside-bool-query/169023/2 "2019-03-19T13:02:57Z")

</div>

Closed on March 19, 2019, 1:02pm UTC

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