elastic / elasticsearch

Free and Open, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
68.62k stars 24.37k forks source link

Date histogram leads to array_index_out_of_bounds_exception (tried on ES demo website) #100964

Open zitounh opened 9 months ago

zitounh commented 9 months ago

Elasticsearch Version

8.10

Installed Plugins

No response

Java Version

bundled

OS Version

Linux

Problem Description

On the ES official demo the following query leads to an error:

GET  .ds-traces-apm.rum-default-2023.09.23-000005/_search
{
    "size": 0,
    "query": {
      "match_all": {}
    },
    "_source": false,
     "aggregations": {
        "AGG0": {
            "filter": {
                "range": {
                    "@timestamp": {
                        "from": "2023-01-01",
                        "to": "2023-02-25",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1.0
                    }
                }
            },
            "aggregations": {
                "AGG0_1": {
                    "date_histogram": {
                        "field": "@timestamp",
                        "format": "yyyy-MM-dd",
                        "calendar_interval": "year",
                        "offset": 0,
                        "order": {
                            "_key": "asc"
                        },
                        "keyed": false,
                        "min_doc_count": 0,
                        "extended_bounds": {
                            "min": "2023-01-01",
                            "max": "2023-02-25"
                        },
                        "hard_bounds": {
                            "min": "2023-01-01",
                            "max": "2023-03-01"
                        }
                    }
                }
            }
        }
    }
}

The response:

{
  "error": {
    "root_cause": [
      {
        "type": "array_index_out_of_bounds_exception",
        "reason": "Index 0 out of bounds for length 0"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": ".ds-traces-apm.rum-default-2023.09.23-000005",
        "node": "h617in0wTpa8CiLs1LjuvQ",
        "reason": {
          "type": "array_index_out_of_bounds_exception",
          "reason": "Index 0 out of bounds for length 0"
        }
      }
    ],
    "caused_by": {
      "type": "array_index_out_of_bounds_exception",
      "reason": "Index 0 out of bounds for length 0",
      "caused_by": {
        "type": "array_index_out_of_bounds_exception",
        "reason": "Index 0 out of bounds for length 0"
      }
    }
  },
  "status": 500
}

Rewritting the query as:

GET .ds-traces-apm.rum-default-2023.09.23-000005/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
         {
           "match_all": {}
         }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "from": "2023-01-01",
              "to": "2023-02-28",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ]
    }
  },
  "_source": false,
  "aggregations": {
    "AGG0_1": {
      "date_histogram": {
        "field": "@timestamp",
        "format": "yyyy-MM-dd",
        "calendar_interval": "year",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2023-01-01",
          "max": "2023-02-28"
        },
        "hard_bounds": {
          "min": "2023-01-01",
          "max": "2023-03-01"
        }
      }
    }
  }
}

Or removing the filter overcomes the issue.

I guess it comes from a non adapted interval with respect to the bounds. Still it should not lead to an error.

Steps to Reproduce

The bug can be reproduced on the official ES demo. I think you can reproduce it on any date field.

Logs (if relevant)

A piece of stack trace on my local machine:

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at org.elasticsearch.server@8.9.2/org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.hasOverlap(RangeAggregator.java:883)
    at org.elasticsearch.server@8.9.2/org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.buildWithoutAttemptedToAdaptToFilters(RangeAggregator.java:455)
    at org.elasticsearch.server@8.9.2/org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.build(RangeAggregator.java:350)
    at org.elasticsearch.server@8.9.2/org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregator.lambda$adaptIntoRangeOrNull$0(DateHistogramAggregator.java:175)
    at org.elasticsearch.server@8.9.2/org.elasticsearch.search.aggregations.AdaptingAggregator.<init>(AdaptingAggregator.java:41)
elasticsearchmachine commented 9 months ago

Pinging @elastic/es-analytics-geo (Team:Analytics)