liveBook · Manning

Geo_shape data type

As explained in Elasticsearch in Action, Second Edition

The Elasticsearch geo_shape field type indexes geographic shapes such as points, lines, and polygons.

Use geo_shape when a location is more than one coordinate: a cafe can store a point, while a service area, route, or boundary can use lines and polygons. Elasticsearch indexes these shapes for focused geospatial search. section 12.1 section 12.2

A geo_shape field supports several GeoJSON and WKT representations:

  • Point and MultiPoint represent one location or an array of locations. section 12.2.2
  • LineString and MultiLineString represent lines or collections of lines. section 12.2.2
  • Polygon and MultiPolygon represent multi-edge areas or collections of polygons. section 12.2.2

Mapping and indexing a cafe location

Create the field as geo_shape, then provide a GeoJSON point or WKT value: PUT cafes { "mappings": { "properties": { "name": { "type": "text" }, "address": { "type": "geo_shape" } } } } PUT cafes/_doc/1 { "name": "Costa Coffee", "address": { "type": "Point", "coordinates": [0.17, 51.57] } } PUT cafes/_doc/2 { "address": "POINT (0.17 51.57)" } GeoJSON requires a type and coordinates. GeoJSON and WKT place coordinates in longitude-latitude order; the string format uses latitude followed by longitude. section 12.2.2 section 12.2.2

Listing 12.4 Creating a mapping with the geo_shape field type
PUT cafes
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "address": {
        "type": "geo_shape"    #1
      }
    }
  }
}

Inspect the complete cafe mapping that declares address as a geo_shape field. section 12.2.2

Listing 12.5 Inputting a geo_shape using WKT and GeoJSON formats
PUT cafes/_doc/1                      #1
{
  "name":"Costa Coffee",
  "address" : {
    "type" : "Point",                 #2
    "coordinates" : [0.17, 51.57]     #3
  }
}
 
PUT /cafes/_doc/2                     #4
{
  "address" : "POINT (0.17 51.57)"    #5
}

Compare runnable GeoJSON and WKT documents for indexing the same point-shaped location. section 12.2.2

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains mappings, supported shapes, and GeoJSON or WKT input, then defers detailed indexing mechanics and subsequent search operations.
Examples
Uses a cafes index, a Costa Coffee document, point coordinates, and polygonal envelopes containing addresses.
What you can do after
Operate/configure geo_shape mappings and index geographic shapes using GeoJSON or WKT.
How it connects
Connects geo_shape with geo_point, GeoJSON, WKT, geo_shape queries, and geospatial search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.