Skip to content

Range Searches

Edit page

The keyword _range matches documents where the given value is between the given _from and _to fixed range (including both endpoints).

If you only use the keyword _from matches documents where the given value is greater than or equal to a fixed value.

If you only use the keyword _to matches documents where the given value is less than or equal a fixed value.

This example find all bank accounts for which their account holders are females in the ages between 20 and 30:

SEARCH /bank/
{
"_query": {
"_and": [
{ "gender": "female" },
{
"age": {
"_in": {
"_range": {
"_from": 20,
"_to": 30
}
}
}
}
]
}
}

A _range with neither _from nor _to matches every document that has the field. Ranges work on numbers, dates, times and (by their text order) strings.

A field indexed with terms only (_index: field_terms, see Indexing Mode) has no values to check a range against, so the range is answered by the field’s accuracy terms: a closed range returns the documents of the buckets it covers (which can be wider than the range by less than the finest bucket), and a range that is open at one end, or wider than the terms can cover, answers 400. Sources: src/multivalue/range.cc:362-364,412-428, src/query_dsl.cc:453, docs/mechanisms/numeric-range-terms.md.