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

ValuesSource refactoring: Wire up string_stats aggregation #52875

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -138,7 +138,7 @@ public abstract class AggregatorTestCase extends ESTestCase {
private static final String NESTEDFIELD_PREFIX = "nested_";
private List<Releasable> releasables = new ArrayList<>();
private static final String TYPE_NAME = "type";
private static ValuesSourceRegistry valuesSourceRegistry;
protected static ValuesSourceRegistry valuesSourceRegistry;

// A list of field types that should not be tested, or are not currently supported
private static List<String> TYPE_TEST_BLACKLIST = List.of(
Expand Down Expand Up @@ -687,7 +687,7 @@ private void writeTestDoc(MappedFieldType fieldType, String fieldName, RandomInd

String typeName = fieldType.typeName();
ValuesSourceType vst = fieldType.getValuesSourceType();

if (vst.equals(CoreValuesSourceType.NUMERIC)) {
// TODO note: once VS refactor adds DATE/BOOLEAN, this conditional will go away
if (typeName.equals(DateFieldMapper.CONTENT_TYPE) || typeName.equals(DateFieldMapper.DATE_NANOS_CONTENT_TYPE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public List<AggregationSpec> getAggregations() {
new AggregationSpec(
StringStatsAggregationBuilder.NAME,
StringStatsAggregationBuilder::new,
StringStatsAggregationBuilder.PARSER).addResultReader(InternalStringStats::new),
StringStatsAggregationBuilder.PARSER)
.addResultReader(InternalStringStats::new)
.setAggregatorRegistrar(StringStatsAggregationBuilder::registerAggregators),
new AggregationSpec(
BoxplotAggregationBuilder.NAME,
BoxplotAggregationBuilder::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
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 @@ -108,6 +109,10 @@ public StringStatsAggregationBuilder showDistribution(boolean showDistribution)
return this;
}

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

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), showDistribution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
package org.elasticsearch.xpack.analytics.stringstats;

import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
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 @@ -34,6 +38,24 @@ class StringStatsAggregatorFactory extends ValuesSourceAggregatorFactory {
this.showDistribution = showDistribution;
}

static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(StringStatsAggregationBuilder.NAME,
CoreValuesSourceType.BYTES, new StringStatsAggregatorSupplier() {
@Override
public Aggregator build(String name,
ValuesSource valuesSource,
boolean showDistribution,
DocValueFormat format,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {
return new StringStatsAggregator(name, showDistribution, (ValuesSource.Bytes) valuesSource,
format, context, parent, pipelineAggregators, metaData);
}
});
}

@Override
protected Aggregator createUnmapped(SearchContext searchContext,
Aggregator parent,
Expand All @@ -50,12 +72,15 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
boolean collectsFromSingleBucket,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {
if (valuesSource instanceof ValuesSource.Bytes == false) {
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
this.name());
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
StringStatsAggregationBuilder.NAME);

if (aggregatorSupplier instanceof StringStatsAggregatorSupplier == false) {
throw new AggregationExecutionException("Registry miss-match - expected StringStatsAggregatorSupplier, found [" +
aggregatorSupplier.getClass().toString() + "]");
}
return new StringStatsAggregator(name, showDistribution, (ValuesSource.Bytes) valuesSource, config.format(), searchContext, parent,
pipelineAggregators, metaData);
return ((StringStatsAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, showDistribution, config.format(),
searchContext, parent, pipelineAggregators, metaData);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.analytics.stringstats;

import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.Aggregator;
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;

public interface StringStatsAggregatorSupplier extends AggregatorSupplier {

Aggregator build(String name,
ValuesSource valuesSource,
boolean showDistribution,
DocValueFormat format,
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 @@ -18,24 +18,35 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.mapper.TextFieldMapper;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.xpack.analytics.AnalyticsPlugin;
import org.junit.BeforeClass;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import static java.util.Collections.singleton;

public class StringStatsAggregatorTests extends AggregatorTestCase {

@BeforeClass()
public static void registerBuilder() {
StringStatsAggregationBuilder.registerAggregators(valuesSourceRegistry);
}

private void testCase(Query query,
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
Consumer<InternalStringStats> verify) throws IOException {
Expand Down Expand Up @@ -259,4 +270,22 @@ public void testNestedAggregation() throws IOException {
directory.close();
}


// @Override
// protected Collection<Class<? extends Plugin>> getPlugins() {
//// List<Class<? extends Plugin>> plugins = new ArrayList<>(super.getPlugins());
//// plugins.add(AggregateMetricMapperPlugin.class);
//// plugins.add(LocalStateCompositeXPackPlugin.class);
//// return plugins;
// return null;
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed cleaning this up. Fixed



@Override
protected NamedXContentRegistry xContentRegistry() {
SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.singletonList(new AnalyticsPlugin()));
return new NamedXContentRegistry(searchModule.getNamedXContents());
}


}