While searching for results, the most common criteria in ElasticSearch is the relevance against a text query. Thus the real world applications need to control the sorting criteria. The following are the typical scenarios:
Sorting a user by surname and name
Sorting items by stock, price (ascendant, descendent)
Sorting documents by size, file type, source, and so on
You need a working ElasticSearch cluster and an index populated with the script available in the online code.
For sorting the results, we need to perform the steps given as follows:
We need to add a
sort
section to our query as follows:curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{"query":{"match_all":{}}, "sort" : [ {"price" : {"order" : "asc", "mode" : "avg", "ignore_unmapped":true, "missing":"_last"}}, "_score" ] }'
The returned result should be similar to the following one:
…, "hits" : { "total" : 3, "max_score" : null, "hits"...