What is the difference between map and mapPartitions in Spark?
May 3, 2021What is the difference between HiveServer1 and HiveServer2?
May 7, 2021Filtering based on a range like greater than, less than, greater than equal etc. are pretty common requirements when you work with data. In this post we will see how to perform range based filtering with Elasticsearch.
Solution
You can specify the range along with the “query” when executing a search.
- gt for Greater than.
- gte for Greater than or equal to.
- lt for Less than.
- lte for Less than or equal to.
Here we are trying to fetch the documents which are greater than or equal to 40 and less than or equal to 50.
We got a total of 45 hits and we have showed few hits below.
curl -X GET "localhost:9200/account/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "range": { "age": { "gte": 40, "lte": 50 } } } }'
Here is the output.
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 45, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "account", "_type" : "_doc", "_id" : "291", "_score" : 1.0, "_source" : { "account_number" : 291, "balance" : 19955, "firstname" : "Lynn", "lastname" : "Pollard", "age" : 40, "gender" : "F", "address" : "685 Pierrepont Street", "employer" : "Slambda", "email" : "lynnpollard@slambda.com", "city" : "Mappsville", "state" : "ID" } }, { "_index" : "account", "_type" : "_doc", "_id" : "474", "_score" : 1.0, "_source" : { "account_number" : 474, "balance" : 35896, "firstname" : "Obrien", "lastname" : "Walton", "age" : 40, "gender" : "F", "address" : "192 Ide Court", "employer" : "Suremax", "email" : "obrienwalton@suremax.com", "city" : "Crucible", "state" : "UT" } }, { "_index" : "account", "_type" : "_doc", "_id" : "479", "_score" : 1.0, "_source" : { "account_number" : 479, "balance" : 31865, "firstname" : "Cameron", "lastname" : "Ross", "age" : 40, "gender" : "M", "address" : "904 Bouck Court", "employer" : "Telpod", "email" : "cameronross@telpod.com", "city" : "Nord", "state" : "MO" } }, { "_index" : "account", "_type" : "_doc", "_id" : "549", "_score" : 1.0, "_source" : { "account_number" : 549, "balance" : 1932, "firstname" : "Jacqueline", "lastname" : "Maxwell", "age" : 40, "gender" : "M", "address" : "444 Schenck Place", "employer" : "Fuelworks", "email" : "jacquelinemaxwell@fuelworks.com", "city" : "Oretta", "state" : "OR" } } '