Aggregations Refactor: Refactor Nested and Reverse Nested Aggregations

This commit is contained in:
Colin Goodheart-Smithe 2015-11-25 09:52:45 +00:00
parent cf7e525244
commit ea6f312abe
7 changed files with 187 additions and 9 deletions

View File

@ -27,7 +27,11 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.Weight; import org.apache.lucene.search.Weight;
import org.apache.lucene.search.join.BitSetProducer; import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.util.BitSet; import org.apache.lucene.util.BitSet;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.mapper.object.ObjectMapper;
import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.Aggregator;
@ -44,12 +48,15 @@ import org.elasticsearch.search.aggregations.support.AggregationContext;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* *
*/ */
public class NestedAggregator extends SingleBucketAggregator { public class NestedAggregator extends SingleBucketAggregator {
static final ParseField PATH_FIELD = new ParseField("path");
private BitSetProducer parentFilter; private BitSetProducer parentFilter;
private final Query childFilter; private final Query childFilter;
@ -141,11 +148,25 @@ public class NestedAggregator extends SingleBucketAggregator {
private final String path; private final String path;
/**
* @param name
* the name of this aggregation
* @param path
* the path to use for this nested aggregation. The path must
* match the path to a nested object in the mappings.
*/
public Factory(String name, String path) { public Factory(String name, String path) {
super(name, InternalNested.TYPE); super(name, InternalNested.TYPE);
this.path = path; this.path = path;
} }
/**
* Get the path to use for this nested aggregation.
*/
public String path() {
return path;
}
@Override @Override
public Aggregator createInternal(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket, public Aggregator createInternal(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
@ -162,6 +183,37 @@ public class NestedAggregator extends SingleBucketAggregator {
return new NestedAggregator(name, factories, objectMapper, context, parent, pipelineAggregators, metaData); return new NestedAggregator(name, factories, objectMapper, context, parent, pipelineAggregators, metaData);
} }
@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(PATH_FIELD.getPreferredName(), path);
builder.endObject();
return builder;
}
@Override
protected AggregatorFactory doReadFrom(String name, StreamInput in) throws IOException {
String path = in.readString();
Factory factory = new Factory(name, path);
return factory;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(path);
}
@Override
protected int doHashCode() {
return Objects.hash(path);
}
@Override
protected boolean doEquals(Object obj) {
Factory other = (Factory) obj;
return Objects.equals(path, other.path);
}
private final static class Unmapped extends NonCollectingAggregator { private final static class Unmapped extends NonCollectingAggregator {
public Unmapped(String name, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) public Unmapped(String name, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData)

View File

@ -46,7 +46,7 @@ public class NestedParser implements Aggregator.Parser {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) { } else if (token == XContentParser.Token.VALUE_STRING) {
if ("path".equals(currentFieldName)) { if (context.parseFieldMatcher().match(currentFieldName, NestedAggregator.PATH_FIELD)) {
path = parser.text(); path = parser.text();
} else { } else {
throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
@ -67,9 +67,8 @@ public class NestedParser implements Aggregator.Parser {
return new NestedAggregator.Factory(aggregationName, path); return new NestedAggregator.Factory(aggregationName, path);
} }
// NORELEASE implement this method when refactoring this aggregation
@Override @Override
public AggregatorFactory[] getFactoryPrototypes() { public AggregatorFactory[] getFactoryPrototypes() {
return null; return new AggregatorFactory[] { new NestedAggregator.Factory(null, null) };
} }
} }

View File

@ -24,7 +24,11 @@ import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.BitSetProducer; import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.util.BitSet; import org.apache.lucene.util.BitSet;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.mapper.object.ObjectMapper;
import org.elasticsearch.search.SearchParseException; import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregationExecutionException;
@ -42,12 +46,15 @@ import org.elasticsearch.search.aggregations.support.AggregationContext;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* *
*/ */
public class ReverseNestedAggregator extends SingleBucketAggregator { public class ReverseNestedAggregator extends SingleBucketAggregator {
static final ParseField PATH_FIELD = new ParseField("path");
private final Query parentFilter; private final Query parentFilter;
private final BitSetProducer parentBitsetProducer; private final BitSetProducer parentBitsetProducer;
@ -120,13 +127,28 @@ public class ReverseNestedAggregator extends SingleBucketAggregator {
public static class Factory extends AggregatorFactory { public static class Factory extends AggregatorFactory {
private final String path; private String path;
public Factory(String name, String path) { public Factory(String name) {
super(name, InternalReverseNested.TYPE); super(name, InternalReverseNested.TYPE);
}
/**
* Set the path to use for this nested aggregation. The path must match
* the path to a nested object in the mappings. If it is not specified
* then this aggregation will go back to the root document.
*/
public void path(String path) {
this.path = path; this.path = path;
} }
/**
* Get the path to use for this nested aggregation.
*/
public String path() {
return path;
}
@Override @Override
public Aggregator createInternal(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket, public Aggregator createInternal(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket,
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
@ -152,6 +174,39 @@ public class ReverseNestedAggregator extends SingleBucketAggregator {
return new ReverseNestedAggregator(name, factories, objectMapper, context, parent, pipelineAggregators, metaData); return new ReverseNestedAggregator(name, factories, objectMapper, context, parent, pipelineAggregators, metaData);
} }
@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (path != null) {
builder.field(PATH_FIELD.getPreferredName(), path);
}
builder.endObject();
return builder;
}
@Override
protected AggregatorFactory doReadFrom(String name, StreamInput in) throws IOException {
Factory factory = new Factory(name);
factory.path = in.readOptionalString();
return factory;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalString(path);
}
@Override
protected int doHashCode() {
return Objects.hash(path);
}
@Override
protected boolean doEquals(Object obj) {
Factory other = (Factory) obj;
return Objects.equals(path, other.path);
}
private final static class Unmapped extends NonCollectingAggregator { private final static class Unmapped extends NonCollectingAggregator {
public Unmapped(String name, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) public Unmapped(String name, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData)

View File

@ -58,12 +58,15 @@ public class ReverseNestedParser implements Aggregator.Parser {
} }
} }
return new ReverseNestedAggregator.Factory(aggregationName, path); ReverseNestedAggregator.Factory factory = new ReverseNestedAggregator.Factory(aggregationName);
if (path != null) {
factory.path(path);
}
return factory;
} }
// NORELEASE implement this method when refactoring this aggregation
@Override @Override
public AggregatorFactory[] getFactoryPrototypes() { public AggregatorFactory[] getFactoryPrototypes() {
return null; return new AggregatorFactory[] { new ReverseNestedAggregator.Factory(null) };
} }
} }

View File

@ -119,7 +119,8 @@ public class NestedAggregatorTests extends ESSingleNodeTestCase {
AggregationContext context = new AggregationContext(searchContext); AggregationContext context = new AggregationContext(searchContext);
AggregatorFactories.Builder builder = AggregatorFactories.builder(); AggregatorFactories.Builder builder = AggregatorFactories.builder();
builder.addAggregator(new NestedAggregator.Factory("test", "nested_field")); NestedAggregator.Factory factory = new NestedAggregator.Factory("test", "nested_field");
builder.addAggregator(factory);
AggregatorFactories factories = builder.build(); AggregatorFactories factories = builder.build();
searchContext.aggregations(new SearchContextAggregations(factories)); searchContext.aggregations(new SearchContextAggregations(factories));
factories.init(context); factories.init(context);

View File

@ -0,0 +1,32 @@
/*
* 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.nested;
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregator.Factory;
public class NestedTests extends BaseAggregationTestCase<NestedAggregator.Factory> {
@Override
protected Factory createTestAggregatorFactory() {
return new Factory(randomAsciiOfLengthBetween(1, 20), randomAsciiOfLengthBetween(3, 40));
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.nested;
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
import org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregator.Factory;
public class ReverseNestedTests extends BaseAggregationTestCase<ReverseNestedAggregator.Factory> {
@Override
protected Factory createTestAggregatorFactory() {
Factory factory = new Factory(randomAsciiOfLengthBetween(1, 20));
if (randomBoolean()) {
factory.path(randomAsciiOfLengthBetween(3, 40));
}
return factory;
}
}