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

I'm trying to sort my term buckets on score relevancy instead of doc count. I'm trying do to something like this. 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

Read this and specifically the "Also be patient" part.

Could you provide a full recreation script as described in About the Elasticsearch category. 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.

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