So far we've run our queries and got the results in the order determined by the score of each document. However, it is not enough for all the use cases. It is really handy to be able to sort our results on the basis of the field values. For example, when you are searching logs or time-based data in general, you probably want to have the most recent data first. In addition to that, Elasticsearch allows us to control how the document such be sorted not only using field values, but also using more sophisticated sorting like ones that use scripts or sorting on fields that have multiple values. We will cover all that in this section.
Let's look at the following query that returns all the books with at least one of the specified words:
curl -XGET 'localhost:9200/library/book/_search?pretty' -d '{ "query" : { "terms" : { "title" : [ "crime", "front", "punishment" ] } } }'
Under the hood, we can imagine that Elasticsearch sees the preceding query as follows...