# Multiple nested queries with the same path

**URL:** https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532
**Category:** Elasticsearch
**Created:** [November 20, 2018, 11:57am UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532 "2018-11-20T11:57:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Barry2222](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@Barry2222](https://discuss.elastic.co/u/Barry2222)
#### Post date: [November 20, 2018, 11:57am UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/1 "2018-11-20T11:57:50Z")

</div>

It's my first post so I'd like to say Hi,

In my project I have a nested multi-level structure and I'm building a kind of D&D query builder where conditions from any level may be grouped together and any condition may be usued multiple times.  
The easiest way form me to build complex queries would be if I could repeat nested queries with the same path but this seems to not work as I expected. Here's an example:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "g",
            "query": {
              "term" : {
                "g.t1" : {
                  "query" : "term1"
                }
              }
            }
          }
        },
        {
          "nested": {
            "path": "g",
            "query": {
              "term" : {
                "g.t2" : {
                  "query" : "term2"
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

This query produces g.t1 = "term1" **||** g.t2 = "term2", but i would expect it to produce  
g.t1 = "term1" **&&** g.t2 = "term2".  
I know I can combine this case under one nested query but this is just a simple example. In my real application, conditions are much more complex and trying to combine them under single nested query may be hard. Is there a way to achieve an AND condition with the structure I presented? If no, could anyone please give me a hint like to build such a complex queries?

Thanks in advance

---

<div class="post-metadata">

### Author: ![Barry2222](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@Barry2222](https://discuss.elastic.co/u/Barry2222)
#### Post date: [November 25, 2018, 3:52pm UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/2 "2018-11-25T15:52:30Z")

</div>

anyone, please?

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [November 26, 2018, 6:02pm UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/3 "2018-11-26T18:02:00Z")

</div>

Hi Barry, welcome to our Discuss forum!

Rather than having a bool query that wraps two nested queries, what you probably want to do is have a nested query that wraps a bool query. Try this:

```auto
{
  "query": {
    "nested": {
      "path": "g",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "g.t1": {
                  "value": "term1"
                }
              }
            },
            {
              "term": {
                "g.t2": {
                  "value": "term2"
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

I know you were saying you did not want to put everything under one nested query, so if the above is not what you're looking for, can you give an example of a query that you cannot build this way?

---

<div class="post-metadata">

### Author: ![Barry2222](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@Barry2222](https://discuss.elastic.co/u/Barry2222)
#### Post date: [December 4, 2018, 3:44pm UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/4 "2018-12-04T15:44:48Z")

</div>

An easy example would be something like this :  
root condition - means a query on the root level  
nested condition - means a query on the nested level

> **(** \<root condition 1\> **OR** \<nested condition 1\> **)**  **AND** **(** \<root condition 2\> **OR** \<nested condition 2\>**)**

How to build a query like this?

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [December 4, 2018, 5:31pm UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/5 "2018-12-04T17:31:33Z")

</div>

Unfortunately what you're trying to do is not trivial. You would need something like a "reverse nested query" to do this, which [has been discussed but is not likely to be implemented in the short term](https://github.com/elastic/elasticsearch/issues/20101).

There is a suggestion in [this topic](https://discuss.elastic.co/t/query-on-fields-of-owning-document-inside-nested-query/58369) on how you could rewrite your query logic. Maybe that helps?

---

<div class="post-metadata">

### Author: ![Barry2222](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@Barry2222](https://discuss.elastic.co/u/Barry2222)
#### Post date: [December 5, 2018, 9:34am UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/6 "2018-12-05T09:34:48Z")

</div>

Unfortunately rewriting the query as in suggested topic is impossible. The example I mentioned is the simplest case, in my application queries can get much more complex so rewriting them this way would be extremely hard, if even possible.

I'll figure something out, thank you for help

---

<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 2, 2019, 9:44am UTC](https://discuss.elastic.co/t/multiple-nested-queries-with-the-same-path/157532/7 "2019-01-02T09:44:01Z")

</div>

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