Mapping a geo point field
ElasticSearch natively supports the use of geolocation types: special types that allow you to localize your document in geographic coordinates (latitude and longitude) around the world.
There are two main document types used in the geographic world: point and shape. In this recipe, we'll see geo point, the base element of geolocation.
Getting ready
You need a working ElasticSearch cluster.
How to do it...
The type of the field must be set to geo_point
in order to define a geo point.
You can extend the earlier order
example by adding a new field that stores the location of a customer. The following will be the result:
{ "order": { "properties": { "id": { "type": "string", "store": "yes", "index": "not_analyzed" }, "date": { "type": "date", "store": "no", "index": "not_analyzed" }, "customer_id": { "type": "string", "store": "yes", "index": "not_analyzed" }, "customer_ip": { "type": "ip", "store": "yes", "index": "not_analyzed" }, "customer_location": { "type": "geo_point", "store": "yes" }, "sent": { "type": "boolean", "store": "no", "index": "not_analyzed" } } } }
How it works...
When ElasticSearch indexes a document with a geo point field (latitude, longitude), it processes the latitude and longitude coordinates and creates special accessory field data to quickly query these coordinates.
Depending on the properties, given a latitude and longitude it's possible to compute the geohash value (http://en.wikipedia.org/wiki/Geohash). The index process also optimizes these values for special computation, such as distance and ranges, and in a shape match.
Geo point has special parameters that allow you to store additional geographic data:
lat_lon
(by default,false
): This allows you to store the latitude and longitude in the.lat
and.lon
fields. Storing these values improves performance in many memory algorithms used in distance and shape calculus.geohash
(by default,false
): This allows you to store the computed geohash value.geohash_precision
(by default,12
): This defines the precision to be used in a geohash calculus. For example, given a geo point value [45.61752, 9.08363], it will store:customer_location = "45.61752, 9.08363"
customer_location.lat = 45.61752
customer_location.lon = 9.08363
customer_location.geohash = "u0n7w8qmrfj"
There's more...
Geo point is a special type and can accept several formats as input:
- Latitude and longitude as properties:
"customer_location": { "lat": 45.61752, "lon": 9.08363 },
- Latitude and longitude as a string:
"customer_location": "45.61752,9.08363",
- Latitude and longitude as geohash string
- Latitude and longitude as a GeoJSON array (note that in this latitude and longitude are reversed):
"customer_location": [9.08363, 45.61752]