# Nested query with multiple conditions

**URL:** <https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981>\
**Category:** Elasticsearch\
**Created:** [September 18, 2019, 11:00am UTC](https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981 "2019-09-18T11:00:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mudboyzh](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Mudboyzh](https://discuss.elastic.co/u/Mudboyzh)\
**Post date:** [September 18, 2019, 11:00am UTC](https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981/1 "2019-09-18T11:00:49Z")

</div>

Hi,  
I look the [document](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) and it works when a pair of match and range query.

The problem is I try to make multi pair of match and range query, it won't return any document.

Here is my index mapping

```auto
PUT master
{
  "mappings": {
    "properties": {
      "masterId":{
        "type": "keyword"
      },
      "metaData":{
        "type": "object"
      },
      "tags": {
        "type": "nested", 
        "properties": {
          "name": { 
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }  
          },
          "count": { "type": "integer" }
        }
      }
    } 
  }
}

```

Data

```auto
PUT master/_doc/docA
{
  "id": "docA",
  "metaData": null,
  "tags": [
      {
        "name": "Apple",
        "count": 4
      },
      {
        "name": "Banana",
        "count": 3
      },
      {
        "name": "Cake",
        "count": 2
      }
    ]
}

PUT master/_doc/docB
{
  "id": "docB",
  "metaData": null,
  "tags": [
      {
        "name": "Apple",
        "count": 5
      },
      {
        "name": "Banana",
        "count": 2
      },
      {
        "name": "Cake",
        "count": 4
      }
    ]
}

```

I want to find that has ( 'apple' , count \>= 4 ) AND ( 'Cake', count \>= 3)  
I expect that return docB  
My query is

```auto
(not working)
GET master/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must": [
            { "match": { "tags.name": "Apple"} },
            { "range": { "tags.count": { "gte": 4 } } },
            { "match": { "tags.name": "Cake"} },
            { "range": { "tags.count": { "gte": 3 } } }
          ]
        }
      }
    }
  }
}

```

Is there any way in one query to achieve it or just need multi query?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [September 18, 2019, 6:51pm UTC](https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981/2 "2019-09-18T18:51:50Z")

</div>

```
{
  "query": {
      "bool": {
      "must": [

   { "nested": {
  "path": "tags",
  "query": {
    "bool": {
      "must": [
        { "match": { "tags.name": "Apple"} },
        { "range": { "tags.count": { "gte": 4 } } },
      ]
    }
  }
}}, 
{ "nested": {
  "path": "tags",
  "query": {
    "bool": {
      "must": [
        { "match": { "tags.name": "Cake"} },
        { "range": { "tags.count": { "gte": 3 } } }
      ]
    }
  }
}}
]
  }
}

```

bool queries are evaluated against a single doc, so, you probably need to pull up conditions.

---

<div class="post-metadata">

**Author:** ![Mudboyzh](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Mudboyzh](https://discuss.elastic.co/u/Mudboyzh)\
**Post date:** [September 18, 2019, 11:33pm UTC](https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981/3 "2019-09-18T23:33:31Z")

</div>

I got it, thx.

---

<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:** [October 16, 2019, 11:33pm UTC](https://discuss.elastic.co/t/nested-query-with-multiple-conditions/199981/4 "2019-10-16T23:33:34Z")

</div>

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