Incorrect Elasticsearch Sort

When I make a request like this

curl -XGET "http://localhost:9200/log/travels/_search?pretty" -d '                                                                 
{
    "aggs":{
        "travelers":{
            "terms":{
                "field":"traveler",
                "shard_size":0,
                "size":5,
                "order":{
                    "cities":"desc"
                }
            },
            "aggs":{
                "cities":{
                    "nested":{
                        "path":"cities"
                    },
                    "aggs":{
                        "city_count":{
                            "cardinality":{
                                "field":"cities.name"
                            }
                        }
                    }
                }
            }
        }
    }
}'

I get a response that I ordered incorrectly, for example

"aggregations" : {
    "travelers" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 410,
      "buckets" : [ {
        "key" : "patrick",
        "doc_count" : 9,
        "cities" : {
          "doc_count" : 10,
          "city_count" : {
            "value" : 3
          }
        }
      }, {
        "key" : "jonathan",
        "doc_count" : 8,
        "cities" : {
          "doc_count" : 10,
          "city_count" : {
            "value" : 4
          }
        }
      }, {
        "key" : "yosef",
        "doc_count" : 8,
        "cities" : {
          "doc_count" : 10,
          "city_count" : {
            "value" : 4
          }
        }
      }, {
        "key" : "mark",
        "doc_count" : 8,
        "cities" : {
          "doc_count" : 9,
          "city_count" : {
            "value" : 2
          }
        }
      }, {
        "key" : "mike",
        "doc_count" : 7,
        "cities" : {
          "doc_count" : 9,
          "city_count" : {
            "value" : 5
          }
        }
      } ]
    }
  }

I wanted it to be ordered by the number of cities. If I change the power to value_count, it is ordered correctly, but I cannot do this because it counts duplicates.

If more detailed information helps you, feel free to ask.

+4
source share
1 answer

If you are trying to sort by sub-aggregation at level 2 depth, the syntax is orderslightly different from the documentation (look at the end of the paragraph).

ElasticSearch.

- :

"order":{ "cities>city_count.value":"desc" }

, !

+6

All Articles