Multiple nested queries to the same path?












1















Let's say I have a nested structure like A -> B -> C
I'm trying to create a kind of query builder which would allow a query like



(A.a = 1 && A.B.b =2) || (A.B.b = 4 && A.B.C.c = 1)


or even more complex. So to do it I'm using multiple nested queries related with the same path



{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"a": {
"value": "1"
}
}
},
{
"nested": {
"path": "B",
"query": {
"term": {
"B.b": {
"value": "2"
}
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "B",
"query": {
"term": {
"B.b": {
"value": "4"
}
}
}
}
},
{
"nested": {
"path": "B.C",
"query": {
"term": {
"B.C.c": {
"value": "1"
}
}
}
}
}
]
}
}
]
}
}
}


but the results are not what I would expect them to be. I know I can move the B.C.c condition to be an inner nested query but it's not the case. Maybe to simplify a bit I would expect a query like below:



{
"query": {
"bool": {
"must": [
{
"nested"....
},
{
"nested" to the same path as index 0
}
]
}
}
}


to be A && B but it seems it's not. For a query I'm struggling with it seems like the second nested condition is omitted.



The queries I need to build may have conditions from different levels grouped together, and every condition may be used multiple times in different groups. What is the correct approach for such a scenario?



EDIT:
It seems that both nested queries work on separate nested documents. Is there a way to make them run on the same nested document and preserve the query structure (two nested queries, not one)? So if the first nested query has condition A.a = 1 and the second nested is A.b = 2 the result would be
A.a = 1 && A.b = 2 insted of A.a = 1 || A.b = 2










