Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed Dec 23, 2023
1 parent 029686f commit 654e542
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,18 @@ public static void buildFastFilterContext(
* Encapsulates metadata about a value source needed to rewrite
*/
public static class FastFilterContext {
private final boolean missing;
private final boolean hasScript;
private boolean missing = false;
private boolean hasScript = false;
private final MappedFieldType fieldType;

private long afterKey = -1L;
private int size = Integer.MAX_VALUE; // only used by composite aggregation for pagination
private Weight[] filters = null;

/**
* @param missing whether missing value/bucket is set
* @param hasScript whether script is used
* @param fieldType null if the field doesn't exist
*/
public FastFilterContext(boolean missing, boolean hasScript, MappedFieldType fieldType) {
this.missing = missing;
this.hasScript = hasScript;
public FastFilterContext(MappedFieldType fieldType) {
this.fieldType = fieldType;
}

Expand All @@ -248,6 +244,11 @@ public void setAfterKey(long afterKey) {
this.afterKey = afterKey;
}

public void setMissingAndHasScript(boolean missing, boolean hasScript) {
this.missing = missing;
this.hasScript = hasScript;
}

/**
* The pre-conditions to initiate fast filter optimization on aggregations are:
* 1. The query with aggregation has to be PointRangeQuery on the same date field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ final class CompositeAggregator extends BucketsAggregator {
bucketOrds = LongKeyedBucketOrds.build(context.bigArrays(), CardinalityUpperBound.ONE);
preparedRounding = dateHistogramSource.getPreparedRounding();
fastFilterContext = new FastFilterRewriteHelper.FastFilterContext(
sourceConfigs[0].missingBucket(),
sourceConfigs[0].hasScript(),
sourceConfigs[0].fieldType()
);
fastFilterContext.setMissingAndHasScript(
sourceConfigs[0].missingBucket(),
sourceConfigs[0].hasScript()
);
if (rawAfterKey != null) {
assert rawAfterKey.size() == 1 && formats.size() == 1;
long afterValue = formats.get(0).parseLong(rawAfterKey.get(0).toString(), false, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.opensearch.search.aggregations.LeafBucketCollector;
import org.opensearch.search.aggregations.LeafBucketCollectorBase;
import org.opensearch.search.aggregations.bucket.BucketsAggregator;
import org.opensearch.search.aggregations.bucket.FastFilterRewriteHelper;
import org.opensearch.search.internal.SearchContext;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ private AutoDateHistogramAggregator(
this.preparedRounding = prepareRounding(0);

fastFilterContext = new FastFilterRewriteHelper.FastFilterContext(
valuesSourceConfig.missing() != null,
valuesSourceConfig.script() != null,
valuesSourceConfig.fieldType()
);
fastFilterContext.setMissingAndHasScript(
valuesSourceConfig.missing() != null,
valuesSourceConfig.script() != null
);
if (fastFilterContext.isRewriteable(parent, subAggregators.length)) {
FastFilterRewriteHelper.buildFastFilterContext(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ class DateHistogramAggregator extends BucketsAggregator implements SizedBucketAg
bucketOrds = LongKeyedBucketOrds.build(context.bigArrays(), cardinality);

fastFilterContext = new FastFilterRewriteHelper.FastFilterContext(
valuesSourceConfig.missing() != null,
valuesSourceConfig.script() != null,
valuesSourceConfig.fieldType()
);
fastFilterContext.setMissingAndHasScript(
valuesSourceConfig.missing() != null,
valuesSourceConfig.script() != null
);
if (fastFilterContext.isRewriteable(parent, subAggregators.length)) {
FastFilterRewriteHelper.buildFastFilterContext(
context,
Expand Down

0 comments on commit 654e542

Please sign in to comment.