Sorting data using script

ElasticSearch provides scripting support for the sorting functionality. In real world applications, there is often a need to modify the default sort by the match score using an algorithm that depends on the context and some external variables. Some common scenarios are given as follows:

  • Sorting places near a point
  • Sorting by most-read articles
  • Sorting items by custom user logic
  • Sorting items by revenue

Getting ready

You will need a working ElasticSearch cluster and an index populated with the script used in Chapter 6, Aggregations, which is available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

In order to sort using scripting, perform the following steps:

  1. If you want to order your documents ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.