Reader small image

You're reading from  Elasticsearch 8.x Cookbook - Fifth Edition

Product typeBook
Published inMay 2022
PublisherPackt
ISBN-139781801079815
Edition5th Edition
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

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 (https://curl.haxx.se/), Postman (https://www.getpostman.com/), 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
    {"index":{"_index":"test-point","_id":"1"}}
    {"device":"device1","location":{"x":10,"y":10}}
    {"index":{"_index":"test-point","_id":"2"}}
    {"device":"device2","location":{"x":10,"y":15}}
    {"index":{"_index":"test-point","_id":"3"}}
    {"device":"device3","location":{"x":15,"y":10}}

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 }
    …truncated…

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 https://www.elastic.co/guide/en/elasticsearch/reference/current/point.html, while the official documentation for Shape types can be found at https://www.elastic.co/guide/en/elasticsearch/reference/current/shape.html.
  • The official documentation about Shape Query can be found at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-shape-query.html.
Previous PageNext Page
You have been reading a chapter from
Elasticsearch 8.x Cookbook - Fifth Edition
Published in: May 2022Publisher: PacktISBN-13: 9781801079815
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