Skip to content

[Elasticsearch] Combine query clauses whenever possible to avoid deep nested BoolQuery#9069

Merged
rodrigozhou merged 1 commit intomainfrom
rodrigozhou/opt-es-qc
Jan 27, 2026
Merged

[Elasticsearch] Combine query clauses whenever possible to avoid deep nested BoolQuery#9069
rodrigozhou merged 1 commit intomainfrom
rodrigozhou/opt-es-qc

Conversation

@rodrigozhou
Copy link
Contributor

@rodrigozhou rodrigozhou commented Jan 16, 2026

What changed?

Combining query clauses in Elasticsearch.

  1. AND queries: combined multiple AND clause into a single BoolQuery. Example: a AND b AND c
  • Before:
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          {
            "bool": {
              "filter": [
                { ... }, // `b` clause
                { ... }  // `c` clause
              ]
            }
          }
        ]
      }
    }
  • After:
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          { ... }, // `b` clause
          { ... }  // `c` clause
        ]
      }
    }
  1. OR queries: combined multiple OR clause into a single BoolQuery (similar to AND above)

  2. NOT (a OR b): replace should with must_not.

  • Before:
    {
      "bool": {
        "must_not": {
          "bool": {
            "should": [
              { ... }, // `a` clause
              { ... }  // `b` clause
            ]
          }
        }
      }
    }
  • After:
    {
      "bool": {
        "must_not": [
          { ... }, // `a` clause
          { ... }  // `b` clause
        ]
      }
    }
  1. a AND !b: combine filter and must_not clauses into a single BoolQuery.
  • Before:
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          {
            "bool": {
              "must_not": [
                { ... } // `b` clause
              ]
            }
          }
        ]
      }
    }
  • After:
    {
      "bool": {
        "filter": [
          { ... } // `a` clause
        ]
        "must_not": [
          { ... } // `b` clause
        ]
      }
    }

Why?

Reduce nesting depth of bool queries.

How did you test it?

  • built
  • run locally and tested manually
  • covered by existing tests
  • added new unit test(s)
  • added new functional test(s)

Potential risks

@rodrigozhou rodrigozhou force-pushed the rodrigozhou/opt-es-qc branch from 4059169 to 4fdea6b Compare January 16, 2026 22:06
@rodrigozhou rodrigozhou marked this pull request as ready for review January 16, 2026 22:06
@rodrigozhou rodrigozhou requested review from a team as code owners January 16, 2026 22:06
@rodrigozhou rodrigozhou merged commit 2c99e41 into main Jan 27, 2026
67 checks passed
@rodrigozhou rodrigozhou deleted the rodrigozhou/opt-es-qc branch January 27, 2026 00:12
simvlad pushed a commit that referenced this pull request Jan 27, 2026
… nested BoolQuery (#9069)

## What changed?
Combining query clauses in Elasticsearch.

1. `AND` queries: combined multiple `AND` clause into a single
`BoolQuery`. Example: `a AND b AND c`
  - Before:
    ```json
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          {
            "bool": {
              "filter": [
                { ... }, // `b` clause
                { ... }  // `c` clause
              ]
            }
          }
        ]
      }
    }
    ```
  - After:
    ```json
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          { ... }, // `b` clause
          { ... }  // `c` clause
        ]
      }
    }
    ```

2. `OR` queries: combined multiple `OR` clause into a single `BoolQuery`
(similar to `AND` above)

3. `NOT (a OR b)`: replace `should` with `must_not`.
  - Before:
    ```json
    {
      "bool": {
        "must_not": {
          "bool": {
            "should": [
              { ... }, // `a` clause
              { ... }  // `b` clause
            ]
          }
        }
      }
    }
    ```
  - After:
    ```json
    {
      "bool": {
        "must_not": [
          { ... }, // `a` clause
          { ... }  // `b` clause
        ]
      }
    }
    ```

4. `a AND !b`: combine `filter` and `must_not` clauses into a single
`BoolQuery`.
  - Before:
    ```json
    {
      "bool": {
        "filter": [
          { ... }, // `a` clause
          {
            "bool": {
              "must_not": [
                { ... } // `b` clause
              ]
            }
          }
        ]
      }
    }
    ```
  - After:
    ```json
    {
      "bool": {
        "filter": [
          { ... } // `a` clause
        ]
        "must_not": [
          { ... } // `b` clause
        ]
      }
    }
    ```

## Why?
Reduce nesting depth of bool queries.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [ ] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)

## Potential risks
@awln-temporal awln-temporal mentioned this pull request Feb 11, 2026
@chaptersix chaptersix mentioned this pull request Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants