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.
Geohash Cell Queryedit
The geohash_cell
query simply translates a lat/lon
location into a
geohash with the specified precision and finds all locations that contain
that geohash—a very efficient query indeed.
GET /attractions/restaurant/_search { "query": { "constant_score": { "filter": { "geohash_cell": { "location": { "lat": 40.718, "lon": -73.983 }, "precision": "2km" } } } } }
This query translates the lat/lon
point into a geohash of the appropriate
length—in this example dr5rsk
—and looks for all locations that contain
that exact term.
However, the query as written in the preceding example may not return all restaurants within 5km of the specified point. Remember that a geohash is just a rectangle, and the point may fall anywhere within that rectangle. If the point happens to fall near the edge of a geohash cell, the filter may well exclude any restaurants in the adjacent cell.
To fix that, we can tell the query to include the neigboring cells, by
setting neighbors
to true
:
GET /attractions/restaurant/_search { "query": { "constant_score": { "filter": { "geohash_cell": { "location": { "lat": 40.718, "lon": -73.983 }, "neighbors": true, "precision": "2km" } } } } }
Clearly, looking for a geohash with precision 2km
plus all the neighboring
cells results in quite a large search area. This query is not built for
accuracy, but it is very efficient and can be used as a prefiltering step
before applying a more accurate geo-filter.
Specifying the precision
as a distance can be misleading. A precision
of 2km
is converted to a geohash of length 6, which actually has dimensions
of about 1.2km x 0.6km. You may find it more understandable to specify an
actual length such as 5
or 6
.
The other advantage that this query has over a geo_bounding_box
query is
that it supports multiple locations per field.
The lat_lon
option that we discussed in Optimizing Bounding Boxes is efficient,
but only when there is a single lat/lon
point per field.
- 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