[BUG] filter aggregation with `now` date math intermittently fails with "DateRangeIncludingNowQuery ... does not implement createWeight" under concurrent segment search
Describe the bug
A filter aggregation whose filter is a range query on a date field using now (for example "gte": "now/d-1d") intermittently fails on individual shards. The request returns partial results with "timed_out": false:
"_shards": {
"total": 5, "successful": 4, "skipped": 0, "failed": 1,
"failures": [{
"shard": 0, "index": "repro",
"reason": {
"type": "unsupported_operation_exception",
"reason": "Query DateRangeIncludingNowQuery(IndexOrDocValuesQuery(indexQuery=timestamp:[1789344000000 TO 9223372036854775807], dvQuery=timestamp:[1789344000000 TO 9223372036854775807])) does not implement createWeight"
}
}]
}Most identical requests succeed.
What we observed with the reproduction below:
| Condition | Shard failures |
|---|---|
index.search.concurrent_segment_search.mode: all |
yes |
index.search.concurrent_segment_search.mode: auto (default) |
yes |
index.search.concurrent_segment_search.mode: none |
no |
mode: all with search.concurrent.max_slice_count: 1 |
no |
absolute timestamp instead of now |
no |
now-1d (no rounding) instead of now/d-1d |
yes |
the same ranges in a filters (plural) aggregation |
no |
| the same range as the top-level query | no |
| OpenSearch 3.2.0 / 3.8.0 | yes |
| OpenSearch 3.1.0 | no |
Related component
Search:Aggregations
To Reproduce
The index needs many segments per shard so that concurrent segment search uses more than one slice; merging is suppressed for that.
- Start a node:
docker run -d --name os-repro -p 19200:9200 \
-e discovery.type=single-node \
-e DISABLE_SECURITY_PLUGIN=true \
-e DISABLE_INSTALL_DEMO_CONFIG=true \
-e OPENSEARCH_JAVA_OPTS="-Xms2g -Xmx2g" \
opensearchproject/opensearch:3.8.0- Create the index with merging suppressed and concurrent segment search enabled:
curl -XPUT localhost:19200/repro -H 'Content-Type: application/json' -d '{
"settings": { "index": {
"number_of_shards": 5, "number_of_replicas": 0, "refresh_interval": "-1",
"search": { "concurrent_segment_search": { "mode": "all" } },
"merge": { "policy": {
"max_merged_segment": "1kb", "segments_per_tier": 200, "floor_segment": "1kb" } } } },
"mappings": { "properties": {
"timestamp": { "type": "date" }, "n": { "type": "long" } } } }'- Index 120,000 documents in 60 refreshed batches (about 60 segments per shard):
python3 - <<'PY'
import json, random, time, urllib.request
BASE, NOW, DAY = "http://localhost:19200", int(time.time()*1000), 86400000
for b in range(60):
lines = []
for i in range(2000):
lines.append(json.dumps({"index": {"_index": "repro"}}))
lines.append(json.dumps({"timestamp": NOW - random.randint(0, 400)*DAY, "n": i}))
r = urllib.request.Request(BASE + "/_bulk", data=("\n".join(lines) + "\n").encode(), method="POST")
r.add_header("Content-Type", "application/x-ndjson")
urllib.request.urlopen(r).read()
urllib.request.urlopen(urllib.request.Request(BASE + "/repro/_refresh", method="POST")).read()
PY- Save this search as
query.json:
{
"size": 0,
"query": { "match_all": {} },
"aggs": {
"day": { "filter": { "range": { "timestamp": { "gte": "now/d-1d" } } } },
"week": { "filter": { "range": { "timestamp": { "gte": "now/d-7d" } } } },
"month": { "filter": { "range": { "timestamp": { "gte": "now/d-1M" } } } },
"year": { "filter": { "range": { "timestamp": { "gte": "now/d-1y" } } } }
}
}- Run it 100 times and count
_shards.failed:
for i in $(seq 1 100); do
curl -s -XPOST localhost:19200/repro/_search -H 'Content-Type: application/json' -d @query.json \
| grep -o '"failed":[0-9]*' | head -1
done | sort | uniq -cSome requests report "failed":1 (occasionally "failed":2). Add ?error_trace=true to the search URL to get the stack trace below.
- Switch concurrent segment search off and repeat step 5; every request reports
"failed":0:
curl -XPUT localhost:19200/repro/_settings -H 'Content-Type: application/json' \
-d '{"index.search.concurrent_segment_search.mode":"none"}'The failures are intermittent. We saw them most often shortly after the node started; if all 100 requests succeed, restart the node and run step 5 again.
Expected behavior
All shards succeed, as they do with index.search.concurrent_segment_search.mode: none and on OpenSearch 3.1.0.
Additional Details
Plugins
The plugins bundled in the official opensearchproject/opensearch image; the security plugin is disabled (DISABLE_SECURITY_PLUGIN=true).
Host/Environment
- OS: macOS 27.0 (arm64), Docker Desktop (Docker Engine 29.8.0, linux/arm64)
- Version: 3.8.0 (also reproduced on 3.2.0; not reproduced on 3.1.0)
- Single node, all settings other than those shown in the steps left at their defaults
Additional context
Stack trace (from ?error_trace=true, OpenSearch 3.8.0):
java.lang.UnsupportedOperationException: Query DateRangeIncludingNowQuery(IndexOrDocValuesQuery(indexQuery=timestamp:[1789344000000 TO 9223372036854775807], dvQuery=timestamp:[1789344000000 TO 9223372036854775807])) does not implement createWeight
at org.apache.lucene.search.Query.createWeight(Query.java:69)
at org.opensearch.search.approximate.ApproximateScoreQuery.createWeight(ApproximateScoreQuery.java:92)
at org.apache.lucene.search.IndexSearcher.createWeight(IndexSearcher.java:990)
at org.opensearch.search.internal.ContextIndexSearcher.createWeight(ContextIndexSearcher.java:248)
at org.opensearch.search.aggregations.bucket.filter.FilterAggregatorFactory.getWeight(FilterAggregatorFactory.java:86)
at org.opensearch.search.aggregations.bucket.filter.FilterAggregator.getLeafCollector(FilterAggregator.java:78)
at org.opensearch.search.aggregations.AggregatorBase.getLeafCollector(AggregatorBase.java:211)
at org.opensearch.search.aggregations.MultiBucketCollector.getLeafCollector(MultiBucketCollector.java:165)
at org.opensearch.search.aggregations.MultiBucketCollector.getLeafCollector(MultiBucketCollector.java:56)
at org.apache.lucene.search.MultiCollector.getLeafCollector(MultiCollector.java:130)
at org.opensearch.search.internal.ContextIndexSearcher.searchLeaf(ContextIndexSearcher.java:360)
at org.opensearch.search.internal.ContextIndexSearcher.search(ContextIndexSearcher.java:325)
at org.apache.lucene.search.IndexSearcher.lambda$search$1(IndexSearcher.java:790)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:328)
at org.apache.lucene.search.TaskExecutor$Task.run(TaskExecutor.java:173)
at org.apache.lucene.search.TaskExecutor.lambda$invokeAll$1(TaskExecutor.java:98)
at org.opensearch.threadpool.TaskAwareRunnable.doRun(TaskAwareRunnable.java:78)
at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
at org.opensearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:59)
at org.opensearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:1011)
at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
at java.base/java.lang.Thread.run(Thread.java:1474)Code at the frames in the trace (3.8.0 tag):
- For a range that uses
now,DateFieldMapperreturnsnew ApproximateScoreQuery(new DateRangeIncludingNowQuery(query), approxQuery)(DateFieldMapper.java:571). This wrapping was added in 3.2.0 by #18511. ApproximateScoreQuery.java:92isreturn resolvedQuery.createWeight(...).resolvedQueryis a mutable field:setContext(), called fromContextIndexSearcher.rewrite(), sets it tooriginalQuerywhencanApproximate()is false (always the case when the request has aggregations), andrewrite()then replaces it with the rewritten query.DateRangeIncludingNowQuery.rewrite()unwraps itself, and the class does not overridecreateWeight().FilterAggregatorFactory.getWeight()builds the weight lazily withcreateWeight(rewrite(filter), ...), without synchronization. It is called fromFilterAggregator.getLeafCollector(), which in the trace runs on a concurrent segment search slice task. Its javadoc says: "as aggregations are initialsed and executed in a serial manner, no concurrency considerations are necessary here."
Given that the failure needs more than one slice, we suspect that slices of one shard call getWeight() concurrently on the same ApproximateScoreQuery instance, but we have not confirmed this.
Workaround: set index.search.concurrent_segment_search.mode (or the cluster setting search.concurrent_segment_search.mode) to none.
This was originally reported by a Fess user; Fess builds its date facets as filter aggregations with now-based ranges:
https://discuss.codelibs.org/t/intermittent-daterangeincludingnowquery-shard-failures-on-fess-15-8-opensearch-3-8/2766
Source: opensearch-project/OpenSearch