# Filter to exclude hits in a deeply nested document

**URL:** <https://discuss.elastic.co/t/filter-to-exclude-hits-in-a-deeply-nested-document/2201>\
**Category:** Elasticsearch\
**Created:** [June 9, 2015, 10:36am UTC](https://discuss.elastic.co/t/filter-to-exclude-hits-in-a-deeply-nested-document/2201 "2015-06-09T10:36:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nishant](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nishant](https://discuss.elastic.co/u/Nishant)\
**Post date:** [June 9, 2015, 10:36am UTC](https://discuss.elastic.co/t/filter-to-exclude-hits-in-a-deeply-nested-document/2201/1 "2015-06-09T10:36:50Z")

</div>

Hi All,  
I have recently started exploring Elasticsearch and hit a roadblock. I have a document with 3 level of nesting as follows:

> {  
> "sales": {  
> "properties" :  
> {  
> "id" : {  
> "type" : "long"  
> },  
> ......  
> "outlets" : {  
> "type" : "nested",  
> "properties" : {  
> "name" : {  
> "type" : "string","index":"not\_analyzed"  
> },  
> "branchId" : {  
> "type" : "long"  
> }  
> "products" : {  
> "type" : "nested",  
> "properties" : {  
> "code" : {  
> "type" : "string","index":"not\_analyzed"  
> },  
> "inventory" : {  
> "type" : "integer"  
> },  
> "price" : {  
> "type" : "double"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }

So Sales would have records of several nested outlets, which in turn will again have nested records of various products available.

Now I would like to retrieve the list names of all outlets which do NOT sell a particular code. In a completely flat mapping I could get the same using **not** or **must\_not** filters. However for a scenario where the documents lists an outlet 'X' which sells 2 product with codes 'a' & 'b'. Now when i try the adding a nested query with a "not"/"must\_not" filter for a match with 'a', inner hits still return the outlet X.

I have used the following queries and their variations:

## Query 1

> {  
> "query":{  
> "filtered" : {  
> "filter" : {  
> "nested" : {  
> "path" : "outlets.products",  
> "filter" : {  
> "not":{  
> "term" : {"outlets.products.code" : 'a'}  
> }  
> }  
> }  
> }  
> }  
> }  
> }

## Query 2

{

```
"_source" : {
    "includes" : ["outlets.name","outlets.products.code"],
    "excludes" : []
},

"inner_hits":{
    "outlets.products" : {
        "path" : { 
            "outlets.products" : { 
                "query" : {
                  "filtered":{
                    "filter": {
                        "not":{
                            "term" : {"outlets.products.code" : 'a'}                    
                        }
                    }
                  }
                }
                
                
            }
        }
    }

}

```

}

I have tried using a parent/child based mapping but the result still remains the same.

Please help me understand where I am going wrong and what should be my approach.

Thanks 😄

---

<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 6, 2017, 12:08am UTC](https://discuss.elastic.co/t/filter-to-exclude-hits-in-a-deeply-nested-document/2201/2 "2017-07-06T00:08:52Z")

</div>


