Aggregations Refactor: Refactor Children Aggregation

This commit is contained in:
Colin Goodheart-Smithe 2015-11-25 10:06:17 +00:00
parent ea6f312abe
commit a495a75bfc
3 changed files with 84 additions and 4 deletions

View File

@ -67,9 +67,8 @@ public class ChildrenParser implements Aggregator.Parser {
return new ParentToChildrenAggregator.Factory(aggregationName, childType); return new ParentToChildrenAggregator.Factory(aggregationName, childType);
} }
// NORELEASE implement this method when refactoring this aggregation
@Override @Override
public AggregatorFactory[] getFactoryPrototypes() { public AggregatorFactory[] getFactoryPrototypes() {
return null; return new AggregatorFactory[] { new ParentToChildrenAggregator.Factory(null, null) };
} }
} }

View File

@ -27,10 +27,14 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight; import org.apache.lucene.search.Weight;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.util.LongArray; import org.elasticsearch.common.util.LongArray;
import org.elasticsearch.common.util.LongObjectPagedHashMap; import org.elasticsearch.common.util.LongObjectPagedHashMap;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper; import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
@ -44,21 +48,26 @@ import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.FieldContext; import org.elasticsearch.search.aggregations.support.FieldContext;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set;
// The RecordingPerReaderBucketCollector assumes per segment recording which isn't the case for this // The RecordingPerReaderBucketCollector assumes per segment recording which isn't the case for this
// aggregation, for this reason that collector can't be used // aggregation, for this reason that collector can't be used
public class ParentToChildrenAggregator extends SingleBucketAggregator { public class ParentToChildrenAggregator extends SingleBucketAggregator {
static final ParseField TYPE_FIELD = new ParseField("type");
private final String parentType; private final String parentType;
private final Weight childFilter; private final Weight childFilter;
private final Weight parentFilter; private final Weight parentFilter;
@ -188,8 +197,14 @@ public class ParentToChildrenAggregator extends SingleBucketAggregator {
private Query parentFilter; private Query parentFilter;
private Query childFilter; private Query childFilter;
/**
* @param name
* the name of this aggregation
* @param childType
* the type of children documents
*/
public Factory(String name, String childType) { public Factory(String name, String childType) {
super(name, InternalChildren.TYPE, new ValuesSourceParser.Input<ValuesSource.Bytes.WithOrdinals.ParentChild>()); super(name, InternalChildren.TYPE, ValuesSourceType.BYTES, ValueType.STRING);
this.childType = childType; this.childType = childType;
} }
@ -247,5 +262,35 @@ public class ParentToChildrenAggregator extends SingleBucketAggregator {
} }
} }
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(TYPE_FIELD.getPreferredName(), childType);
return builder;
}
@Override
protected ValuesSourceAggregatorFactory<ParentChild> innerReadFrom(String name, ValuesSourceType valuesSourceType,
ValueType targetValueType, StreamInput in) throws IOException {
String childType = in.readString();
Factory factory = new Factory(name, childType);
return factory;
}
@Override
protected void innerWriteTo(StreamOutput out) throws IOException {
out.writeString(childType);
}
@Override
protected int innerHashCode() {
return Objects.hash(childType);
}
@Override
protected boolean innerEquals(Object obj) {
Factory other = (Factory) obj;
return Objects.equals(childType, other.childType);
}
} }
} }

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;
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
import org.elasticsearch.search.aggregations.bucket.children.ParentToChildrenAggregator;
import org.elasticsearch.search.aggregations.bucket.children.ParentToChildrenAggregator.Factory;
public class ChildrenTests extends BaseAggregationTestCase<ParentToChildrenAggregator.Factory> {
@Override
protected Factory createTestAggregatorFactory() {
String name = randomAsciiOfLengthBetween(3, 20);
String childType = randomAsciiOfLengthBetween(5, 40);
Factory factory = new Factory(name, childType);
return factory;
}
}