Sorting data using scripts
Elasticsearch provides scripting support for sorting functionality. In real-world applications, there is often a need to modify the default sorting using an algorithm that is dependent on the context and some external variables. Some common scenarios are as follows:
- Sorting places near a point
- Sorting by most read articles
- Sorting items by custom user logic
- Sorting items by revenue
Because the computing of scores on a large dataset is very CPU-intensive, if you use scripting, then it’s better to execute it on a small dataset using standard score queries for detecting the top documents, and then execute a rescoring on the top subset.
Getting ready
You will need an up-and-running Elasticsearch installation, similar to the one that we described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.
To execute the commands, any HTTP client can be used, such as cURL (https://curl.haxx.se...