liveBook · Manning

Distance_feature query

As explained in Elasticsearch in Action, Second Edition

An Elasticsearch query that increases the relevance scores of documents whose date or geographic-point values are closer to a specified origin within a specified pivot distance.

Use a distance_feature query when matching documents should remain eligible but receive higher relevance scores as their date or geographic-point values approach an origin. A pivot distance controls the distance scale used for the boost; it does not define a filtering radius. section 12.0 section 12.9.1

Configure the query with these core choices:

Prioritize universities near London Bridge

After mapping location as geo_point, search with: GET universities/_search { "query": { "distance_feature": { "field": "location", "origin": [-0.0860, 51.5048], "pivot": "10 km" } } } The query boosts universities closer to the specified origin, allowing nearby results to appear higher in relevance order.

Prefer iPhones near a release date

Combine a name match with a date preference: match iPhones containing “12”, then use a distance_feature should clause with release_date, origin 1-12-2020, and pivot 30 d. The returned iPhone 12 Mini scores above iPhone 12 because its release date is closer to the origin.

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains supported fields, origin, pivot, mappings, and date and geographic examples, but does not detail the scoring formula.
Examples
Uses universities near London Bridge, classic literature from 1813, and iPhones released near 1-12-2020.
What it assumes
Assumes basic Elasticsearch mappings and searches while demonstrating the required date, date_nanos, and geo_point field types.
What you can do after
Apply it via Elasticsearch JSON requests to rank date- or location-based matches by closeness to an origin.
How it connects
Connects geographic search and date fields with relevance scoring, geo_point mapping, and bool queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.