Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid nested bool query with more "query", but works with "filter" #142

Closed
piotrkochan opened this issue Jul 6, 2017 · 6 comments · Fixed by #163 · May be fixed by #144
Closed

Invalid nested bool query with more "query", but works with "filter" #142

piotrkochan opened this issue Jul 6, 2017 · 6 comments · Fixed by #163 · May be fixed by #144

Comments

@piotrkochan
Copy link

piotrkochan commented Jul 6, 2017

Example code:

const bodybuilder = require('bodybuilder');
let body = bodybuilder();

body.query('bool', b => b
      .filter('term', 'field1', x)
      .filter('term', 'field2', x)
      .orFilter('term', 'field3', x));
body.query('bool', b => b
      .filter('term', 'field1', x)
      .filter('term', 'field2', x)
      .orFilter('term', 'field3', x));

Makes perfect, valid query:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "filter": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "field1": 1
                    }
                  },
                  {
                    "term": {
                      "field2": 2
                    }
                  }
                ],
                "should": [
                  {
                    "term": {
                      "field3": 3
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "bool": {
            "filter": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "field4": 10
                    }
                  },
                  {
                    "term": {
                      "field5": 20
                    }
                  }
                ],
                "should": [
                  {
                    "term": {
                      "field6": 30
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Change filters to queries:

const bodybuilder = require('bodybuilder');
let body = bodybuilder();

body.query('bool', b => b
      .query('term', 'field1', x)
      .query('term', 'field2', x)
      .orQuery('term', 'field3', x));
body.query('bool', b => b
      .query('term', 'field1', x)
      .query('term', 'field2', x)
      .orQuery('term', 'field3', x));

And built query is invalid:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[bool] query does not support [query]",
                "line": 7,
                "col": 22
            }
        ],
        "type": "parsing_exception",
        "reason": "[bool] query does not support [query]",
        "line": 7,
        "col": 22
    },
    "status": 400
}
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "field1": 1
                    }
                  },
                  {
                    "term": {
                      "field2": 2
                    }
                  },
                  {
                    "term": {
                      "field3": 3
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "bool": {
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "field4": 10
                    }
                  },
                  {
                    "term": {
                      "field5": 20
                    }
                  },
                  {
                    "term": {
                      "field6": 30
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
@johannes-scharlach
Copy link
Collaborator

johannes-scharlach commented Jul 9, 2017

I've had a go at this in #144, but the current solution would be a breaking change to people using nested, has_parent or has_child with elasticsearch version 1.x.

If you need this change quickly, you can fork my branch and build it for yourself or help with making the change backwards-compatible :) (All tests would need to pass)

@i-like-robots
Copy link

I've also just stumbled into this problem too, unfortunately =[

@piotrkochan
Copy link
Author

I've already moved to https://github.com/sudo-suhas/elastic-builder

@johannes-scharlach
Copy link
Collaborator

johannes-scharlach commented Aug 3, 2017

You can always specify the raw query syntax if something isn't supported. That's not optimal, but works all the time. Example:

bodybuilder()
  .query("bool", {
    must: [
      {
        term: {
          field1: 1
        }
      },
      {
        term: {
          field2: 2
        }
      }
    ],
    should: [
      {
        term: {
          field3: 3
        }
      }
    ]
  })
  .query("bool", {
    must: [
      {
        term: {
          field4: 10
        }
      },
      {
        term: {
          field5: 20
        }
      }
    ],
    should: [
      {
        term: {
          field6: 30
        }
      }
    ]
  })
  .build();

This would produce the query @piotrkochan was aiming for.

@piotrkochan
Copy link
Author

Of course I could but this make no sense. The point of use libraries like bodybuilder is to do not write raw queries. Still it is really good to build simple queries.

@johannes-scharlach
Copy link
Collaborator

Sure, it would be better if we could merge #144. If you're interested in contributing, I can help point you to the pieces of code that would need to be changed to make the tests pass.

ferronrsmith added a commit that referenced this issue Jan 29, 2018
bodybuilder now generates valid nested queries

fixes #142
fixes #162
ferronrsmith added a commit that referenced this issue Jan 29, 2018
bodybuilder now generates valid nested queries

fixes #142
fixes #162
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants