Rakmo
Rakmo

Reputation: 1982

How to query elasticsearch for greater than and less than?

I want to get values between 1000 and 2000. I tried this query:

{
    "query": {
        "bool": {
            "filter": [{
                "range": {
                    "price": {
                        "gte": 1000
                    },
                    "price": {
                        "lte": 2000
                    }
                }
            }]
        }
    }
}

But this doesn't give satisfactory results. Greater than works fine. I am using elasticsearch v6.3. Please help with solution for both inclusive and exclusive of both values.

Upvotes: 53

Views: 132152

Answers (1)

Mikhail Kholodkov
Mikhail Kholodkov

Reputation: 25136

Returns documents where the price value between 1000 and 2000 inclusive.

{
    "query": {
        "range" : {
            "price" : {
                "gte" : 1000,
                "lte" : 2000
            }
        }
    }
}

Range Query

Matches documents with fields that have terms within a certain range. The type of the Lucene query depends on the field type, for string fields, the TermRangeQuery, while for number/date fields, the query is a NumericRangeQuery.

gte - Greater-than or equal to

lte - Less-than or equal to

gt - Greater-than

lt - Less-than

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

Upvotes: 113

Related Questions