14

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

2 Answers 2

24

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')
4

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.