Cut children aggregation to registerAggregation

This commit is contained in:
Nik Everett 2016-04-12 18:25:56 -04:00
parent b8fdb19312
commit a170de5df7
3 changed files with 51 additions and 9 deletions

View File

@ -101,7 +101,7 @@ import org.elasticsearch.search.aggregations.AggregationPhase;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorBuilder;
import org.elasticsearch.search.aggregations.AggregatorParsers;
import org.elasticsearch.search.aggregations.bucket.children.ChildrenParser;
import org.elasticsearch.search.aggregations.bucket.children.ChildrenAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.children.InternalChildren;
import org.elasticsearch.search.aggregations.bucket.filter.FilterParser;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
@ -468,7 +468,8 @@ public class SearchModule extends AbstractModule {
registerAggregatorParser(new GeoBoundsParser());
registerAggregatorParser(new GeoCentroidParser());
registerAggregatorParser(new ScriptedMetricParser());
registerAggregatorParser(new ChildrenParser());
registerAggregation(ChildrenAggregatorBuilder.PROTOTYPE::readFrom, ChildrenAggregatorBuilder::parse,
ChildrenAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerPipelineParser(new DerivativeParser());
registerPipelineParser(new MaxBucketParser());

View File

@ -44,13 +44,16 @@ public abstract class Aggregator extends BucketCollector implements Releasable {
* Parses the aggregation request and creates the appropriate aggregator factory for it.
*
* @see AggregatorBuilder
*/
*/
@FunctionalInterface
public interface Parser {
/**
* @return The aggregation type this parser is associated with.
*/
String type();
default String type() {
throw new UnsupportedOperationException(); // NORELEASE remove before 5.0.0GA
}
/**
* Returns the aggregator factory with which this parser is associated, may return {@code null} indicating the
@ -68,7 +71,9 @@ public abstract class Aggregator extends BucketCollector implements Releasable {
* @return an empty {@link AggregatorBuilder} instance for this parser
* that can be used for deserialization
*/
AggregatorBuilder<?> getFactoryPrototypes();
default AggregatorBuilder<?> getFactoryPrototypes() {
throw new UnsupportedOperationException(); // NORELEASE remove before 5.0.0GA
}
}
/**

View File

@ -20,30 +20,35 @@
package org.elasticsearch.search.aggregations.bucket.children;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.bucket.children.ChildrenAggregatorBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.FieldContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild;
import java.io.IOException;
import java.util.Objects;
public class ChildrenAggregatorBuilder extends ValuesSourceAggregatorBuilder<ParentChild, ChildrenAggregatorBuilder> {
public static final String NAME = InternalChildren.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
static final ChildrenAggregatorBuilder PROTOTYPE = new ChildrenAggregatorBuilder("", "");
public static final ChildrenAggregatorBuilder PROTOTYPE = new ChildrenAggregatorBuilder("", "");
private String parentType;
private final String childType;
@ -105,6 +110,37 @@ public class ChildrenAggregatorBuilder extends ValuesSourceAggregatorBuilder<Par
return builder;
}
public static ChildrenAggregatorBuilder parse(String aggregationName, XContentParser parser,
QueryParseContext context) throws IOException {
String childType = null;
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("type".equals(currentFieldName)) {
childType = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
}
}
if (childType == null) {
throw new ParsingException(parser.getTokenLocation(),
"Missing [child_type] field for children aggregation [" + aggregationName + "]");
}
return new ChildrenAggregatorBuilder(aggregationName, childType);
}
@Override
protected ChildrenAggregatorBuilder innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {