# Fitering nested documents

**URL:** https://discuss.elastic.co/t/fitering-nested-documents/64779
**Category:** Elasticsearch
**Created:** [November 2, 2016, 10:37pm UTC](https://discuss.elastic.co/t/fitering-nested-documents/64779 "2016-11-02T22:37:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pmishra002](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@pmishra002](https://discuss.elastic.co/u/pmishra002)
#### Post date: [November 2, 2016, 10:37pm UTC](https://discuss.elastic.co/t/fitering-nested-documents/64779/1 "2016-11-02T22:37:05Z")

</div>

I have a set of documents with the following structure of a document. The mapping have been defined as nested for specific elemnets e.g. el2array and el3

```
{
          "el1": "val1"
          "el2array": [
            {
              "apn": "name1",
              "VN": "7.0"
            }
            {
              "apn": "name2",
              "VN": "1.7"
            }
          ],
          "el3": {
            "mk": "LG",
            "mo": "5X",
          }
}

```

where el3 and el2array are nested.

I would like to run a es query which can satisfy a query to get a set of documents which has an el2array.apn=name1 and the VN of the same apn (name1) can have VN=7.0 or 8.0 i.e get all documents with data as below.

```
        {
          "apn": "name1",
          "VN": "7.0"
        }
        
        or
        
        {
          "apn": "name1",
          "VN": "8.0"
        }

```

I tried with a query like below and it does return documents with el2array.apn=name1 and all documents with el2array.VN=7.0 or 8.0 that may or may not be associated with el2array.apn=name1. The query returns documents like below which is wrong.

```
{
          "el2array": [
            {
              "apn": "name1",
              "VN": "10.0" // note: VN is no 7.0 or 8.0
            }
            {
              "apn": "name2",
              "VN": "8.0" // another element in the array has 8.0
            }
          ]
}

```

Below is the query that I am using. Could someone suggest as to what would be the correct query / filter ?

```
{
  "query" : {
    "filtered" : {
      "query" : {
        "bool" : { }
      },
      "filter" : {
        "nested" : {
          "filter" : {
            "and" : {
              "filters" : [ 
              	
              	{"term" : {"el2array.apn" :"name1"}}, 
                {"or": [{"term" : {"el2array.VN" : "7.0"} },{"term" : {"el2array.VN" : "8.0"} }] }
              
              ]
            }
          },
          "path" : "el2array"
        }
      }
    }
  }
}
```

---

<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 5, 2017, 10:07pm UTC](https://discuss.elastic.co/t/fitering-nested-documents/64779/2 "2017-07-05T22:07:08Z")

</div>


