AnalyticsBuilder
AnalyticsBuilder

Reputation: 4261

Sorting by multiple params in pyes and elasticsearch

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

Upvotes: 14

Views: 13442

Answers (2)

justinvf
justinvf

Reputation: 3129

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)

Upvotes: 4

AnalyticsBuilder
AnalyticsBuilder

Reputation: 4261

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

Upvotes: 24

Related Questions