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

No comments:

Post a Comment