Skip to content

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

@piotrkochan

Description

@piotrkochan

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
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions