# 'And' effect search for multiple nested object

**URL:** https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148
**Category:** Elasticsearch
**Created:** [December 11, 2017, 5:01pm UTC](https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148 "2017-12-11T17:01:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![johntadd](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@johntadd](https://discuss.elastic.co/u/johntadd)
#### Post date: [December 11, 2017, 5:01pm UTC](https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148/1 "2017-12-11T17:01:28Z")

</div>

I have a complicated mapping where multiple levels of nested objects contain information e.g. level3\_attr1 and level4\_attr1 I want to search.

```
mappings : {
    mytype: {
        properties: {
            level1tag: {
                properties: {
                    level2tag: {
                        type: "nested", 
                        properties: {
                            ...
                            level3tag: {
                                type: "nested",
                                properties: {
                                    ...
                                    level3_attr1: {
                                        type: "text", 
                                        fields: { keyword: { type: "keyword", ignore_above: 256 } }
                                    } 
                                    level4tag: {
                                        type: "nested",
                                        properties: {
                                            ...
                                            level4_attr1: {
                                                type: "text", 
                                                fields: { keyword: { type: "keyword", ignore_above: 256 } }
                                            } 
                                        }
                                    }// end level4tag
                                    ...
                                }
                            }// level3tag
                        } 
                    }// level2tag    
                }
            }// level1tag 
        }
    }
}

```

Now I want to achieve the effect like SQL's `... where level3_attr1 = 'value A' and level4_attr1 = 'value B' ...` through search API. But specifying query as below

```
{ 
    "query": { 
        "bool" : { 
            "must": [ 
                { 
                    "nested": { 
                        "path": "level1tag.leve2tag.level3tag.level4tag", 
                        "query": { 
                            "bool": { 
                                "must": [ 
                                    { 
                                        "term": { 
                                            "level1tag.level2tag.level3tag.level4tag.level4_attr1.keyword": "value A" 
                                        }
                                    } 
                                ] 
                            } 
                        } 
                    } 
                }, 
                { 
                    "nested": { 
                        "path": "level1.level2.level3", 
                        "query": { 
                            "bool": { 
                                "must": [ 
                                    { 
                                        "term" : {
                                            "level1tag.level2tag.level3tag.level3_attr1.keyword": "value B" 
                                          } 
                                    } 
                                ] 
                            } 
                        } 
                    }  
                } 
            ] 
        } 
    }
}

```

returns the result with effect like **or** (i.e. level3\_attr1 = 'value A' or level4\_attr1 = 'value B') where multiple docs having one of the attributes match returned.

So my question is how can I achieve the effect like and when using search API in a multiple nested object mapping index?

---

<div class="post-metadata">

### Author: ![johntadd](https://avatars.discourse-cdn.com/v4/letter/j/ecae2f/32.png) [@johntadd](https://discuss.elastic.co/u/johntadd)
#### Post date: [December 12, 2017, 10:57am UTC](https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148/2 "2017-12-12T10:57:50Z")

</div>

Come across the the link - [Querying a multiple level nested object](https://discuss.elastic.co/t/querying-a-multiple-level-nested-object/56889). That basically helps solve my problem.

Instead of separated paths on different nested object query,

```
{
    "query": {
        "bool": {
            "must": [
                { ... nested query on path1 }
                { ... nested query on path2 }
            ]
        }
    }
}

```

query should be specified with base path then down to the deeper level. So each level can specify its own condition.

```
"query": {
    "nested": {
        "path": "level1tag.level2tag.level3tag",
        "query": {
            "bool": {
                "must": [
                    {
                        "term": { "level1tag.level2tag.level3tag.level3_attr1": "value A" }
                    },
                    {
                        "nested": {
                            "path": "level1tag.level2tag.level3tag.level4tag",
                            "query": {
                                "bool": {
                                     "must" : [
                                         { "term": { "level1tag.level2tag.level3tag.level4tag.level4_attr1" : "value B"} }
                                      ]
                                }
                            } 
                        }    
                    }
                ]
            }
        }
    }
}
```

---

<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: [January 9, 2018, 10:57am UTC](https://discuss.elastic.co/t/and-effect-search-for-multiple-nested-object/111148/3 "2018-01-09T10:57:53Z")

</div>

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