rudijs.github.com

Web Development, Web Operations, Devops Blog

Docker Elasticsearch v2 Geolocation Sort and Filter

Overview

Now that Elasticsearch v2.0.0 is out lets take a quick look at how quick and easy it is to explore it’s features using Docker.

This post will demonstrate two Elasticsearch Geolocation features using the official Docker image with Linux Ubuntu 14.04:

  1. Sorting by Distance
  2. Geo Distance Filter

All the following steps are commands that you will run from a terminal.

You can type them out or copy and paste them directly.

Setup

Clone the code repository and cd into it:

Start an Elasticsearch Docker container.

This command will download the official Elasticsearch v2.0.0 image from hub.docker.com and start a container instance:

  • sudo docker run -d --name elasticsearch -v "$PWD/esdata":/usr/share/elasticsearch/data -p 9200:9200 elasticsearch:2.0.0 -Des.network.bind_host=0.0.0.0

Create

Create a new Elasticsearch index

  • curl -XPUT http://localhost:9200/geo

Create a mapping on the new index for our data

  • mapping_place.json
  • curl -XPUT localhost:9200/geo/_mapping/place --data-binary @mapping_place.json

Geo Location Data

For this demostration we’ll be using the East Coast of Australia from Cairns to Hobart.

Australian East Coast

Add some ‘places’ to our index

Click the links to the place_ files below to view the JSON query objects being sent to the server.

  • place_brisbane.json
  • curl -XPOST http://localhost:9200/geo/place/ --data-binary @place_brisbane.json
  • place_sydney.json
  • curl -XPOST http://localhost:9200/geo/place/ --data-binary @place_sydney.json
  • place_melbourne.json
  • curl -XPOST http://localhost:9200/geo/place/ --data-binary @place_melbourne.json

Search all data (no sorting or filters):

  • curl -s -XGET http://localhost:9200/geo/place/_search
  • Results: Brisbane, Sydney, Melbourne

The next searches we’ll do will be JSON objects that we POST to the server.

Click the links to the query_ files below to view the JSON query objects being sent to the server.

Search and sort by distance:

  • Search from Cairns in Far North Queensland (Top of the map): query_distance_from_cairns.json
  • curl -s -XPOST http://localhost:9200/geo/place/_search --data-binary @query_distance_from_cairns.json
  • Results: Brisbane, Sydney, Melbourne
  • Search from Hobart in Tasmania (Bottom of the map): query_distance_from_hobart.json
  • curl -s -XPOST http://localhost:9200/geo/place/_search --data-binary @query_distance_from_hobart.json
  • Results: Melbourne, Sydney, Brisbane
  • Search from Canberra, the National Capital, which is nearer to Sydney: query_distance_from_canberra.json
  • curl -s -XPOST http://localhost:9200/geo/place/_search --data-binary @query_distance_from_canberra.json
  • Results: Sydney, Melbourne, Brisbane

Search, sort and filter by distance:

  • Search from Hobart in Tasmania (Bottom of the map) and limit the distance range to 1,500km: query_distance_from_hobart_filter_1500km.json
  • curl -s -XPOST http://localhost:9200/geo/place/_search --data-binary @query_distance_from_hobart_filter_1500km.json
  • Results: Melbourne, Sydney

Summary

Deploying Elasticsearch v2.0.0 with Docker and using Elasticsearch’s Geolocation features is clean, simple and very powerful.

For complete details on the tools and options available consult the Official Documentation

I hope this post gets you up and running quickly and painlessly - ready to explore more of the power of the Elasticsearch.

Comments and feedback are very much welcomed.

If I’ve overlooked anything, if you can see room for improvement or if any errors please do let me know.

Thanks!