share|improve this question





























    1















    Let's say I have a nested structure like A -> B -> C
    I'm trying to create a kind of query builder which would allow a query like



    (A.a = 1 && A.B.b =2) || (A.B.b = 4 && A.B.C.c = 1)


    or even more complex. So to do it I'm using multiple nested queries related with the same path



    {
    "query": {
    "bool": {
    "should": [
    {
    "bool": {
    "must": [
    {
    "term": {
    "a": {
    "value": "1"
    }
    }
    },
    {
    "nested": {
    "path": "B",
    "query": {
    "term": {
    "B.b": {
    "value": "2"
    }
    }
    }
    }
    }
    ]
    }
    },
    {
    "bool": {
    "must": [
    {
    "nested": {
    "path": "B",
    "query": {
    "term": {
    "B.b": {
    "value": "4"
    }
    }
    }
    }
    },
    {
    "nested": {
    "path": "B.C",
    "query": {
    "term": {
    "B.C.c": {
    "value": "1"
    }
    }
    }
    }
    }
    ]
    }
    }
    ]
    }
    }
    }


    but the results are not what I would expect them to be. I know I can move the B.C.c condition to be an inner nested query but it's not the case. Maybe to simplify a bit I would expect a query like below:



    {
    "query": {
    "bool": {
    "must": [
    {
    "nested"....
    },
    {
    "nested" to the same path as index 0
    }
    ]
    }
    }
    }


    to be A && B but it seems it's not. For a query I'm struggling with it seems like the second nested condition is omitted.



    The queries I need to build may have conditions from different levels grouped together, and every condition may be used multiple times in different groups. What is the correct approach for such a scenario?



    EDIT:
    It seems that both nested queries work on separate nested documents. Is there a way to make them run on the same nested document and preserve the query structure (two nested queries, not one)? So if the first nested query has condition A.a = 1 and the second nested is A.b = 2 the result would be
    A.a = 1 && A.b = 2 insted of A.a = 1 || A.b = 2










    share|improve this question



























      1












      1








      1








      Let's say I have a nested structure like A -> B -> C
      I'm trying to create a kind of query builder which would allow a query like



      (A.a = 1 && A.B.b =2) || (A.B.b = 4 && A.B.C.c = 1)


      or even more complex. So to do it I'm using multiple nested queries related with the same path



      {
      "query": {
      "bool": {
      "should": [
      {
      "bool": {
      "must": [
      {
      "term": {
      "a": {
      "value": "1"
      }
      }
      },
      {
      "nested": {
      "path": "B",
      "query": {
      "term": {
      "B.b": {
      "value": "2"
      }
      }
      }
      }
      }
      ]
      }
      },
      {
      "bool": {
      "must": [
      {
      "nested": {
      "path": "B",
      "query": {
      "term": {
      "B.b": {
      "value": "4"
      }
      }
      }
      }
      },
      {
      "nested": {
      "path": "B.C",
      "query": {
      "term": {
      "B.C.c": {
      "value": "1"
      }
      }
      }
      }
      }
      ]
      }
      }
      ]
      }
      }
      }


      but the results are not what I would expect them to be. I know I can move the B.C.c condition to be an inner nested query but it's not the case. Maybe to simplify a bit I would expect a query like below:



      {
      "query": {
      "bool": {
      "must": [
      {
      "nested"....
      },
      {
      "nested" to the same path as index 0
      }
      ]
      }
      }
      }


      to be A && B but it seems it's not. For a query I'm struggling with it seems like the second nested condition is omitted.



      The queries I need to build may have conditions from different levels grouped together, and every condition may be used multiple times in different groups. What is the correct approach for such a scenario?



      EDIT:
      It seems that both nested queries work on separate nested documents. Is there a way to make them run on the same nested document and preserve the query structure (two nested queries, not one)? So if the first nested query has condition A.a = 1 and the second nested is A.b = 2 the result would be
      A.a = 1 && A.b = 2 insted of A.a = 1 || A.b = 2










      share|improve this question
















      Let's say I have a nested structure like A -> B -> C
      I'm trying to create a kind of query builder which would allow a query like



      (A.a = 1 && A.B.b =2) || (A.B.b = 4 && A.B.C.c = 1)


      or even more complex. So to do it I'm using multiple nested queries related with the same path



      {
      "query": {
      "bool": {
      "should": [
      {
      "bool": {
      "must": [
      {
      "term": {
      "a": {
      "value": "1"
      }
      }
      },
      {
      "nested": {
      "path": "B",
      "query": {
      "term": {
      "B.b": {
      "value": "2"
      }
      }
      }
      }
      }
      ]
      }
      },
      {
      "bool": {
      "must": [
      {
      "nested": {
      "path": "B",
      "query": {
      "term": {
      "B.b": {
      "value": "4"
      }
      }
      }
      }
      },
      {
      "nested": {
      "path": "B.C",
      "query": {
      "term": {
      "B.C.c": {
      "value": "1"
      }
      }
      }
      }
      }
      ]
      }
      }
      ]
      }
      }
      }


      but the results are not what I would expect them to be. I know I can move the B.C.c condition to be an inner nested query but it's not the case. Maybe to simplify a bit I would expect a query like below:



      {
      "query": {
      "bool": {
      "must": [
      {
      "nested"....
      },
      {
      "nested" to the same path as index 0
      }
      ]
      }
      }
      }


      to be A && B but it seems it's not. For a query I'm struggling with it seems like the second nested condition is omitted.



      The queries I need to build may have conditions from different levels grouped together, and every condition may be used multiple times in different groups. What is the correct approach for such a scenario?



      EDIT:
      It seems that both nested queries work on separate nested documents. Is there a way to make them run on the same nested document and preserve the query structure (two nested queries, not one)? So if the first nested query has condition A.a = 1 and the second nested is A.b = 2 the result would be
      A.a = 1 && A.b = 2 insted of A.a = 1 || A.b = 2







      elasticsearch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:26







      Barry Garry

















      asked Nov 19 '18 at 19:08









      Barry GarryBarry Garry

      62




      62
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381104%2fmultiple-nested-queries-to-the-same-path%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381104%2fmultiple-nested-queries-to-the-same-path%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()