# How to set multiple filter conditions in a Nested Object

**URL:** https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146
**Category:** Elasticsearch
**Created:** [July 23, 2018, 10:59am UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146 "2018-07-23T10:59:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![abeltodev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abeltodev/32/40101_2.png) [@abeltodev](https://discuss.elastic.co/u/abeltodev)
#### Post date: [July 23, 2018, 10:59am UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/1 "2018-07-23T10:59:09Z")

</div>

Hi,

I have an object with multiple nested objects. Like this:

```
{ "_source" : {
    'title': 'blabla',
    "languages":[
        {"id":2,"name":"English","slug":"English","level":"0","mandatory":"false"},

        {"id":4,"name":"German","slug":"German","level":"1","mandatory":"true"}
    ]

```

I need to get documents which have the English language at level gte 0 AND the German language at level gte 1.

I'm here but without success:

```
[{"nested":{
  "path":"languages",
   "query":{
     "bool":{
      "must":[
       
       {"bool":{"must":[{"match":{"languages.id":2}},{"range":{"languages.level":{"gte":0}}}]}},
       
       {"bool":{"must":[{"match":{"languages.id":4}},{"range":{"languages.level":{"gte":1}}}]}}
]}}}}]

```

Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 23, 2018, 11:13am UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/2 "2018-07-23T11:13:10Z")

</div>

What is the result you are getting?

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

### Author: ![abeltodev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abeltodev/32/40101_2.png) [@abeltodev](https://discuss.elastic.co/u/abeltodev)
#### Post date: [July 23, 2018, 12:52pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/3 "2018-07-23T12:52:37Z")

</div>

Thank you for your answer.

Put some documents:

```
PUT testing/_doc/1
{
  "title": "my first document",

  "languages": [

    {
      "id": 1,
      "name": "English",
      "level": 2
    },

    {
      "id": 2,
      "name": "German",
      "level": 1
    }   
  ]
}

PUT testing/_doc/2
{
  "title": "my second document",

  "languages": [

    {
      "id": 1,
      "name": "English",
      "level": 2
    },

    {
      "id": 3,
      "name": "Spanish",
      "level": 2
    }   
  ]
}

```

Where languages field is defined as a nested object.

Then I need documents for example with English (id 1) level gt 0 and German (id 2) level gt 1. The documents need to accomplish both conditions.

The query:

```
{
"body": {
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "languages",
            "query": {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "languages.id": 1
                          }
                        },
                        {
                          "range": {
                            "languages.level": {
                              "gte": 0
                            }
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "languages.id": 2
                          }
                        },
                        {
                          "range": {
                            "languages.level": {
                              "gte": 0
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
}

```

The result is doc 1 and doc 2. But doc 2 has not German at level gt 0.

I hope now it is more clear.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 23, 2018, 1:12pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/4 "2018-07-23T13:12:16Z")

</div>

Great! It would help even more if you share the creation of the index with the associated mapping. That way we can just copy and paste your example in Kibana to reproduce and help.

---

<div class="post-metadata">

### Author: ![abeltodev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abeltodev/32/40101_2.png) [@abeltodev](https://discuss.elastic.co/u/abeltodev)
#### Post date: [July 23, 2018, 1:23pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/5 "2018-07-23T13:23:46Z")

</div>

Sure:

```
PUT testing
{
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type": "text"
        },
        "languages": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "text"
            },
            "level": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 23, 2018, 1:45pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/6 "2018-07-23T13:45:07Z")

</div>

I think you need to define a nested query anytime you enter a bool clause.

Here we go:

```auto
DELETE testing
PUT testing
{
  "mappings": {
    "_doc": {
      "properties": {
        "title": {
          "type": "text"
        },
        "languages": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "text"
            },
            "level": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}
PUT testing/_doc/1
{
  "title": "my first document",
  "languages": [

    {
      "id": 1,
      "name": "English",
      "level": 2
    },

    {
      "id": 2,
      "name": "German",
      "level": 1
    }   
  ]
}

PUT testing/_doc/2
{
  "title": "my second document",
  "languages": [

    {
      "id": 1,
      "name": "English",
      "level": 2
    },

    {
      "id": 3,
      "name": "Spanish",
      "level": 2
    }   
  ]
}
GET testing/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "languages",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "languages.id": 1
                          }
                        },
                        {
                          "range": {
                            "languages.level": {
                              "gte": 0
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              },
              {
                "nested": {
                  "path": "languages",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "languages.id": 2
                          }
                        },
                        {
                          "range": {
                            "languages.level": {
                              "gte": 0
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

BTW you index creation is incorrect as the type is `_doc` and not `doc`.

---

<div class="post-metadata">

### Author: ![abeltodev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abeltodev/32/40101_2.png) [@abeltodev](https://discuss.elastic.co/u/abeltodev)
#### Post date: [July 23, 2018, 8:44pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/7 "2018-07-23T20:44:10Z")

</div>

oh wth!?

i've tried before to post the question but didn't work.

I've tried again and now it works. 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: [August 20, 2018, 8:44pm UTC](https://discuss.elastic.co/t/how-to-set-multiple-filter-conditions-in-a-nested-object/141146/8 "2018-08-20T20:44:14Z")

</div>

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