WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Geo Distance Aggregationedit
The geo_distance
agg is useful for searches such as
to "find all pizza restaurants within 1km of me." The search results
should, indeed, be limited to the 1km radius specified by the user, but we can
add “another result found within 2km”:
GET /attractions/restaurant/_search { "query": { "bool": { "must": { "match": { "name": "pizza" } }, "filter": { "geo_bounding_box": { "location": { "top_left": { "lat": 40.8, "lon": -74.1 }, "bottom_right": { "lat": 40.4, "lon": -73.7 } } } } } }, "aggs": { "per_ring": { "geo_distance": { "field": "location", "unit": "km", "origin": { "lat": 40.712, "lon": -73.988 }, "ranges": [ { "from": 0, "to": 1 }, { "from": 1, "to": 2 } ] } } }, "post_filter": { "geo_distance": { "distance": "1km", "location": { "lat": 40.712, "lon": -73.988 } } } }
The main query looks for restaurants with |
|
The bounding box filters these results down to just those in the greater New York area. |
|
The |
|
Finally, the |
The response from the preceding request is as follows:
"hits": { "total": 1, "max_score": 0.15342641, "hits": [ { "_index": "attractions", "_type": "restaurant", "_id": "3", "_score": 0.15342641, "_source": { "name": "Mini Munchies Pizza", "location": [ -73.983, 40.719 ] } } ] }, "aggregations": { "per_ring": { "buckets": [ { "key": "*-1.0", "from": 0, "to": 1, "doc_count": 1 }, { "key": "1.0-2.0", "from": 1, "to": 2, "doc_count": 1 } ] } }
The |
|
The aggregation includes the search result plus the other pizza restaurant within 2km of the user. |
In this example, we have counted the number of restaurants that fall
into each concentric ring. Of course, we could nest sub-aggregations under
the per_rings
aggregation to calculate the average price per ring, the
maximum popularity, and more.
- Elasticsearch - The Definitive Guide:
- Foreword
- Preface
- Getting Started
- You Know, for Search…
- Installing and Running Elasticsearch
- Talking to Elasticsearch
- Document Oriented
- Finding Your Feet
- Indexing Employee Documents
- Retrieving a Document
- Search Lite
- Search with Query DSL
- More-Complicated Searches
- Full-Text Search
- Phrase Search
- Highlighting Our Searches
- Analytics
- Tutorial Conclusion
- Distributed Nature
- Next Steps
- Life Inside a Cluster
- Data In, Data Out
- What Is a Document?
- Document Metadata
- Indexing a Document
- Retrieving a Document
- Checking Whether a Document Exists
- Updating a Whole Document
- Creating a New Document
- Deleting a Document
- Dealing with Conflicts
- Optimistic Concurrency Control
- Partial Updates to Documents
- Retrieving Multiple Documents
- Cheaper in Bulk
- Distributed Document Store
- Searching—The Basic Tools
- Mapping and Analysis
- Full-Body Search
- Sorting and Relevance
- Distributed Search Execution
- Index Management
- Inside a Shard
- You Know, for Search…
- Search in Depth
- Structured Search
- Full-Text Search
- Multifield Search
- Proximity Matching
- Partial Matching
- Controlling Relevance
- Theory Behind Relevance Scoring
- Lucene’s Practical Scoring Function
- Query-Time Boosting
- Manipulating Relevance with Query Structure
- Not Quite Not
- Ignoring TF/IDF
- function_score Query
- Boosting by Popularity
- Boosting Filtered Subsets
- Random Scoring
- The Closer, The Better
- Understanding the price Clause
- Scoring with Scripts
- Pluggable Similarity Algorithms
- Changing Similarities
- Relevance Tuning Is the Last 10%
- Dealing with Human Language
- Aggregations
- Geolocation
- Modeling Your Data
- Administration, Monitoring, and Deployment