Sorting by multiple params in pyes and elasticsearch

不羁岁月 提交于 2019-12-04 19:24:23

问题


I can pass a single sort parameter to the search query in pyes like this:

s = MatchAllQuery()
conn.search(query=Search(s), indexes=["test"], sort='_score')

But I need to pass an extra parameter to sort the docs with the same score, like this:

{
  "sort": [
    "_score",
    {
      "extra_param": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "term": {
      "match_all": {}
    }
  }
}

How can I do this in pyes?

Thanks


回答1:


If you'd like the results in the result set with the same score to be ordered by price, append price to the sort string:

s = MatchAllQuery()
conn.search(query=Search(s), indexes=["test"], sort='_score,price')

By default the sort order is ascending. To pass the sort order append :asc or :desc to the sort parameter

s = MatchAllQuery()
conn.search(query=Search(s), indexes=["test"], sort='_score,price:desc')



回答2:


If you want to do more detailed sorting that what's available via es.search's sort keyword, you can just pass the search dict into the es.Search constructor.

s = Search({'term': {'foo.monkey': 'george'}},
           sort=[{'_geo_distance': {'unit': 'mi',
                                    'order': 'desc',
                                    'monkey.location': '81,20'}}]) 
conn.search(s)


来源:https://stackoverflow.com/questions/9084536/sorting-by-multiple-params-in-pyes-and-elasticsearch

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