How to perform range filtering like greater than, less than in Elasticsearch? - Big Data In Real World

How to perform range filtering like greater than, less than in Elasticsearch?

What is the difference between map and mapPartitions in Spark?
May 3, 2021
What is the difference between HiveServer1 and HiveServer2?
May 7, 2021
What is the difference between map and mapPartitions in Spark?
May 3, 2021
What is the difference between HiveServer1 and HiveServer2?
May 7, 2021

Filtering 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"
        }
      }
'

 

Big Data In Real World
Big Data In Real World
We are a group of Big Data engineers who are passionate about Big Data and related Big Data technologies. We have designed, developed, deployed and maintained Big Data applications ranging from batch to real time streaming big data platforms. We have seen a wide range of real world big data problems, implemented some innovative and complex (or simple, depending on how you look at it) solutions.

Comments are closed.

How to perform range filtering like greater than, less than in Elasticsearch?
This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.

Hadoop In Real World is now Big Data In Real World!

X