Elasticsearch provides scripting support for sorting functionality. In real-world applications, there is often a need to modify the default sort by match score using an algorithm that depends on the context and some external variables. Some common scenarios are as follows:
You need an up-and-running Elasticsearch installation as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
To execute curl
via the command-line you need to install curl
for your operating system.
To correctly execute the following commands, you need an index populated with the chapter_09/populate_for_scripting.sh
script available in the online code.
For sorting using scripting, we will perform the following steps:
price
field multiplied by a factor
parameter (that is, sales tax), the search will be as shown in the following code:curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search? pretty&size=3' -d '{ "query": { "match_all": {} }, "sort": { "_script": { "script": { "inline": "Math.sqrt(doc["price"].value * params.factor)", "params": { "factor": 1.1 } }, "type": "number", "order": "asc" } } }'
In this case, we have used a match_all
query and a sort
script. In real-world applications, the documents to be sorted must not be of a high cardinality.
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1000, "max_score" : null, "hits" : [ { "_index" : "test-index", "_type" : "test-type", "_id" : "161", "_score" : null, "_source" : ... truncated ..., "sort" : [ 0.0278578661440021 ] }, { "_index" : "test-index", "_type" : "test-type", "_id" : "634", "_score" : null, "_source" : ... truncated ..., "sort" : [ 0.08131364254827411 ] }, { "_index" : "test-index", "_type" : "test-type", "_id" : "465", "_score" : null, "_source" : ... truncated ..., "sort" : [ 0.1094966959069832 ] } ] } }
The sort
parameter, which we discussed in Chapter 5, Search, can be extended with the help of scripting.
The sort
scripting allows defining several parameters, such as:
order (default "asc") ("asc" or "desc")
: This determines whether the order must be ascending or descending.type
: This defines the type to convert the value.script
: This contains the script object to be executed.Extending the sort with scripting allows the use of a broader approach in scoring your hits.
Painless and Groovy provides a lot of built-in functions (mainly taken from Java Math
class) that can be used in scripts such as the following:
Function |
Description |
|
The current time in milliseconds |
|
Returns the trigonometric sine of an angle |
|
Returns the trigonometric cosine of an angle |
|
Returns the trigonometric tangent of an angle |
|
Returns the arc sine of a value |
|
Returns the arc cosine of a value |
|
Returns the arc tangent of a value |
|
Converts an angle measured in degrees to an approximately equivalent angle measured in radians |
|
Converts an angle measured in radians to an approximately equivalent angle measured in degrees |
|
Returns Euler's number raised to the power of a value |
|
Returns the natural logarithm (base e) of a value |
|
Returns the base 10 logarithm of a value |
|
Returns the correctly rounded positive square root of a value |
|
Returns the cube root of a double value |
|
Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard |
|
Returns the smallest (closest to negative infinity) value that is greater than or equal to the argument and is equal to a mathematical integer |
|
Returns the largest (closest to positive infinity) value that is less than or equal to the argument and is equal to a mathematical integer |
|
Returns the value that is closest in value to the argument and is equal to a mathematical integer |
|
Returns the angle, theta from the conversion of rectangular coordinates (x,y_) to polar coordinates (r,_theta) |
|
Returns the value of the first argument raised to the power of the second argument |
|
Returns the closest integer to the argument |
|
Returns a random double value |
|
Returns the absolute value of a value |
|
Returns the greater of two values |
|
Returns the smaller of two values |
|
Returns the size of the unit in the last place of the argument |
|
Returns the signum function of the argument |
|
Returns the hyperbolic sine of a value |
|
Returns the hyperbolic cosine of a value |
|
Returns the hyperbolic tangent of a value |
|
Returns sqrt(x2+y2) without intermediate overflow or underflow |
|
Returns the arc cosine of a value |
|
Returns the arc tangent of a value |
If you want to retrieve records in a random order, you can use a script with a random method as shown in the following code:
curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search? &pretty&size=3' -d '{ "query": { "match_all": {} }, "sort": { "_script": { "script": { "inline": "Math.random()" }, "type": "number", "order": "asc" } } }'
In this example, for every hit, the new sort value is computed executing the scripting function Math.random()
.
3.145.93.68