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

Nested terms and cardinality aggregation causes scoring exception #112975

Closed
wiggzz opened this issue Sep 16, 2024 · 2 comments · Fixed by #113129
Closed

Nested terms and cardinality aggregation causes scoring exception #112975

wiggzz opened this issue Sep 16, 2024 · 2 comments · Fixed by #113129
Assignees
Labels
:Analytics/Aggregations Aggregations >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)

Comments

@wiggzz
Copy link

wiggzz commented Sep 16, 2024

Elasticsearch Version

8.15

Installed Plugins

No response

Java Version

bundled

OS Version

Linux 3acef6594579 6.5.0-15-generic #15-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 9 22:39:36 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Problem Description

Under specific conditions I am hitting a runtime_exception when using some specific aggregations. Namely, a terms and cardinality aggregation inside a terms aggregation inside a nested aggregation. See the reproduction steps below.

The error is:

{
  "error": {
    "root_cause": [
      {
        "type": "runtime_exception",
        "reason": "score for different docid, nesting an aggregation under a children aggregation and terms aggregation with collect mode breadth_first isn't possible"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my_index",
        "node": "i6xLcTXyT1SrJ5LhtKyUjg",
        "reason": {
          "type": "runtime_exception",
          "reason": "score for different docid, nesting an aggregation under a children aggregation and terms aggregation with collect mode breadth_first isn't possible"
        }
      }
    ],
    "caused_by": {
      "type": "runtime_exception",
      "reason": "score for different docid, nesting an aggregation under a children aggregation and terms aggregation with collect mode breadth_first isn't possible",
      "caused_by": {
        "type": "runtime_exception",
        "reason": "score for different docid, nesting an aggregation under a children aggregation and terms aggregation with collect mode breadth_first isn't possible"
      }
    }
  },
  "status": 500
}

Steps to Reproduce

PUT http://localhost:9200/my_index
Content-Type: application/json

{
    "mappings": {
        "properties": {
            "tags": {
                "type": "nested",
                "properties": {
                    "key": {
                        "type": "keyword"
                    },
                    "value": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

### create doc
POST http://localhost:9200/my_index/_doc
Content-Type: application/json

{
    "tags": [
        {
            "key": "state",
            "value": "texas"
        }
    ]
}

### create another doc
POST http://localhost:9200/my_index/_doc
Content-Type: application/json

{
    "tags": [
        {
            "key": "state",
            "value": "utah"
        }
    ]
}

### create third doc
POST http://localhost:9200/my_index/_doc
Content-Type: application/json

{
    "tags": [
        {
            "key": "state",
            "value": "texas"
        }
    ]
}

### query (which produces an error)
POST http://localhost:9200/my_index/_search
Content-Type: application/json

{
    "aggregations": {
        "tags": {
            "nested": {
                "path": "tags"
            },
            "aggregations": {
                "keys": {
                    "terms": {
                        "field": "tags.key",
                        "execution_hint": "map"
                    },
                    "aggregations": {
                        "values": {
                            "terms": {
                                "field": "tags.value"
                            }
                        },
                        "value_count": {
                            "cardinality": {
                                "field": "tags.value"
                            }
                        }
                    }
                }
            }
        }
    },
    "size": 0
}


### delete index
DELETE http://localhost:9200/my_index

I can work around this by adding execution_hint: direct to the cardinality aggregation, but from reading the code this appears to be a bug since the MultiBucketCollector is defaulting to ScoreMode.COMPLETE even though the sub-aggregations do not need any scoring (here).

Logs (if relevant)

No response

@wiggzz wiggzz added >bug needs:triage Requires assignment of a team area label labels Sep 16, 2024
@pxsalehi pxsalehi added :Analytics/Aggregations Aggregations and removed needs:triage Requires assignment of a team area label labels Sep 17, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Sep 17, 2024
@elasticsearchmachine
Copy link
Collaborator

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

@iverase
Copy link
Contributor

iverase commented Sep 18, 2024

Thank you for reporting an provide a clear reproduction. It made it very easy to find the issue and open a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Aggregations Aggregations >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants