|
@@ -27,19 +27,51 @@ import org.elasticsearch.search.aggregations.AggregatorFactory;
|
|
|
import org.elasticsearch.search.aggregations.InternalAggregation;
|
|
|
import org.elasticsearch.search.aggregations.NonCollectingAggregator;
|
|
|
import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregator.ExecutionMode;
|
|
|
+import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
|
|
|
+import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
|
|
|
import org.elasticsearch.search.aggregations.support.ValuesSource;
|
|
|
-import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric;
|
|
|
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
|
|
|
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
|
|
|
+import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
|
|
|
import org.elasticsearch.search.internal.SearchContext;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-import static org.elasticsearch.search.aggregations.support.AggregationUsageService.OTHER_SUBTYPE;
|
|
|
-
|
|
|
public class DiversifiedAggregatorFactory extends ValuesSourceAggregatorFactory {
|
|
|
|
|
|
+ public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
|
|
|
+ builder.register(DiversifiedAggregationBuilder.NAME,
|
|
|
+ List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
|
|
|
+ (DiversifiedAggregatorSupplier) (String name, int shardSize, AggregatorFactories factories, SearchContext context,
|
|
|
+ Aggregator parent, Map<String, Object> metadata, ValuesSource valuesSource,
|
|
|
+ int maxDocsPerValue, String executionHint) ->
|
|
|
+ new DiversifiedNumericSamplerAggregator(name, shardSize, factories, context, parent, metadata, valuesSource,
|
|
|
+ maxDocsPerValue)
|
|
|
+ );
|
|
|
+
|
|
|
+ builder.register(DiversifiedAggregationBuilder.NAME, CoreValuesSourceType.BYTES,
|
|
|
+ (DiversifiedAggregatorSupplier) (String name, int shardSize, AggregatorFactories factories, SearchContext context,
|
|
|
+ Aggregator parent, Map<String, Object> metadata, ValuesSource valuesSource,
|
|
|
+ int maxDocsPerValue, String executionHint) -> {
|
|
|
+ ExecutionMode execution = null;
|
|
|
+ if (executionHint != null) {
|
|
|
+ execution = ExecutionMode.fromString(executionHint);
|
|
|
+ }
|
|
|
+
|
|
|
+ // In some cases using ordinals is just not supported: override it
|
|
|
+ if (execution == null) {
|
|
|
+ execution = ExecutionMode.GLOBAL_ORDINALS;
|
|
|
+ }
|
|
|
+ if ((execution.needsGlobalOrdinals()) && (!(valuesSource instanceof ValuesSource.Bytes.WithOrdinals))) {
|
|
|
+ execution = ExecutionMode.MAP;
|
|
|
+ }
|
|
|
+ return execution.create(name, factories, shardSize, maxDocsPerValue, valuesSource, context, parent, metadata);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private final int shardSize;
|
|
|
private final int maxDocsPerValue;
|
|
|
private final String executionHint;
|
|
@@ -60,30 +92,14 @@ public class DiversifiedAggregatorFactory extends ValuesSourceAggregatorFactory
|
|
|
boolean collectsFromSingleBucket,
|
|
|
Map<String, Object> metadata) throws IOException {
|
|
|
|
|
|
- if (valuesSource instanceof ValuesSource.Numeric) {
|
|
|
- return new DiversifiedNumericSamplerAggregator(name, shardSize, factories, searchContext, parent, metadata,
|
|
|
- (Numeric) valuesSource, maxDocsPerValue);
|
|
|
- }
|
|
|
-
|
|
|
- if (valuesSource instanceof ValuesSource.Bytes) {
|
|
|
- ExecutionMode execution = null;
|
|
|
- if (executionHint != null) {
|
|
|
- execution = ExecutionMode.fromString(executionHint);
|
|
|
- }
|
|
|
-
|
|
|
- // In some cases using ordinals is just not supported: override
|
|
|
- // it
|
|
|
- if (execution == null) {
|
|
|
- execution = ExecutionMode.GLOBAL_ORDINALS;
|
|
|
- }
|
|
|
- if ((execution.needsGlobalOrdinals()) && (!(valuesSource instanceof ValuesSource.Bytes.WithOrdinals))) {
|
|
|
- execution = ExecutionMode.MAP;
|
|
|
- }
|
|
|
- return execution.create(name, factories, shardSize, maxDocsPerValue, valuesSource, searchContext, parent, metadata);
|
|
|
+ AggregatorSupplier supplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
|
|
|
+ DiversifiedAggregationBuilder.NAME);
|
|
|
+ if (supplier instanceof DiversifiedAggregatorSupplier == false) {
|
|
|
+ throw new AggregationExecutionException("Registry miss-match - expected " + DiversifiedAggregatorSupplier.class.toString() +
|
|
|
+ ", found [" + supplier.getClass().toString() + "]");
|
|
|
}
|
|
|
-
|
|
|
- throw new AggregationExecutionException("Sampler aggregation cannot be applied to field [" + config.fieldContext().field()
|
|
|
- + "]. It can only be applied to numeric or string fields.");
|
|
|
+ return ((DiversifiedAggregatorSupplier) supplier).build(name, shardSize, factories, searchContext, parent, metadata, valuesSource,
|
|
|
+ maxDocsPerValue, executionHint);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -99,10 +115,4 @@ public class DiversifiedAggregatorFactory extends ValuesSourceAggregatorFactory
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public String getStatsSubtype() {
|
|
|
- // DiversifiedAggregatorFactory doesn't register itself with ValuesSourceRegistry
|
|
|
- return OTHER_SUBTYPE;
|
|
|
- }
|
|
|
}
|