# Elastic Search nested query and inner hits question

**URL:** <https://discuss.elastic.co/t/elastic-search-nested-query-and-inner-hits-question/91614>\
**Category:** Elasticsearch\
**Created:** [July 3, 2017, 9:22am UTC](https://discuss.elastic.co/t/elastic-search-nested-query-and-inner-hits-question/91614 "2017-07-03T09:22:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prinice\_Tony](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@Prinice\_Tony](https://discuss.elastic.co/u/Prinice_Tony)\
**Post date:** [July 3, 2017, 9:22am UTC](https://discuss.elastic.co/t/elastic-search-nested-query-and-inner-hits-question/91614/1 "2017-07-03T09:22:35Z")

</div>

Hi, I have a requirement with Nested Query as:

- Query hits parent level title, return the doc also with top N nested docs
- Query hits nested level product title, return the doc also with top N nested docs **in order of relevance with the query**
- Query hits nothing, return empty results

So I came to query like this:

GET test-index/\_search  
{  
"query": {  
"bool": {  
"must": {  
"function\_score": {  
"score\_mode": "sum",  
"query": {  
"bool": {   
"should": [  
{  
"multi\_match": {  
"query": "es",  
"type": "cross\_fields",  
"fields": ["Title^6"],  
"minimum\_should\_match": "50%"  
}  
},  
{  
"nested": {  
"path": "Products",  
"score\_mode": "sum",  
"query": {  
"multi\_match": {  
"query": "es",  
"type": "cross\_fields",  
"fields": ["Products.ProductTitle"],  
"minimum\_should\_match": "50%"  
}  
},  
"inner\_hits": {  
"name":"Products",  
"size": N,  
"sort": ["\_score"]  
}   
}  
}  
]  
}  
}  
}  
},  
"should": [  
{  
"nested": {  
"path": "Products",  
"score\_mode": "sum",  
"query": {  
"match\_all": {}  
},  
"inner\_hits": {  
"size": N,  
"name":"Products",  
"sort": ["\_score"]  
}  
}  
}  
]  
}  
}  
}

The problem is, the nested results in inner hits are not in order by relevance by expected.

If I separate the inner hits by "name" as Products1 and Products2, I found the order and score for nested docs are always determined by the second nested query results.

My question is how can I correct the query to meet the 3 requirements above, or at all nested and inner hits now is even not supporting this kind of query?

Thanks in advance

---

<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:** [July 31, 2017, 9:23am UTC](https://discuss.elastic.co/t/elastic-search-nested-query-and-inner-hits-question/91614/2 "2017-07-31T09:23:40Z")

</div>

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