# Nested Query Filter

**URL:** https://discuss.elastic.co/t/nested-query-filter/276160
**Category:** Elasticsearch
**Created:** [June 16, 2021, 3:37pm UTC](https://discuss.elastic.co/t/nested-query-filter/276160 "2021-06-16T15:37:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SAMBUDDHA\_CHAUDHURI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sambuddha_chaudhuri/32/89162_2.png) [@SAMBUDDHA\_CHAUDHURI](https://discuss.elastic.co/u/SAMBUDDHA_CHAUDHURI)
#### Post date: [June 16, 2021, 3:37pm UTC](https://discuss.elastic.co/t/nested-query-filter/276160/1 "2021-06-16T15:37:23Z")

</div>

Currently I am trying to search/filter a nested Document in Elastic Search Spring Data.

The Current Document Structure is:

````json
{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": [
      {
        "id": 1,
        "status": true,
        "issue": "Variation Issue"
      },
      {
        "id": 32,
        "status": false,
        "issue": "NoiseIssue"
      }
    ]
  }
}

Now we need to filter out the policy_data which has Noise Issue and If there is no Policy Data which has Noise Issue the policy_data will be null inside the parent document.

I have tried to use this Query
```json
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "customername": "Cust@345"
          }
        },
        {
          "nested": {
            "path": "policiesDetails.policy_data",
            "query": {
              "bool": {
                "must": {
                  "terms": {
                    "policiesDetails.policy_data.issue": [
                      "Noise Issue"
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

````

This works Fine to filter nested Document. But If the Nested Document does not has the match it removes the entire document from the view.

What i want is if nested filter does not match:-

```json
{
  "id": 1,
  "customername": "Cust@123",
  "policydetails": {
    "address": {
      "city": "Irvine",
      "state": "CA",
      "address2": "23994384, Out OF World",
      "post_code": "92617"
    },
    "policy_data": null
  }

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 14, 2021, 3:38pm UTC](https://discuss.elastic.co/t/nested-query-filter/276160/2 "2021-07-14T15:38:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
