user1680104
user1680104

Reputation: 8887

Elasticsearch scoring based on how close a number is to a query

I want to score my documents based on on how close a number is to a query. Given I have two documents document1.field = 1 and document2.field = 10, a query field = 3 then I want document1._score > document2._score. Or in other words I want something like a fuzzy query against number. How would I achieve this? The use case is I want to support price queries (exact or range), but want to rank stuff that isn't exactly in the boundaries.

Upvotes: 4

Views: 1656

Answers (2)

Scott Stafford
Scott Stafford

Reputation: 44798

You are looking for Decay functions:

Decay functions score a document with a function that decays depending on the distance of a numeric field value of the document from a user given origin. This is similar to a range query, but with smooth edges instead of boxes.

Upvotes: 5

imotov
imotov

Reputation: 30163

It can be implemented using custom_score query where script will determine boost depending on absolute value of the difference between exact price and desired price. The desired price should be passed to the script as a parameter to avoid script recompilation for every request.

Alternatively, it can be implemented using custom_filters_score query. Filters here will contain different ranges around desired price. Smaller ranges will have higher boost and appear higher in the list than larger ranges.

Upvotes: 1

Related Questions