Geo-search in Elasticsearch allows you to perform queries based on geographical data. This is particularly useful for applications that need to handle location-based data, such as finding nearby points of interest, geofencing, and more.

Key Concepts

  1. Geo-Points: Represents a single point on the earth's surface, defined by latitude and longitude.
  2. Geo-Shapes: Represents complex shapes like polygons, lines, and circles.
  3. Geo-Queries: Queries that allow you to search and filter documents based on their geographical location.

Geo-Points

Defining Geo-Points

Geo-points can be defined in your Elasticsearch index mapping. Here’s an example of how to define a geo-point field:

PUT /my_index
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

Indexing Geo-Points

Once the mapping is set, you can index documents with geo-point data:

POST /my_index/_doc/1
{
  "name": "Central Park",
  "location": {
    "lat": 40.785091,
    "lon": -73.968285
  }
}

Geo-Queries

Geo-Distance Query

A geo-distance query allows you to find documents within a certain distance from a specific point. Here’s an example:

GET /my_index/_search
{
  "query": {
    "geo_distance": {
      "distance": "10km",
      "location": {
        "lat": 40.785091,
        "lon": -73.968285
      }
    }
  }
}

Geo-Bounding Box Query

A geo-bounding box query finds documents within a specified rectangle. Here’s an example:

GET /my_index/_search
{
  "query": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lat": 40.915255,
          "lon": -74.25909
        },
        "bottom_right": {
          "lat": 40.496044,
          "lon": -73.700272
        }
      }
    }
  }
}

Geo-Polygon Query

A geo-polygon query finds documents within a specified polygon. Here’s an example:

GET /my_index/_search
{
  "query": {
    "geo_polygon": {
      "location": {
        "points": [
          { "lat": 40.73, "lon": -74.1 },
          { "lat": 40.01, "lon": -71.12 },
          { "lat": 40.70, "lon": -74.00 }
        ]
      }
    }
  }
}

Geo-Shapes

Defining Geo-Shapes

Geo-shapes can be defined in your Elasticsearch index mapping. Here’s an example of how to define a geo-shape field:

PUT /my_index
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

Indexing Geo-Shapes

Once the mapping is set, you can index documents with geo-shape data:

POST /my_index/_doc/1
{
  "name": "Central Park",
  "location": {
    "type": "polygon",
    "coordinates": [
      [
        [-73.981, 40.768],
        [-73.958, 40.768],
        [-73.958, 40.785],
        [-73.981, 40.785],
        [-73.981, 40.768]
      ]
    ]
  }
}

Practical Exercises

Exercise 1: Indexing and Querying Geo-Points

  1. Define a geo-point field in your index mapping.
  2. Index a few documents with geo-point data.
  3. Perform a geo-distance query to find documents within a 5km radius of a specific point.

Solution

  1. Define a geo-point field:

    PUT /places
    {
      "mappings": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
    
  2. Index documents:

    POST /places/_doc/1
    {
      "name": "Place A",
      "location": {
        "lat": 40.730610,
        "lon": -73.935242
      }
    }
    
    POST /places/_doc/2
    {
      "name": "Place B",
      "location": {
        "lat": 40.650002,
        "lon": -73.949997
      }
    }
    
  3. Perform a geo-distance query:

    GET /places/_search
    {
      "query": {
        "geo_distance": {
          "distance": "5km",
          "location": {
            "lat": 40.730610,
            "lon": -73.935242
          }
        }
      }
    }
    

Exercise 2: Indexing and Querying Geo-Shapes

  1. Define a geo-shape field in your index mapping.
  2. Index a document with a polygon geo-shape.
  3. Perform a geo-shape query to find documents within a specified polygon.

Solution

  1. Define a geo-shape field:

    PUT /regions
    {
      "mappings": {
        "properties": {
          "location": {
            "type": "geo_shape"
          }
        }
      }
    }
    
  2. Index a document:

    POST /regions/_doc/1
    {
      "name": "Region A",
      "location": {
        "type": "polygon",
        "coordinates": [
          [
            [-73.981, 40.768],
            [-73.958, 40.768],
            [-73.958, 40.785],
            [-73.981, 40.785],
            [-73.981, 40.768]
          ]
        ]
      }
    }
    
  3. Perform a geo-shape query:

    GET /regions/_search
    {
      "query": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "polygon",
              "coordinates": [
                [
                  [-73.981, 40.768],
                  [-73.958, 40.768],
                  [-73.958, 40.785],
                  [-73.981, 40.785],
                  [-73.981, 40.768]
                ]
              ]
            },
            "relation": "within"
          }
        }
      }
    }
    

Conclusion

In this section, you learned about geo-search capabilities in Elasticsearch, including how to define and index geo-points and geo-shapes, and how to perform various geo-queries. These skills are essential for building applications that require location-based search functionality. In the next module, we will explore custom plugins and how to extend Elasticsearch's capabilities further.

© Copyright 2024. All rights reserved