# How to sort bucket terms in a nested aggregation on score instead of doc count? (Nested field aggregation not allowed with nested field query)

**URL:** <https://discuss.elastic.co/t/how-to-sort-bucket-terms-in-a-nested-aggregation-on-score-instead-of-doc-count-nested-field-aggregation-not-allowed-with-nested-field-query/120547>\
**Category:** Elasticsearch\
**Created:** [February 20, 2018, 2:28am UTC](https://discuss.elastic.co/t/how-to-sort-bucket-terms-in-a-nested-aggregation-on-score-instead-of-doc-count-nested-field-aggregation-not-allowed-with-nested-field-query/120547 "2018-02-20T02:28:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aguyinmontreal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aguyinmontreal/32/37784_2.png) [@aguyinmontreal](https://discuss.elastic.co/u/aguyinmontreal)\
**Post date:** [February 20, 2018, 2:28am UTC](https://discuss.elastic.co/t/how-to-sort-bucket-terms-in-a-nested-aggregation-on-score-instead-of-doc-count-nested-field-aggregation-not-allowed-with-nested-field-query/120547/1 "2018-02-20T02:28:37Z")

</div>

I'm trying to sort my term buckets on score relevancy instead of doc count. I'm trying do to something like [this](https://stackoverflow.com/a/43929415/6814172). However, nested field aggregations are not allowed to be added to nested queries.

What are my options?

Here is my current working code (which sorts my term buckets on doc count):

```
POST myIndex/_search
{
  "size": 0,

   "aggs": {
      "nested_nestedobjects": {
         "nested": {
            "path": "searchTags_french"
         },
         "aggs": {
         "filtered_nestedobjects": {
               "filter": {
                  "match": {
                     "searchTags_french.tag": {
                       "fuzziness": "AUTO",
                       "operator": "and",
                       "query": "test query"}

                  }
               },
               "aggs": {
                  "my_agg": {
                     "terms": {
                       "size": 20,
                        "field": "searchTags_french.tag.raw"
                     }
                  }
               }
            }
         }
      }
   }
}

```

My mapping:

```
"searchTags_french": {
      "type": "nested",
      "properties": {
        "tag": {
          "type": "text",
          "analyzer": "completion_analyzer_french",
          "search_analyzer": "completion_search_analyzer_french",
          "fields": {
            "raw": {
              "type": "keyword",
              "index": "true"
            }
          }
        }
      }
    }

```

P.s. I'm using ElasticSearch 6.2

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 20, 2018, 10:11pm UTC](https://discuss.elastic.co/t/how-to-sort-bucket-terms-in-a-nested-aggregation-on-score-instead-of-doc-count-nested-field-aggregation-not-allowed-with-nested-field-query/120547/3 "2018-02-20T22:11:43Z")

</div>

Read [this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and specifically the "Also be patient" part.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Could you also provide the current output and the expected output?  
I don't think you can order terms bucket by score as score does not make sense in a Terms Agg IMHO.

---

<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 20, 2018, 10:12pm UTC](https://discuss.elastic.co/t/how-to-sort-bucket-terms-in-a-nested-aggregation-on-score-instead-of-doc-count-nested-field-aggregation-not-allowed-with-nested-field-query/120547/4 "2018-03-20T22:12:19Z")

</div>

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