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(), indexes=["test"], sort='_score')

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

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

How can I do this in pyes?



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(), 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(), indexes=["test"], sort='_score,price:desc')


If you want to do more detailed sorting that what's available via'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'}}])