Skip to content

Let terms queries rewrite to a filter on constant_keyword fields#139106

Merged
jimczi merged 8 commits intoelastic:mainfrom
jimczi:value_source_constant
Dec 10, 2025
Merged

Let terms queries rewrite to a filter on constant_keyword fields#139106
jimczi merged 8 commits intoelastic:mainfrom
jimczi:value_source_constant

Conversation

@jimczi
Copy link
Contributor

@jimczi jimczi commented Dec 5, 2025

This commit fixes the logic that lets a terms aggregation rewrite into a filters aggregation. For constant_keyword fields, every doc has the same value, so we can just rewrite the whole thing into a single match_all filter instead of handling multiple terms.

{
  "aggs": {
    "by_index": {
      "terms": {
        "field": "_index"
      }
    }
  }
}
This commit fixes the logic that lets a `terms` aggregation rewrite into a `filters` aggregation.
For `constant_keyword` fields, every doc has the same value, so we can just rewrite the whole thing into a single `match_all` filter instead of handling multiple terms.

```json
{
  "aggs": {
    "by_index": {
      "terms": {
        "field": "_index"
      }
    }
  }
}
```
@jimczi jimczi requested a review from romseygeek December 5, 2025 10:53
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Dec 5, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine
Copy link
Collaborator

Hi @jimczi, I've created a changelog YAML for you.

Copy link
Contributor

@romseygeek romseygeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jimczi jimczi added the auto-backport Automatically create backport pull requests when merged label Dec 10, 2025
@jimczi jimczi merged commit 406d08f into elastic:main Dec 10, 2025
34 checks passed
@jimczi jimczi deleted the value_source_constant branch December 10, 2025 22:51
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
9.1 Commit could not be cherrypicked due to conflicts
9.2 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 139106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/Aggregations Aggregations auto-backport Automatically create backport pull requests when merged backport pending >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.1.10 v9.2.4 v9.3.0

3 participants