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

Remove filter rewrite optimization for range aggregations when segment is not effective match all #15194

Merged
merged 19 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix delete index template failed when the index template matches a data stream but is unused ([#15080](https://github.com/opensearch-project/OpenSearch/pull/15080))
- Fix array_index_out_of_bounds_exception when indexing documents with field name containing only dot ([#15126](https://github.com/opensearch-project/OpenSearch/pull/15126))
- Fixed array field name omission in flat_object function for nested JSON ([#13620](https://github.com/opensearch-project/OpenSearch/pull/13620))
- Fix range aggregation optimization ignoring top level queries ([#15194](https://github.com/opensearch-project/OpenSearch/pull/15194))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,94 @@ setup:
- match: { aggregations.histo.buckets.8.doc_count: 1 }
- match: { aggregations.histo.buckets.12.key_as_string: "2016-06-01T00:00:00.000Z" }
- match: { aggregations.histo.buckets.12.doc_count: 1 }

---
"Date histogram aggregation w/ filter query test":
- skip:
version: " - 2.99.99"
reason: Backport fix to 2.16

- do:
bulk:
refresh: true
index: dhisto-agg-w-query
body:
- '{"index": {}}'
- '{"routing": "route1", "date": "2024-08-12", "dow": "monday"}'
- '{"index": {}}'
- '{"routing": "route1", "date": "2024-08-14", "dow": "wednesday"}'
- '{"index": {}}'
- '{"routing": "route1", "date": "2024-08-19", "dow": "monday"}'
- '{"index": {}}'
- '{"routing": "route2", "date": "2024-08-13", "dow": "tuesday"}'
- '{"index": {}}'
- '{"routing": "route2", "date": "2024-08-15", "dow": "thursday"}'

- do:
search:
index: dhisto-agg-w-query
body:
query:
bool:
must:
match_all: {}
filter:
- terms:
routing:
- "route1"
aggregations:
weekHisto:
date_histogram:
field: date
calendar_interval: week
_source: false

- match: { hits.total.value: 3 }
- match: { aggregations.weekHisto.buckets.0.doc_count: 2 }
- match: { aggregations.weekHisto.buckets.1.doc_count: 1 }

---
"Date histogram aggregation w/ shared field range test":
- do:
bulk:
refresh: true
index: dhisto-agg-w-query
body:
- '{"index": {}}'
- '{"date": "2024-10-31"}'
- '{"index": {}}'
- '{"date": "2024-11-11"}'
- '{"index": {}}'
- '{"date": "2024-11-28"}'
- '{"index": {}}'
- '{"date": "2024-12-25"}'
- '{"index": {}}'
- '{"date": "2025-01-01"}'
- '{"index": {}}'
- '{"date": "2025-02-14"}'

- do:
search:
index: dhisto-agg-w-query
body:
profile: true
query:
range:
date:
gte: "2024-01-01"
lt: "2025-01-01"
aggregations:
monthHisto:
date_histogram:
field: date
calendar_interval: month
_source: false

- match: { hits.total.value: 4 }
- match: { aggregations.monthHisto.buckets.0.doc_count: 1 }
- match: { aggregations.monthHisto.buckets.1.doc_count: 2 }
- match: { aggregations.monthHisto.buckets.2.doc_count: 1 }
- match: { profile.shards.0.aggregations.0.debug.optimized_segments: 1 }
- match: { profile.shards.0.aggregations.0.debug.unoptimized_segments: 0 }
- match: { profile.shards.0.aggregations.0.debug.leaf_visited: 0 }
- match: { profile.shards.0.aggregations.0.debug.inner_visited: 0 }
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,82 @@ setup:
- match: { aggregations.my_range.buckets.3.from: 1.5 }
- is_false: aggregations.my_range.buckets.3.to
- match: { aggregations.my_range.buckets.3.doc_count: 2 }

---
"Filter query w/ aggregation test":
- skip:
version: " - 2.99.99"
reason: Backport fix to 2.16

- do:
bulk:
refresh: true
index: range-agg-w-query
body:
- '{"index": {}}'
- '{"routing": "route1", "v": -10, "date": "2024-10-29"}'
- '{"index": {}}'
- '{"routing": "route1", "v": -5, "date": "2024-10-30"}'
- '{"index": {}}'
- '{"routing": "route1", "v": 10, "date": "2024-10-31"}'
- '{"index": {}}'
- '{"routing": "route2", "v": 15, "date": "2024-11-01"}'
- '{"index": {}}'
- '{"routing": "route2", "v": 20, "date": "2024-11-02"}'

- do:
search:
index: range-agg-w-query
body:
query:
bool:
must:
match_all: {}
filter:
- terms:
routing:
- "route1"
aggregations:
NegPosAgg:
range:
field: v
keyed: true
ranges:
- to: 0
key: "0"
- from: 0
key: "1"
_source: false

- match: { hits.total.value: 3 }
- match: { aggregations.NegPosAgg.buckets.0.doc_count: 2 }
- match: { aggregations.NegPosAgg.buckets.1.doc_count: 1 }

- do:
search:
index: range-agg-w-query
body:
query:
bool:
must:
match_all: {}
filter:
- terms:
routing:
- "route1"
aggregations:
HalloweenAgg:
date_range:
field: date
format: "yyyy-MM-dd"
keyed: true
ranges:
- to: "2024-11-01"
key: "to-october"
- from: "2024-11-01"
key: "from-september"
_source: false

- match: { hits.total.value: 3 }
- match: { aggregations.HalloweenAgg.buckets.to-october.doc_count: 3 }
- match: { aggregations.HalloweenAgg.buckets.from-september.doc_count: 0 }
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private Helper() {}
* Recursively unwraps query into the concrete form
* for applying the optimization
*/
private static Query unwrapIntoConcreteQuery(Query query) {
static Query unwrapIntoConcreteQuery(Query query) {
while (queryWrappers.containsKey(query.getClass())) {
query = queryWrappers.get(query.getClass()).apply(query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.NumericPointEncoder;
import org.opensearch.search.aggregations.bucket.range.RangeAggregator;
import org.opensearch.search.aggregations.support.ValuesSource;
import org.opensearch.search.aggregations.support.ValuesSourceConfig;
import org.opensearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -51,8 +54,14 @@ protected boolean canOptimize(ValuesSourceConfig config, RangeAggregator.Range[]
return false;
}

protected void buildRanges(RangeAggregator.Range[] ranges) {
protected void buildRanges(RangeAggregator.Range[] ranges, SearchContext context) {
assert fieldType instanceof NumericPointEncoder;
Query unwrap = Helper.unwrapIntoConcreteQuery(context.query());
if (!(unwrap instanceof MatchAllDocsQuery)) {
setRanges.accept(null);
return;
}

finnegancarroll marked this conversation as resolved.
Show resolved Hide resolved
NumericPointEncoder numericPointEncoder = (NumericPointEncoder) fieldType;
byte[][] lowers = new byte[ranges.length][];
byte[][] uppers = new byte[ranges.length][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected boolean canOptimize() {

@Override
protected void prepare() {
buildRanges(ranges);
buildRanges(ranges, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@

package org.opensearch.search.aggregations.bucket.range;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.KeywordField;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.util.TestUtil;
Expand All @@ -54,6 +61,7 @@
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType;
import org.opensearch.index.mapper.NumberFieldMapper.NumberType;
import org.opensearch.index.mapper.ParseContext.Document;
import org.opensearch.search.aggregations.AggregationBuilder;
import org.opensearch.search.aggregations.AggregatorTestCase;
import org.opensearch.search.aggregations.CardinalityUpperBound;
Expand Down Expand Up @@ -124,6 +132,36 @@ public void testMatchesNumericDocValues() throws IOException {
});
}

public void testTopLevelTermQuery() throws IOException {
final String KEYWORD_FIELD_NAME = "route";
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.setMinimumNumberShouldMatch(0);
builder.add(new TermQuery(new Term(KEYWORD_FIELD_NAME, "route1")), BooleanClause.Occur.MUST);
Query boolQuery = builder.build();

Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();
doc1.add(new NumericDocValuesField(NUMBER_FIELD_NAME, 3));
doc2.add(new NumericDocValuesField(NUMBER_FIELD_NAME, 11));
doc3.add(new NumericDocValuesField(NUMBER_FIELD_NAME, 12));
doc1.add(new KeywordField(KEYWORD_FIELD_NAME, "route1", Field.Store.NO));
doc2.add(new KeywordField(KEYWORD_FIELD_NAME, "route1", Field.Store.NO));
doc3.add(new KeywordField(KEYWORD_FIELD_NAME, "route2", Field.Store.NO));

testCase(boolQuery, iw -> {
iw.addDocument(doc1);
iw.addDocument(doc2);
iw.addDocument(doc3);
}, range -> {
List<? extends InternalRange.Bucket> ranges = range.getBuckets();
assertEquals(2, ranges.size());
assertEquals(1, ranges.get(0).getDocCount());
assertEquals(1, ranges.get(1).getDocCount());
assertTrue(AggregationInspectionHelper.hasValue(range));
});
}
finnegancarroll marked this conversation as resolved.
Show resolved Hide resolved

public void testDateFieldMillisecondResolution() throws IOException {
DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME);

Expand Down Expand Up @@ -457,6 +495,29 @@ public void testFloatType() throws IOException {
);
}

public void testTopLevelRangeQuery() throws IOException {
NumberFieldType fieldType = new NumberFieldType(NumberType.INTEGER.typeName(), NumberType.INTEGER);
String fieldName = fieldType.numberType().typeName();
Query query = IntPoint.newRangeQuery(fieldName, 5, 20);

testRewriteOptimizationCase(
fieldType,
new double[][] { { 0.0, 10.0 }, { 10.0, 20.0 } },
query,
new Number[] { 0.1, 4.0, 9, 11, 12, 19 },
range -> {
List<? extends InternalRange.Bucket> ranges = range.getBuckets();
assertEquals(2, ranges.size());
assertEquals("0.0-10.0", ranges.get(0).getKeyAsString());
assertEquals(1, ranges.get(0).getDocCount());
assertEquals("10.0-20.0", ranges.get(1).getKeyAsString());
assertEquals(3, ranges.get(1).getDocCount());
assertTrue(AggregationInspectionHelper.hasValue(range));
},
false
);
}

public void testUnsignedLongType() throws IOException {
testRewriteOptimizationCase(
new NumberFieldType(NumberType.UNSIGNED_LONG.typeName(), NumberType.UNSIGNED_LONG),
Expand Down
Loading