Sorting using script
ElasticSearch provides scripting support for sorting functionality. In real-world applications, there is often a need to modify the score using an algorithm that depends 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 customer logic
- Sorting items with more revenue
Getting ready
You need a working ElasticSearch cluster and an index populated with the script used for facet processing, available in the online code.
How to do it...
For sorting using scripting, we will perform the following steps:
- If we want to order our documents by the
price
field multiplied by afactor
parameter (usually VAT), the search will be as shown in the following code:curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?&pretty=true&size=3' -d '{ "query": { "match_all": {} }, "sort": { "_script" : { ...