Reader small image

You're reading from  ElasticSearch Cookbook

Product typeBook
Published inDec 2013
Reading LevelBeginner
PublisherPackt
ISBN-139781782166627
Edition1st Edition
Languages
Right arrow
Author (1)
Alberto Paro
Alberto Paro
author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro

Right arrow

Chapter 7. Scripting

In this chapter, we will cover the following topics:

  • Installing additional script plugins

  • Sorting using script

  • Computing return fields with scripting

  • Filtering a search via scripting

  • Updating with scripting

Introduction


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.

Installing additional script plugins


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.

Getting ready

You need a working ElasticSearch cluster.

How to do it...

For installing a JavaScript language support for Elasticsearch, we will perform the following steps:

  1. From the command line, simply call the following command:

    bin/plugin --install elasticsearch/elasticsearch-lang-javascript/1.4.0
    
  2. 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.

  3. For installing a Python language support for Elasticsearch, just call the following command...

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:

  1. If we want to order our documents by the price field multiplied by a factor 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...

Computing return fields with scripting


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.

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 computing return fields with scripting, we will perform the following steps:

  1. 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

  2. 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\"...

Filtering a search via scripting


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.

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 filtering a search using a scripting, we will perform the following steps:

  1. 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
            ...

Updating with scripting


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.

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 updating using a scripting, we will perform the following steps:

  1. 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"
        }
    }'
  2. If everything is correct, the result returned by ElasticSearch should be:

    {
      "ok" : true,
      "_index" : "test-index",
      "_type" : "test-type...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
ElasticSearch Cookbook
Published in: Dec 2013Publisher: PacktISBN-13: 9781782166627
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro