Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 740
Rakmo Avatar asked Jul 13 '18 13:07

Rakmo


People also ask

What is range query in Elasticsearch?

By default, Elasticsearch uses the date format provided in the <field> 's mapping. This value overrides that mapping format. For valid syntax, see format . If a format or date value is incomplete, the range query replaces any missing components with default values.


1 Answers

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

like image 142
Mikhail Kholodkov Avatar answered Oct 02 '22 15:10

Mikhail Kholodkov