Elastic Search nested query and inner hits question

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

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