# Nested top\_hits scoring question

**URL:** <https://discuss.elastic.co/t/nested-top-hits-scoring-question/166503>\
**Category:** Elasticsearch\
**Created:** [January 31, 2019, 9:09am UTC](https://discuss.elastic.co/t/nested-top-hits-scoring-question/166503 "2019-01-31T09:09:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slade](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@Slade](https://discuss.elastic.co/u/Slade)\
**Post date:** [January 31, 2019, 9:09am UTC](https://discuss.elastic.co/t/nested-top-hits-scoring-question/166503/1 "2019-01-31T09:09:54Z")

</div>

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:

```auto
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:

```auto
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?

```auto
{
  "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"
              }
            }
          ]
        }
      }
    }
  }
}

```

---

<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:** [February 28, 2019, 9:09am UTC](https://discuss.elastic.co/t/nested-top-hits-scoring-question/166503/2 "2019-02-28T09:09:58Z")

</div>

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