Elasticsearch aggregation order by top hit score Elasticsearch aggregation order by top hit score elasticsearch elasticsearch

Elasticsearch aggregation order by top hit score


I had the same issue, and the way I resolved it was to introduce a sub-aggregation on the docs score. Then in my outer aggregation, I ordered by name of the max_score aggregation.

GET /my-index/my-type/_search{  "query": {      "bool" : {        "should" : [ {          "match" : {            "searchTerm" : {              "query" : "style",              "type" : "boolean"            }          }        },         {          "flt_field" : {            "searchTerm" : {              "like_text" : "style"            }          }        }]      }    },    "aggs": {        "group_by_target_url": {          "terms": {            "field": "targetUrl",            "order": {              "max_score": "desc"            }          },          "aggs": {            "max_score": {              "max": {                "script": "doc.score"              }            }          }    }  }    }

I followed the directions on this link:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html