I'm having trouble finding an answer on SO, elastic search docs, or google to find this answer for my use case: Find the closest number to X input that is still lower then X.
I have a mapping that looks like this:
{
"rule": {
"properties": {
"price": { "type": "long" },
"from": { "type": "long" }
}
}
}
What I need the closest matching from
, that is less then the input value.
So for example I have these rules:
{
{ "rule": {"from": 1, "price": 5} },
{ "rule": {"from": 50, "price": 4} },
{ "rule": {"from": 100, "price": 3} },
{ "rule": {"from": 150, "price": 2} }
}
If I search for the from
with the value off 75, I'd want the rule for "from": 50
.
Most of the answers I found were relating to geo/ip or text, I could not find an example that made it click for me.
Range query can be used to get all rules which are less than equal to input value. Top 1 sorted document(by from ) can be returned
Query:
{
"query": {
"range": {
"rule.from": {
"lte": 75
}
}
},
"size": 1,
"sort": [
{
"rule.from": {
"order": "desc"
}
}
]
}