Elasticsearch two level sort in aggregation list

余生长醉 提交于 2020-01-03 17:27:31

问题


Currently I am sorting aggregations by document score, so most relevant items come first in aggregation list like below:

{
            'aggs' : {
                'guilds' : {
                    'terms' : {
                        'field' : 'guilds.title.original',
                        'order' : [{'max_score' : 'desc'}],
                        'aggs' : {
                            'max_score' : {
                                'script' : 'doc.score'
                            }
                        }
                    }
                }
            }
        }

I want to add another sort option to the order terms order array in my JSON. but when I do that like this :

{
        'order' : [{'max_score' : 'desc'}, {"_count" : "desc"},
    }

The second sort does not work. For example when all of the scores are equal it then should sort based on query but it does not work.


回答1:


As a correction to Andrei's answer ... to order aggregations by multiple criteria, you MUST create an array as shown in Terms Aggregation: Order and you MUST be using ElasticSearch 1.5 or later.

So, for Andrei's answer, the correction is: "order" : [ { "max_score": "desc" }, { "_count": "desc" } ]

As Andrei has it, ES will not complain but it will ONLY use the last item listed in the "order" element.




回答2:


I don't know how your 'aggs' is even working because I tried it and I had parsing errors in three places: "order" is not allowed to have that array structure, your second "aggs" should be placed outside the first "terms" aggs and, finally, the "max_score" aggs should have had a "max" type of "aggs". In my case, to make it work (and it does actually order properly), it should look like this:

  "aggs": {
    "guilds": {
      "terms": {
        "field": "guilds.title.original",
        "order": {
          "max_score": "desc", 
          "_count": "desc"
        }
      },
      "aggs": {
        "max_score": {
          "max": {
            "script": "doc.score"
          }
        }
      }
    }
  }


来源:https://stackoverflow.com/questions/26400307/elasticsearch-two-level-sort-in-aggregation-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!