Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Elasticsearch Essentials

You're reading from  Elasticsearch Essentials

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781784391010
Pages 240 pages
Edition 1st Edition
Languages

Table of Contents (18) Chapters

Elasticsearch Essentials
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Getting Started with Elasticsearch Understanding Document Analysis and Creating Mappings Putting Elasticsearch into Action Aggregations for Analytics Data Looks Better on Maps: Master Geo-Spatiality Document Relationships in NoSQL World Different Methods of Search and Bulk Operations Controlling Relevancy Cluster Scaling in Production Deployments Backups and Security Index

Chapter 5. Data Looks Better on Maps: Master Geo-Spatiality

The world is getting smarter day by day and searches based on locations have become an integral part of our daily life. Be it searching for shopping centers, hospitals, restaurants, or any locations, we always look out for information such as distance and other information about the area. Elasticsearch is helpful in combining geo-location data with full-text search, structured search, and also in doing analytics.

In this chapter, we will cover the following topics:

  • Introducing geo-spatial data

  • Geo-location data types

  • Working with geo-point data

  • Geo aggregations

  • Working with geo-shapes

Introducing geo-spatial data


Geo-spatial data is information of any object on the earth and is presented by numeric values called latitude-longitude (lat-lon) that are presented on geographical systems. Apart from lat-lon, a geo-spatial object also contains other information about that object such as name, size, and shape. Elasticsearch is very helpful when working with such kinds of data. It doesn't only provide powerful geo-location searches, but also has functionalities such as sorting with geo distance, creating geo clusters, scoring based on location, and working with arbitrary geo-shapes.

Elasticsearch has two data types to solely work on geo-spatial data; they are as follows:

  • geo_point: This is a combination of latitude-longitude pairs that defines a single location point

  • geo_shape: This works on latitude-longitudes, but with complex shapes such as points, multi-points, lines, circles, polygons, and multi-polygons defined by a geo-JSON data structure

Working with geo-point data


Geo-points are single location points defined by a latitude-longitude pair on the surface of the earth. Using geo-points you can do the following things:

  • Calculate the distance between two points

  • Find the document that falls in a specified rectangular area

  • Sort documents based on distance and score results based on it

  • Create clusters of geo-points using aggregations

Mapping geo-point fields

Unlike all the data types in Elasticsearch, geo-point fields can't be determined dynamically. So, you have to define the mapping in advance before indexing data. The mapping for a geo-point field can be defined in the following format:

"location": {
    "type": "geo_point"
}

A geo_point mapping indexes a single field (the location in our example) in the lat-lon format. You can optionally index .lat and .lon separately by setting the lat-lon parameter to true.

Indexing geo-point data

Elasticsearch supports the following three formats to index geo_point data with the same mapping that...

Geo-aggregations


Sometimes searches may return too many results but you might be just interested in finding out how many documents exist in a particular range of a location. A simple example can be to see how many news events related to crime occurred in an area by plotting them on a map or by generating a heatmap cluster of the events on the map, as shown in the following image:

Elasticsearch offers both metric and bucket aggregations for geo_point fields.

Geo distance aggregation

Geo distance aggregation is an extension of range aggregation. It allows you to create buckets of documents based on specified ranges. Let's see how this can be done using an example.

Python example

query = {
  "aggs": {
    "news_hotspots": {
      "geo_distance": {
        "field": "location",
        "origin": "28.61, 77.23",
     "unit": "km",
     "distance_type": "plane",
        "ranges": [
          {
            "to": 50
          },
          {
            "from": 50, "to": 200
          },
          {
 ...

Geo-shapes


Geo-shapes are completely different from geo-points. Until now we have worked with simple geo-location and rectangle searches. However, with geo-shapes, the sky is the limit. On a map, you can simply draw a line, polygon, or circle and ask Elasticsearch to populate the data according to the co-ordinates of your queries, as seen in the following image:

Let's see some of the most important geo-shapes.

Point

A point is a single geographical coordinate, such as your current location shown by your smart-phone. A point in Elasticsearch is represented as follows:

{
    "location" : {
        "type" : "point",
        "coordinates" : [28.498564, 77.0812823]
    }
}

Linestring

A linestring can be defined in two ways. If it contains two coordinates, it will be a straight line, but if it contains more than two points, it will be an arbitrary path:

{
    "location" : {
        "type" : "linestring",
        "coordinates" : [[-77.03653, 38.897676], [-77.009051, 38.889939]]
    }
}

Circles

A circle...

Summary


In this chapter, we learned about geo data concepts and covered the rich geo search functionalities offered by Elasticsearch, including creating mappings for geo-points and geo-shapes, indexing documents, geo-aggregations, and sorting data based on geo-distance. We also covered code examples for the most widely used geo-queries in both Python and Java.

In the next chapter, you will learn how document relationships can be managed in Elasticsearch using nested and parent-child relationships.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Elasticsearch Essentials
Published in: Jan 2016 Publisher: ISBN-13: 9781784391010
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.
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}