Search code examples
elasticsearchsubqueryelasticsearch-java-api

Elastic Search : Restricting the search result in array


My index metadata :

{
   "never": {
      "aliases": {},
      "mappings": {
         "userDetails": {
            "properties": {
               "Residence_address": {
                  "type": "nested",
                  "include_in_parent": true,
                  "properties": {
                     "Address_type": {
                        "type": "string",
                        "analyzer": "standard"
                     },
                     "Pincode": {
                        "type": "string",
                        "analyzer": "standard"
                     },
                     "address": {
                        "type": "string",
                        "analyzer": "standard"
                     }
                  }
               }
            }
         }
      },
      "settings": {
         "index": {
            "creation_date": "1468850158519",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "version": {
               "created": "1060099"
            },
            "uuid": "v2njuC2-QwSau4DiwzfQ-g"
         }
      },
      "warmers": {}
   }
}

My setting :

POST never 

{
   "settings": {
        "number_of_shards" : 5,
      "analysis": {
         "analyzer": {
            "standard": {
               "tokenizer": "keyword",
               "filter" : ["lowercase","reverse"]
            }
         }
      }
   }
}

My data :

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.375,
      "hits": [
         {
            "_index": "never",
            "_type": "userDetails",
            "_id": "1",
            "_score": 0.375,
            "_source": {
               "Residence_address": [
                  {
                     "address": "Omega Residency",
                     "Address_type": "Owned",
                     "Pincode": "500004"
                  },
                  {
                     "address": "Collage of Engineering",
                     "Address_type": "Rented",
                     "Pincode": "411005"
                  }
               ]
            }
         }
      ]
   }
}

My query :

POST /never/_search?pretty
{
   "query": {
         "match": {
                "Residence_address.address": "Omega"
            }
         }
      }

My Result :

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.375,
      "hits": [
         {
            "_index": "never",
            "_type": "userDetails",
            "_id": "1",
            "_score": 0.375,
            "_source": {
               "Residence_address": [
                  {
                     "address": "Omega Residency",
                     "Address_type": "Owned",
                     "Pincode": "500004"
                  },
                  {
                     "address": "Collage of Engineering",
                     "Address_type": "Rented",
                     "Pincode": "411005"
                  }
               ]
            }
         }
      ]
   }
}

Is there any way to restrict my result to only object containing address = Omega Residency and NOT the other object having address = Collage of Engineering?


Solution

  • You can only do it with nested query and inner_hits. I see that you have include_in_parent: true and not using nested queries though. If you only want to get the matched nested objects you'd need to use inner_hits from nested queries:

    GET /never/_search?pretty
    {
      "_source": false, 
      "query": {
        "nested": {
          "path": "Residence_address",
          "query": {
            "match": {
              "Residence_address.address": "Omega Residency"
            }
          },
          "inner_hits" : {}
        }
      }
    }