Friday 6 November 2015

Elasticsearch: missing filter

“missing” filter return documents, where there is no value for the field.

For example, I had some data in my student type.

GET /college/student/_search
Above query returns following data.

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 1,
      "hits": [
         {
            "_index": "college",
            "_type": "student",
            "_id": "4",
            "_score": 1,
            "_source": {
               "id": 75,
               "firstName": "Rama Krishna",
               "lastName": "Gurram",
               "hobbies": [
                  "climbing hills",
                  null
               ]
            }
         },
         {
            "_index": "college",
            "_type": "student",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 358,
               "firstName": "Hari Krishna",
               "lastName": "Gurram",
               "hobbies": [
                  "visiting places",
                  "watching movies",
                  "chat with friends"
               ]
            }
         },
         {
            "_index": "college",
            "_type": "student",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": 12345,
               "firstName": "Joel Babu",
               "lastName": "Chelli",
               "hobbies": [
                  null
               ]
            }
         },
         {
            "_index": "college",
            "_type": "student",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 765,
               "firstName": "PTR",
               "lastName": "sailaja"
            }
         }
      ]
   }
}

Get all documents, where hobbies is not exist.

GET /college/student/_search
{
  "query":{
    "filtered": {
      "filter": {
        "missing": {
          "field": "hobbies"
        }
      }
    }
  }
}
Above query returns following response.

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "college",
            "_type": "student",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": 12345,
               "firstName": "Joel Babu",
               "lastName": "Chelli",
               "hobbies": [
                  null
               ]
            }
         },
         {
            "_index": "college",
            "_type": "student",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 765,
               "firstName": "PTR",
               "lastName": "sailaja"
            }
         }
      ]
   }
}






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment