Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Computing return fields with scripting


ElasticSearch allows you 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 will need a working ElasticSearch cluster and an index populated with the script (chapter_06/populate_aggregations.sh) used in Chapter 6, Aggregations, which is available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

In order to compute return fields with scripting, perform the following steps:

  1. Return the following script fields:

    • "my_calc_field": This concatenates the text of the "name" and "description" fields

    • "my_calc_field2": This multiplies the "price" value by the "discount" parameter

  2. From the command line, execute the following code:

    curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?&pretty=true&size=3' -d '{
      "query": {
        ...