A.1. Points and distances between them

To search for points, you first have to index them. Elasticsearch has a geo point type especially for that. You can see an example of how to use it in the code samples by looking at mapping.json.

Note

The code samples for this book, along with instructions on how to use them, can be found at https://github.com/dakrone/elasticsearch-in-action.

Each event has a location field, which is an object that includes the geolocation field as a geo_point type:

"geolocation" : { "type" : "geo_point"}

With the geo point type defined in your mapping, you can index points by giving the latitude and longitude, as you can see in populate.sh:

"geolocation": "39.748477,-104.998852"

TIP

You can also provide the latitude and longitude as properties, an array, or a geohash. This doesn’t change the way points are indexed; it’s just for your convenience, in case you have a preferred way. You can find more details at www.elastic.co/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html.

Having geo points indexed as part of your event documents (from the dataset used throughout the book) enables you to add distance criteria to your searches in the following ways:

Sort results by the distance from a given point— This makes the event closest to you appear first. Filter results by distance— This lets you display only events that are within a certain range—for example, 100 kilometers from you.

Aggregate results by distance— This allows you to create buckets of ranges. For example, you can get the number of events within 100 km from you, the number of events from 100 km to 200 km, and so on.