Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using geo distance filter


When you are working with geolocations, one of the most common tasks is to filter results based on their distance from a location. This scenario covers the following common site requirements:

  • Finding the nearest restaurant in a 20 km distance

  • Finding your nearest friends in a 10 km range

The geo_distance filter is used to achieve this goal.

Getting ready

You need a working ElasticSearch cluster and an index populated with the GeoScript chapter_05/geo/populate_geo.sh, available in the code bundle for this book.

How to do it...

Search for documents in which the pin.location is 200 km away from latitude 40, longitude 70, as follows:

curl -XGET 'http://127.0.0.1:9200/test-mindex/_search?pretty' -d '{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "pin.location": {
            "lat": 40,
            "lon": 70
          },
          "distance": "200km",
          "optimize_bbox": "memory"
        }
      },
      "query": {
        "match_all...