Nested top_hits scoring question

Okay so it seems that all I needed to do was to nest the the top hits aggregations within a filter aggregation and then have the bool query with the should clause within that filter aggregation.

I wish to retrieve nested documents with the top_hits aggregation with the results being sorted by their score descending.
Example data:

PUT /item

 {
    "mappings": {
        "_doc" : {
            "properties" : {
				"name": { "type" : "keyword" },
                "supplierName": { "type" : "keyword" },				
                "comments" : { 
                    "type" : "nested",
                    "properties" : {
                        "username" : { "type" : "keyword" },
                        "comment" : { "type" : "text" }
                    }
                }
            }
        }
    }
 }

PUT /item/_doc/1?refresh
{
     "name":"ItemOne",
     "supplierName":"CoolSupplier",
     "comments": [
         
         {"username": "steve", "comment": "Silly item"},
         {"username": "mark", "comment": "Cool item"},
         {"username": "jake", "comment": "Okay item"},
         {"username": "austin", "comment": "Horrible item"}
     ]
}

PUT /item/_doc/2?refresh
{
     "name":"ItemTwo",
     "supplierName":"CoolSupplier",
     "comments": [
         
         {"username": "steve", "comment": "Fantastic product"},
         {"username": "mark", "comment": "Product wasn't too good"},
         {"username": "jake", "comment": "Product is cool"},
         {"username": "austin", "comment": "Horrible product"}
     ]
}

When running the following query:

GET item/_search
{
  "size": 0,
  "aggs": {
    "nestedAggs": {
      "nested": {
        "path": "comments"
      },
      "aggs": {
        "topHitAggs": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  },
  "query": {
    "nested": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "comments.comment": {
                  "query": "horrible",
                  "boost": 5
                }
              }
            }
          ]
        }
      },
      "path": "comments"
    }
  }
}

I'd expect the comment which matches the nested should query to be the first result and the one with the highest score. So in this example I'd expect either "Horrible product" or "Horrible item" to be the first result which is not the case (see results below).
It seems however that all of the comments receive a boost in score even if only one of them matches the should query. Why is this?
What am I missing?

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "nestedAggs" : {
      "doc_count" : 8,
      "topHitAggs" : {
        "hits" : {
          "total" : 8,
          "max_score" : 6.775847,
          "hits" : [
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "2",
              "_nested" : {
                "field" : "comments",
                "offset" : 0
              },
              "_score" : 6.775847,
              "_source" : {
                "username" : "steve",
                "comment" : "Fantastic product"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "2",
              "_nested" : {
                "field" : "comments",
                "offset" : 1
              },
              "_score" : 6.775847,
              "_source" : {
                "username" : "mark",
                "comment" : "Product wasn't too good"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "2",
              "_nested" : {
                "field" : "comments",
                "offset" : 2
              },
              "_score" : 6.775847,
              "_source" : {
                "username" : "jake",
                "comment" : "Product is cool"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "2",
              "_nested" : {
                "field" : "comments",
                "offset" : 3
              },
              "_score" : 6.775847,
              "_source" : {
                "username" : "austin",
                "comment" : "Horrible product"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "1",
              "_nested" : {
                "field" : "comments",
                "offset" : 0
              },
              "_score" : 6.019864,
              "_source" : {
                "username" : "steve",
                "comment" : "Silly item"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "1",
              "_nested" : {
                "field" : "comments",
                "offset" : 1
              },
              "_score" : 6.019864,
              "_source" : {
                "username" : "mark",
                "comment" : "Cool item"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "1",
              "_nested" : {
                "field" : "comments",
                "offset" : 2
              },
              "_score" : 6.019864,
              "_source" : {
                "username" : "jake",
                "comment" : "Okay item"
              }
            },
            {
              "_index" : "item",
              "_type" : "_doc",
              "_id" : "1",
              "_nested" : {
                "field" : "comments",
                "offset" : 3
              },
              "_score" : 6.019864,
              "_source" : {
                "username" : "austin",
                "comment" : "Horrible item"
              }
            }
          ]
        }
      }
    }
  }
}

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