Thursday, 5 November 2015

Elasticsearch: Range filter

Range filter is used to perform operations like greater than(>), less than (<), greater than or equal to (>=) and less than or equal to (<=).
Example
"range" : {
         "id" : {
        "gte" : 1,
        "lt"  : 400
    }
}

Above snippet return all documents where id’s >=1 and <400.

Range filter accepts below parameters.
Parameter
Meaning
gte
Greater than or equal to
gt
Greater than
lte
Less than or equal to
lt
Less than
  

Get all employees whose ids are between 10 and 20.

GET /organization/employee/_search
{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "id": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}


Above query return following response.

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "organization",
            "_type": "employee",
            "_id": "5",
            "_score": 1,
            "_source": {
               "id": 12,
               "firstName": "Sailaja",
               "lastName": "Navakotla",
               "designation": "Software Engineer",
               "mailId": "wxyasdf@wxyz.com",
               "hobbies": [
                  "climbing hills",
                  "shopping",
                  "travelling"
               ],
               "address": {
                  "street": "TNagar",
                  "city": "Chennai",
                  "state": "Tamilnadu",
                  "country": "India",
                  "PIN": "5609126"
               }
            }
         }
      ]
   }
}




Prevoius                                                 Next                                                 Home

Wednesday, 4 November 2015

Elasticsearch: terms filter

 “terms” filter is just like “term” filter, but is used to search for multiple values.

1.Get all the documents where employee id's are 358, 75 and 12
One way to solve this by using bool and term filters.

GET /organization/employee/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool" :{
          "should": [
            {"term" : {"id" : "358"}},
            {"term" : {"id" : "75"}},
            {"term" : {"id" : "12"}}
          ]
        }
      }
    }
  }
}
We can rewrite above query using terms filter.

GET /organization/employee/_search
{
  "query": {
    "filtered": {
      "filter": {
       "terms" : {"id" : [358, 75, 12]}
      }
    }
  }
}




Prevoius                                                 Next                                                 Home

Tuesday, 3 November 2015

Elasticsearch: bool filter

"bool" filter is used to combine other filters. “bool” filter is composed of three sections.
{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}

“must” clause is equivalent of &&(Logical AND) operator in java.
“should” clause is equivalent of ||(Logical OR) operator in java
“must_not” clause is equivalent of !(Logical NOT) operator in java

 “must”, “should”, “must_not” sections are optional, you can use any one/two/three sections. Each section “must”, “should”, “must_not” can contain more than one filter.

1.Get documents where employee hobbies include “movies” and his id must not 765 and he is not from Karnataka.
GET /organization/employee/_search
{
  "query":{
    "filtered": {
      "filter": {
        "bool": {
          "must" : {
            "term" : {"hobbies" : "movies"}
          },
          "must_not": [
            {"term" : {"id" : "765"}},
            {"term" : {"address.state" : "Karnataka"}}
          ]
        }
      }
    }
  }
}
Above query return following result.

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "organization",
            "_type": "employee",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 358,
               "firstName": "Hari Krishna",
               "lastName": "Gurram",
               "designation": "Senior Software Engineer",
               "mailId": "abcdefabcdef@abcdef.com",
               "hobbies": [
                  "watching movies",
                  "Writing blogs",
                  "Reading philosophy"
               ],
               "address": {
                  "street": "Panchayat Office",
                  "city": "Ongole",
                  "state": "Andhra Pradesh",
                  "country": "India",
                  "PIN": "523169"
               }
            }
         },
         {
            "_index": "organization",
            "_type": "employee",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": 12345,
               "firstName": "Joel Babu",
               "lastName": "Chelli",
               "designation": "Software Engineer",
               "mailId": "wxyzwxyz@wxyz.com",
               "hobbies": [
                  "Playing games",
                  "shopping",
                  "watch movies"
               ],
               "address": {
                  "street": "Marthalli",
                  "city": "Bangalore",
                  "state": "Karnataka",
                  "country": "India",
                  "PIN": "560047"
               }
            }
         }
      ]
   }
}






Prevoius                                                 Next                                                 Home