25

What is the best way to specify the sort order in ElasticSearch for multiple fields? The query string format does not seem to work at all:

http://elasticsearch_url/index/_search?sort=field1:asc&sort=field2:desc&size=100

One would like to sort first by field1, then by field2, but only one of the fields seems to be sorted correctly. The full notations works better, but the first entries have occasionally the wrong search order:

curl -s -XGET http://elasticsearch_url/index/_search -d '
{
    "sort": [
        { "field1": { "order": "desc" }},
        { "field2": { "order": "desc" }}
    ],
    "size": 100
}'
1
  • 2
    You can't sort multiple fields using the first query string you have used because just one field sort is taking into consideration. If you wish to apply sort on multiple fields, you'll need to use the full notation sort description Commented Apr 22, 2015 at 15:38

1 Answer 1

21

Apparently the second, full notation works better.

There was another problem that one of the fields contained urls, which was parsed in odd ways by ElasticSearch. Even normal string fields can be difficult to sort, indexing a url in ElasticSearch is even more difficult.

The sort keyword takes in an array that can target multiple fields.

curl -s -XGET http://elasticsearch_url/index/_search -d '
{
    "sort": [
        { "field1": { "order": "desc" }},
        { "field2": { "order": "desc" }}
    ],
    "size": 100
}'
Sign up to request clarification or add additional context in comments.

3 Comments

They are the code snippets identical ? What was the change. I still didnt get it
@DollarAkshay same. Have you found a solution to this, please?
I am also facing the problem to sort with multiple fields, @0x4a6f4672 have you got any resolution to above?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.