Cut filters aggregation to registerAggregation

and remove its PROTOTYPE.
This commit is contained in:
Nik Everett 2016-04-14 14:53:43 -04:00
parent 135511134c
commit cf80b00507
3 changed files with 148 additions and 191 deletions

View File

@ -105,7 +105,7 @@ import org.elasticsearch.search.aggregations.bucket.children.ChildrenAggregatorB
import org.elasticsearch.search.aggregations.bucket.children.InternalChildren;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersParser;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregatorBuilder;
import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridParser;
import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid;
@ -456,7 +456,8 @@ public class SearchModule extends AbstractModule {
registerAggregatorParser(new GlobalParser());
registerAggregatorParser(new MissingParser());
registerAggregation(FilterAggregatorBuilder::new, FilterAggregatorBuilder::parse, FilterAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new FiltersParser(queryParserRegistry));
registerAggregation(FiltersAggregatorBuilder::new, (n, c) -> FiltersAggregatorBuilder.parse(queryParserRegistry, n, c),
FiltersAggregatorBuilder.AGGREGATION_NAME_FIELD);
registerAggregatorParser(new SamplerParser());
registerAggregatorParser(new DiversifiedSamplerParser());
registerAggregatorParser(new TermsParser());

View File

@ -19,11 +19,16 @@
package org.elasticsearch.search.aggregations.bucket.filters;
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.index.query.MatchAllQueryBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.search.aggregations.AggregatorBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
@ -37,9 +42,15 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class FiltersAggregatorBuilder extends AggregatorBuilder<FiltersAggregatorBuilder> {
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
static final FiltersAggregatorBuilder PROTOTYPE = new FiltersAggregatorBuilder("", new MatchAllQueryBuilder());
public class FiltersAggregatorBuilder extends AggregatorBuilder<FiltersAggregatorBuilder> {
public static final String NAME = InternalFilters.TYPE.name();
public static final ParseField AGGREGATION_NAME_FIELD = new ParseField(NAME);
private static final ParseField FILTERS_FIELD = new ParseField("filters");
private static final ParseField OTHER_BUCKET_FIELD = new ParseField("other_bucket");
private static final ParseField OTHER_BUCKET_KEY_FIELD = new ParseField("other_bucket_key");
private final List<KeyedFilter> filters;
private final boolean keyed;
@ -80,6 +91,49 @@ public class FiltersAggregatorBuilder extends AggregatorBuilder<FiltersAggregato
this.keyed = false;
}
/**
* Read from a stream.
*/
public FiltersAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalFilters.TYPE);
keyed = in.readBoolean();
int filtersSize = in.readVInt();
filters = new ArrayList<>(filtersSize);
if (keyed) {
for (int i = 0; i < filtersSize; i++) {
filters.add(KeyedFilter.PROTOTYPE.readFrom(in));
}
} else {
for (int i = 0; i < filtersSize; i++) {
filters.add(new KeyedFilter(String.valueOf(i), in.readQuery()));
}
}
otherBucket = in.readBoolean();
otherBucketKey = in.readString();
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeBoolean(keyed);
out.writeVInt(filters.size());
if (keyed) {
for (KeyedFilter keyedFilter : filters) {
keyedFilter.writeTo(out);
}
} else {
for (KeyedFilter keyedFilter : filters) {
out.writeQuery(keyedFilter.filter());
}
}
out.writeBoolean(otherBucket);
out.writeString(otherBucketKey);
}
@Override
protected boolean usesNewStyleSerialization() {
return true;
}
/**
* Set whether to include a bucket for documents not matching any filter
*/
@ -151,45 +205,92 @@ public class FiltersAggregatorBuilder extends AggregatorBuilder<FiltersAggregato
return builder;
}
@Override
protected FiltersAggregatorBuilder doReadFrom(String name, StreamInput in) throws IOException {
FiltersAggregatorBuilder factory;
if (in.readBoolean()) {
int size = in.readVInt();
List<KeyedFilter> filters = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
filters.add(KeyedFilter.PROTOTYPE.readFrom(in));
}
factory = new FiltersAggregatorBuilder(name, filters);
} else {
int size = in.readVInt();
QueryBuilder<?>[] filters = new QueryBuilder<?>[size];
for (int i = 0; i < size; i++) {
filters[i] = in.readQuery();
}
factory = new FiltersAggregatorBuilder(name, filters);
}
factory.otherBucket = in.readBoolean();
factory.otherBucketKey = in.readString();
return factory;
}
public static FiltersAggregatorBuilder parse(IndicesQueriesRegistry queriesRegistry, String aggregationName, QueryParseContext context)
throws IOException {
XContentParser parser = context.parser();
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeBoolean(keyed);
if (keyed) {
out.writeVInt(filters.size());
for (KeyedFilter keyedFilter : filters) {
keyedFilter.writeTo(out);
}
} else {
out.writeVInt(filters.size());
for (KeyedFilter keyedFilter : filters) {
out.writeQuery(keyedFilter.filter());
List<FiltersAggregator.KeyedFilter> keyedFilters = null;
List<QueryBuilder<?>> nonKeyedFilters = null;
XContentParser.Token token = null;
String currentFieldName = null;
String otherBucketKey = null;
Boolean otherBucket = false;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (context.parseFieldMatcher().match(currentFieldName, OTHER_BUCKET_FIELD)) {
otherBucket = parser.booleanValue();
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if (context.parseFieldMatcher().match(currentFieldName, OTHER_BUCKET_KEY_FIELD)) {
otherBucketKey = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (context.parseFieldMatcher().match(currentFieldName, FILTERS_FIELD)) {
keyedFilters = new ArrayList<>();
String key = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
key = parser.currentName();
} else {
QueryParseContext queryParseContext = new QueryParseContext(queriesRegistry);
queryParseContext.reset(parser);
queryParseContext.parseFieldMatcher(context.parseFieldMatcher());
QueryBuilder<?> filter = queryParseContext.parseInnerQueryBuilder();
keyedFilters.add(new FiltersAggregator.KeyedFilter(key, filter == null ? matchAllQuery() : filter));
}
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (context.parseFieldMatcher().match(currentFieldName, FILTERS_FIELD)) {
nonKeyedFilters = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
QueryParseContext queryParseContext = new QueryParseContext(queriesRegistry);
queryParseContext.reset(parser);
queryParseContext.parseFieldMatcher(context.parseFieldMatcher());
QueryBuilder<?> filter = queryParseContext.parseInnerQueryBuilder();
nonKeyedFilters.add(filter == null ? QueryBuilders.matchAllQuery() : filter);
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
}
out.writeBoolean(otherBucket);
out.writeString(otherBucketKey);
if (otherBucket && otherBucketKey == null) {
otherBucketKey = "_other_";
}
FiltersAggregatorBuilder factory;
if (keyedFilters != null) {
factory = new FiltersAggregatorBuilder(aggregationName,
keyedFilters.toArray(new FiltersAggregator.KeyedFilter[keyedFilters.size()]));
} else {
factory = new FiltersAggregatorBuilder(aggregationName,
nonKeyedFilters.toArray(new QueryBuilder<?>[nonKeyedFilters.size()]));
}
if (otherBucket != null) {
factory.otherBucket(otherBucket);
}
if (otherBucketKey != null) {
factory.otherBucketKey(otherBucketKey);
}
return factory;
}
@Override
@ -205,4 +306,9 @@ public class FiltersAggregatorBuilder extends AggregatorBuilder<FiltersAggregato
&& Objects.equals(otherBucket, other.otherBucket)
&& Objects.equals(otherBucketKey, other.otherBucketKey);
}
@Override
public String getWriteableName() {
return NAME;
}
}

View File

@ -1,150 +0,0 @@
/*
* 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.filters;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.search.aggregations.Aggregator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class FiltersParser implements Aggregator.Parser {
public static final ParseField FILTERS_FIELD = new ParseField("filters");
public static final ParseField OTHER_BUCKET_FIELD = new ParseField("other_bucket");
public static final ParseField OTHER_BUCKET_KEY_FIELD = new ParseField("other_bucket_key");
private final IndicesQueriesRegistry queriesRegistry;
@Inject
public FiltersParser(IndicesQueriesRegistry queriesRegistry) {
this.queriesRegistry = queriesRegistry;
}
@Override
public String type() {
return InternalFilters.TYPE.name();
}
@Override
public FiltersAggregatorBuilder parse(String aggregationName, QueryParseContext context)
throws IOException {
XContentParser parser = context.parser();
List<FiltersAggregator.KeyedFilter> keyedFilters = null;
List<QueryBuilder<?>> nonKeyedFilters = null;
XContentParser.Token token = null;
String currentFieldName = null;
String otherBucketKey = null;
Boolean otherBucket = false;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (context.parseFieldMatcher().match(currentFieldName, OTHER_BUCKET_FIELD)) {
otherBucket = parser.booleanValue();
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if (context.parseFieldMatcher().match(currentFieldName, OTHER_BUCKET_KEY_FIELD)) {
otherBucketKey = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (context.parseFieldMatcher().match(currentFieldName, FILTERS_FIELD)) {
keyedFilters = new ArrayList<>();
String key = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
key = parser.currentName();
} else {
QueryParseContext queryParseContext = new QueryParseContext(queriesRegistry);
queryParseContext.reset(parser);
queryParseContext.parseFieldMatcher(context.parseFieldMatcher());
QueryBuilder<?> filter = queryParseContext.parseInnerQueryBuilder();
keyedFilters
.add(new FiltersAggregator.KeyedFilter(key, filter == null ? QueryBuilders.matchAllQuery() : filter));
}
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (context.parseFieldMatcher().match(currentFieldName, FILTERS_FIELD)) {
nonKeyedFilters = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
QueryParseContext queryParseContext = new QueryParseContext(queriesRegistry);
queryParseContext.reset(parser);
queryParseContext.parseFieldMatcher(context.parseFieldMatcher());
QueryBuilder<?> filter = queryParseContext.parseInnerQueryBuilder();
nonKeyedFilters.add(filter == null ? QueryBuilders.matchAllQuery() : filter);
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(),
"Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
}
if (otherBucket && otherBucketKey == null) {
otherBucketKey = "_other_";
}
FiltersAggregatorBuilder factory;
if (keyedFilters != null) {
factory = new FiltersAggregatorBuilder(aggregationName,
keyedFilters.toArray(new FiltersAggregator.KeyedFilter[keyedFilters.size()]));
} else {
factory = new FiltersAggregatorBuilder(aggregationName,
nonKeyedFilters.toArray(new QueryBuilder<?>[nonKeyedFilters.size()]));
}
if (otherBucket != null) {
factory.otherBucket(otherBucket);
}
if (otherBucketKey != null) {
factory.otherBucketKey(otherBucketKey);
}
return factory;
}
@Override
public FiltersAggregatorBuilder getFactoryPrototypes() {
return FiltersAggregatorBuilder.PROTOTYPE;
}
}