Multiple nested queries with the same path

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

anyone, please?

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:

{
  "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?

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?

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.

There is a suggestion in this topic on how you could rewrite your query logic. Maybe that helps?

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

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