You're reading from ElasticSearch Cookbook
ElasticSearch has a powerful way to extend its capabilities with custom scripts that can be written in several programming languages. The most common ones are MVEL, JavaScript, and Python.
We already have a taste of the scripting capabilities of ElasticSearch, in the previous chapter, using scripting for facets. In this chapter we will see how it's possible to create custom scoring algorithms, special processed return fields, custom sorting or complex update operations on records.
The scripting concept of ElasticSearch can be seen as an advanced stored procedures system in the NoSQL world; so, for an advanced use of ElasticSearch, it is very important to master it.
ElasticSearch provides native scripting (a Java code compiled in JAR) and MVEL, but a lot of interesting languages are available, such as JavaScript and Python. These languages must be installed as plugins.
For installing a JavaScript language support for Elasticsearch, we will perform the following steps:
From the command line, simply call the following command:
bin/plugin --install elasticsearch/elasticsearch-lang-javascript/1.4.0
It will fire as results:
-> Installing elasticsearch/elasticsearch-lang-javascript/1.4.0... Trying http://download.elasticsearch.org/elasticsearch/elasticsearch-lang-javascript/elasticsearch-lang-javascript-1.4.0.zip... Downloading ....DONE Installed lang-javascript
If the installation is successful, the output will end with Installed; otherwise, an error is returned.
For installing a Python language support for Elasticsearch, just call the following command...
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
You need a working ElasticSearch cluster and an index populated with the script used for facet processing, available in the online code.
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" : { "script" : "doc[\"price\"].value * factor", "type...
ElasticSearch allows us to define complex expressions that can be used to return a new calculated field value.
These special fields are called script_fields
, and they can be expressed with a script in every available ElasticSearch scripting language.
You need a working ElasticSearch cluster and an index populated with the script used for facet processing, available in the online code.
For computing return fields with scripting, we will perform the following steps:
Return the following script fields:
"my_calc_field"
: This concatenates the texts of the"name"
and"description"
fields"my_calc_field2"
: This multiplies the"price"
value by the"discount"
parameter
From the command-line, we will execute the following code:
curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?&pretty=true&size=3' -d '{ "query": { "match_all": {} }, "script_fields" : { "my_calc_field" : { "script" : "doc[\"name\"...
In Chapter 5, Search, Queries, and Filters, we have seen many filters. ElasticSearch scripting allows extending the traditional filter with custom script.
Using scripting to create a custom filter is a convenient way to write scripting rules not provided by Lucene or ElasticSearch and to create business rules not available in query DSL.
You need a working ElasticSearch cluster and an index, populated with the script used for facet processing, available in the online code.
For filtering a search using a scripting, we will perform the following steps:
We'll write a search with a filter that filters out a document with an age value less than a parameter value:
curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?&pretty=true&size=3' -d '{ "query": { "filtered": { "filter": { "script": { "script": "doc[\"age\"].value > param1", "params" : { "param1" : 80 ...
ElasticSearch allows updating a document in-place.
Updating a document via scripting reduces networking traffic (otherwise, you need to fetch the document, change the field, and send it back) and allows improving performance when you need to process a huge amount of documents.
You need a working ElasticSearch cluster and an index populated with the script used for facet processing, available in the online code.
For updating using a scripting, we will perform the following steps:
We'll write an update action that adds a tag value to a list of tags available in the source of a document. It should look as shown in the following code:
curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/9/_update?&pretty=true' -d '{ "script" : "ctx._source.tag += tag", "params" : { "tag" : "cool" } }'
If everything is correct, the result returned by ElasticSearch should be:
{ "ok" : true, "_index" : "test-index", "_type" : "test-type...