Book Image

Elasticsearch 8.x Cookbook - Fifth Edition

By : Alberto Paro
Book Image

Elasticsearch 8.x Cookbook - Fifth Edition

By: Alberto Paro

Overview of this book

Elasticsearch is a Lucene-based distributed search engine at the heart of the Elastic Stack that allows you to index and search unstructured content with petabytes of data. With this updated fifth edition, you'll cover comprehensive recipes relating to what's new in Elasticsearch 8.x and see how to create and run complex queries and analytics. The recipes will guide you through performing index mapping, aggregation, working with queries, and scripting using Elasticsearch. You'll focus on numerous solutions and quick techniques for performing both common and uncommon tasks such as deploying Elasticsearch nodes, using the ingest module, working with X-Pack, and creating different visualizations. As you advance, you'll learn how to manage various clusters, restore data, and install Kibana to monitor a cluster and extend it using a variety of plugins. Furthermore, you'll understand how to integrate your Java, Scala, Python, and big data applications such as Apache Spark and Pig with Elasticsearch and create efficient data applications powered by enhanced functionalities and custom plugins. By the end of this Elasticsearch cookbook, you'll have gained in-depth knowledge of implementing the Elasticsearch architecture and be able to manage, search, and store data efficiently and effectively using Elasticsearch.
Table of Contents (20 chapters)

Using the Point and Shape field types

The power of geoprocessing Elasticsearch is used to provide capabilities to a large number of applications. However, it has one limitation: it only works for world coordinates.

Using Point and Shape types, X-Pack extends the geo capabilities to every two-dimensional planar coordinate system.

Common scenarios for this use case include mapping and documenting building coordinates and checking if documents are inside a shape.

Getting ready

You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1Getting Started.

To execute the commands in this recipe, you can use any HTTP client, such as curl (, Postman (, or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.

How to do it…

We want to use Elasticsearch to map a device's coordinates in our shop. To achieve this, follow these steps:

  1. To create our index for storing devices and their location, we will use the following mapping:
    PUT test-point
    { "mappings": {
        "properties": {
          "device": { "type": "keyword" },
          "location": { "type": "point" } } } }
  2. Now, we can store some documents that contain our device's data:
    PUT test-point/_bulk

At this point, we want to create shapes in our shop so that we can divide it into parts and check if the people/devices are inside the defined shape. To do this, follow these steps:

  1. First, let's create an index to store our shapes:
    PUT test-shape
    { "mappings": {
        "properties": { 
          "room": { "type": "keyword" },
          "geometry": { "type": "shape" } } } } 
  2. Now, we can store a document to test the mapping:
    POST test-shape/_doc/1
    { "room":"hall",
      "geometry" : {
        "type" : "polygon",
        "coordinates" : [
          [ [8.0, 8.0], [8.0, 12.0], [12.0, 12.0], [12.0, 8.0], [8.0, 8.0]] ] } }
  3. Now, let's search our devices in our stored shape:
    POST test-point/_search
    { "query": {
        "shape": {
          "location": {
            "indexed_shape": { "index": "test-shape", "id": "1", "path": "geometry" } } } } }

The result for both queries will be as follows:

{  …truncated…
    "hits" : [ {
"_index" : "test-point",  "_id" : "1", "_score" : 0.0,
        "_source" : {
          "device" : "device1",
          "location" : { "x" : 10, "y" : 10 }

How it works…

The point and shape types are used to manage every type of two-dimensional planar coordinate system inside documents. Their usage is similar to geo_point and geo_shape.

The advantage of storing shapes in Elasticsearch is that you can simplify how you match constraints between coordinates and shapes. This was shown in our query example, where we loaded the shape's geometry from the test-shape index and the search from the test-point index.

Managing coordinate systems and shapes is a very large topic that requires knowledge of shape types and geo models since they are strongly bound to data models.

See also

  • The official documentation for Point types can be found at, while the official documentation for Shape types can be found at
  • The official documentation about Shape Query can be found at