Skip to content

Convert RareTerms to new VS registry #52166

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ setup:
- length: { aggregations.ip_terms.buckets: 0 }

- do:
catch: request
catch: /Aggregation \[ip_terms\] cannot support regular expression style include\/exclude settings as they can only be applied to string fields\. Use an array of values for include\/exclude clauses/
search:
index: test_1
body: { "size" : 0, "aggs" : { "ip_terms" : { "rare_terms" : { "field" : "ip", "exclude" : "127.*" } } } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ private void registerAggregations(List<SearchPlugin> plugins) {
RareTermsAggregationBuilder::parse)
.addResultReader(StringRareTerms.NAME, StringRareTerms::new)
.addResultReader(UnmappedRareTerms.NAME, UnmappedRareTerms::new)
.addResultReader(LongRareTerms.NAME, LongRareTerms::new));
.addResultReader(LongRareTerms.NAME, LongRareTerms::new)
.setAggregatorRegistrar(RareTermsAggregationBuilder::registerAggregators));
registerAggregation(new AggregationSpec(SignificantTermsAggregationBuilder.NAME, SignificantTermsAggregationBuilder::new,
SignificantTermsAggregationBuilder::parse)
.addResultReader(SignificantStringTerms.NAME, SignificantStringTerms::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

import java.io.IOException;
Expand Down Expand Up @@ -66,6 +67,10 @@ public static AggregationBuilder parse(String aggregationName, XContentParser pa
return PARSER.parse(parser, new RareTermsAggregationBuilder(aggregationName), null);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
RareTermsAggregatorFactory.registerAggregators(valuesSourceRegistry);
}

private IncludeExclude includeExclude = null;
private int maxDocCount = 1;
private double precision = 0.001;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.NonCollectingAggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
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.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;
Expand All @@ -44,6 +47,88 @@ public class RareTermsAggregatorFactory extends ValuesSourceAggregatorFactory {
private final int maxDocCount;
private final double precision;

static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(RareTermsAggregationBuilder.NAME,
List.of(CoreValuesSourceType.BYTES, CoreValuesSourceType.IP),
RareTermsAggregatorFactory.bytesSupplier());

valuesSourceRegistry.register(RareTermsAggregationBuilder.NAME,
List.of(CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN, CoreValuesSourceType.NUMERIC),
RareTermsAggregatorFactory.numericSupplier());
}

/**
* This supplier is used for all the field types that should be aggregated as bytes/strings,
* including those that need global ordinals
*/
private static RareTermsAggregatorSupplier bytesSupplier() {
return new RareTermsAggregatorSupplier() {
@Override
public Aggregator build(String name,
AggregatorFactories factories,
ValuesSource valuesSource,
DocValueFormat format,
int maxDocCount,
double precision,
IncludeExclude includeExclude,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {

ExecutionMode execution = ExecutionMode.MAP; //TODO global ords not implemented yet, only supports "map"

if ((includeExclude != null) && (includeExclude.isRegexBased()) && format != DocValueFormat.RAW) {
throw new IllegalArgumentException("Aggregation [" + name + "] cannot support " +
"regular expression style include/exclude settings as they can only be applied to string fields. " +
"Use an array of values for include/exclude clauses");
}

return execution.create(name, factories, valuesSource, format,
includeExclude, context, parent, pipelineAggregators, metaData, maxDocCount, precision);

}
};
}

/**
* This supplier is used for all fields that expect to be aggregated as a numeric value.
* This includes floating points, and formatted types that use numerics internally for storage (date, boolean, etc)
*/
private static RareTermsAggregatorSupplier numericSupplier() {
return new RareTermsAggregatorSupplier() {
@Override
public Aggregator build(String name,
AggregatorFactories factories,
ValuesSource valuesSource,
DocValueFormat format,
int maxDocCount,
double precision,
IncludeExclude includeExclude,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {

if ((includeExclude != null) && (includeExclude.isRegexBased())) {
throw new IllegalArgumentException("Aggregation [" + name + "] cannot support regular expression " +
"style include/exclude settings as they can only be applied to string fields. Use an array of numeric " +
"values for include/exclude clauses used to filter numeric fields");
}

IncludeExclude.LongFilter longFilter = null;
if (((ValuesSource.Numeric) valuesSource).isFloatingPoint()) {
throw new IllegalArgumentException("RareTerms aggregation does not support floating point fields.");
}
if (includeExclude != null) {
longFilter = includeExclude.convertToLongFilter(format);
}
return new LongRareTermsAggregator(name, factories, (ValuesSource.Numeric) valuesSource, format,
context, parent, longFilter, maxDocCount, precision, pipelineAggregators, metaData);
}
};
}

RareTermsAggregatorFactory(String name, ValuesSourceConfig config,
IncludeExclude includeExclude,
QueryShardContext queryShardContext,
Expand Down Expand Up @@ -79,40 +164,16 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
if (collectsFromSingleBucket == false) {
return asMultiBucketAggregator(this, searchContext, parent);
}
if (valuesSource instanceof ValuesSource.Bytes) {
ExecutionMode execution = ExecutionMode.MAP; //TODO global ords not implemented yet, only supports "map"

DocValueFormat format = config.format();
if ((includeExclude != null) && (includeExclude.isRegexBased()) && format != DocValueFormat.RAW) {
throw new AggregationExecutionException("Aggregation [" + name + "] cannot support " +
"regular expression style include/exclude settings as they can only be applied to string fields. " +
"Use an array of values for include/exclude clauses");
}

return execution.create(name, factories, valuesSource, format,
includeExclude, searchContext, parent, pipelineAggregators, metaData, maxDocCount, precision);
}

if ((includeExclude != null) && (includeExclude.isRegexBased())) {
throw new AggregationExecutionException("Aggregation [" + name + "] cannot support regular expression style include/exclude "
+ "settings as they can only be applied to string fields. Use an array of numeric values for include/exclude clauses " +
"used to filter numeric fields");
}

if (valuesSource instanceof ValuesSource.Numeric) {
IncludeExclude.LongFilter longFilter = null;
if (((ValuesSource.Numeric) valuesSource).isFloatingPoint()) {
throw new AggregationExecutionException("RareTerms aggregation does not support floating point fields.");
}
if (includeExclude != null) {
longFilter = includeExclude.convertToLongFilter(config.format());
}
return new LongRareTermsAggregator(name, factories, (ValuesSource.Numeric) valuesSource, config.format(),
searchContext, parent, longFilter, maxDocCount, precision, pipelineAggregators, metaData);
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
RareTermsAggregationBuilder.NAME);
if (aggregatorSupplier instanceof RareTermsAggregatorSupplier == false) {
throw new AggregationExecutionException("Registry miss-match - expected RareTermsAggregatorSupplier, found [" +
aggregatorSupplier.getClass().toString() + "]");
}

throw new AggregationExecutionException("RareTerms aggregation cannot be applied to field [" + config.fieldContext().field()
+ "]. It can only be applied to numeric or string fields.");
return ((RareTermsAggregatorSupplier) aggregatorSupplier).build(name, factories, valuesSource, config.format(),
maxDocCount, precision, includeExclude, searchContext, parent, pipelineAggregators, metaData);
}

public enum ExecutionMode {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.bucket.terms;

import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.List;
import java.util.Map;

interface RareTermsAggregatorSupplier extends AggregatorSupplier {
Aggregator build(String name,
AggregatorFactories factories,
ValuesSource valuesSource,
DocValueFormat format,
int maxDocCount,
double precision,
IncludeExclude includeExclude,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorTestCase;
Expand Down Expand Up @@ -311,7 +310,7 @@ public void testRangeField() throws Exception {
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
RareTermsAggregationBuilder aggregationBuilder = new RareTermsAggregationBuilder("_name")
.field("field");
expectThrows(AggregationExecutionException.class,
expectThrows(IllegalArgumentException.class,
() -> createAggregator(aggregationBuilder, indexSearcher, fieldType));
}
}
Expand Down