Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch sort order for multiple fields

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
}'
like image 854
0x4a6f4672 Avatar asked Apr 22 '15 15:04

0x4a6f4672


1 Answers

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
}'
like image 64
0x4a6f4672 Avatar answered Sep 16 '22 20:09

0x4a6f4672