Skip to content

Commit ca4919e

Browse files
committed
Aggregations Refactor: Refactor Children Aggregation
1 parent a3dfa11 commit ca4919e

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/bucket/children/ChildrenParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ public AggregatorFactory parse(String aggregationName, XContentParser parser, Se
6767
return new ParentToChildrenAggregator.Factory(aggregationName, childType);
6868
}
6969

70-
// NORELEASE implement this method when refactoring this aggregation
7170
@Override
7271
public AggregatorFactory[] getFactoryPrototypes() {
73-
return null;
72+
return new AggregatorFactory[] { new ParentToChildrenAggregator.Factory(null, null) };
7473
}
7574
}

core/src/main/java/org/elasticsearch/search/aggregations/bucket/children/ParentToChildrenAggregator.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
import org.apache.lucene.search.Scorer;
2727
import org.apache.lucene.search.Weight;
2828
import org.apache.lucene.util.Bits;
29+
import org.elasticsearch.common.ParseField;
30+
import org.elasticsearch.common.io.stream.StreamInput;
31+
import org.elasticsearch.common.io.stream.StreamOutput;
2932
import org.elasticsearch.common.lease.Releasables;
3033
import org.elasticsearch.common.lucene.Lucene;
3134
import org.elasticsearch.common.util.LongArray;
3235
import org.elasticsearch.common.util.LongObjectPagedHashMap;
36+
import org.elasticsearch.common.xcontent.XContentBuilder;
3337
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
3438
import org.elasticsearch.index.mapper.DocumentMapper;
3539
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
@@ -43,23 +47,27 @@
4347
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
4448
import org.elasticsearch.search.aggregations.support.AggregationContext;
4549
import org.elasticsearch.search.aggregations.support.FieldContext;
50+
import org.elasticsearch.search.aggregations.support.ValueType;
4651
import org.elasticsearch.search.aggregations.support.ValuesSource;
52+
import org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild;
4753
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
4854
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
49-
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
5055
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
5156

5257
import java.io.IOException;
5358
import java.util.Arrays;
5459
import java.util.LinkedHashSet;
5560
import java.util.List;
5661
import java.util.Map;
62+
import java.util.Objects;
5763
import java.util.Set;
5864

5965
// The RecordingPerReaderBucketCollector assumes per segment recording which isn't the case for this
6066
// aggregation, for this reason that collector can't be used
6167
public class ParentToChildrenAggregator extends SingleBucketAggregator {
6268

69+
static final ParseField TYPE_FIELD = new ParseField("type");
70+
6371
private final String parentType;
6472
private final Weight childFilter;
6573
private final Weight parentFilter;
@@ -200,8 +208,14 @@ public static class Factory extends ValuesSourceAggregatorFactory<ValuesSource.B
200208
private Query parentFilter;
201209
private Query childFilter;
202210

211+
/**
212+
* @param name
213+
* the name of this aggregation
214+
* @param childType
215+
* the type of children documents
216+
*/
203217
public Factory(String name, String childType) {
204-
super(name, InternalChildren.TYPE, new ValuesSourceParser.Input<ValuesSource.Bytes.WithOrdinals.ParentChild>());
218+
super(name, InternalChildren.TYPE, ValuesSourceType.BYTES, ValueType.STRING);
205219
this.childType = childType;
206220
}
207221

@@ -259,5 +273,35 @@ private void resolveConfig(AggregationContext aggregationContext) {
259273
}
260274
}
261275

276+
@Override
277+
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
278+
builder.field(TYPE_FIELD.getPreferredName(), childType);
279+
return builder;
280+
}
281+
282+
@Override
283+
protected ValuesSourceAggregatorFactory<ParentChild> innerReadFrom(String name, ValuesSourceType valuesSourceType,
284+
ValueType targetValueType, StreamInput in) throws IOException {
285+
String childType = in.readString();
286+
Factory factory = new Factory(name, childType);
287+
return factory;
288+
}
289+
290+
@Override
291+
protected void innerWriteTo(StreamOutput out) throws IOException {
292+
out.writeString(childType);
293+
}
294+
295+
@Override
296+
protected int innerHashCode() {
297+
return Objects.hash(childType);
298+
}
299+
300+
@Override
301+
protected boolean innerEquals(Object obj) {
302+
Factory other = (Factory) obj;
303+
return Objects.equals(childType, other.childType);
304+
}
305+
262306
}
263307
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.bucket;
21+
22+
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
23+
import org.elasticsearch.search.aggregations.bucket.children.ParentToChildrenAggregator;
24+
import org.elasticsearch.search.aggregations.bucket.children.ParentToChildrenAggregator.Factory;
25+
26+
public class ChildrenTests extends BaseAggregationTestCase<ParentToChildrenAggregator.Factory> {
27+
28+
@Override
29+
protected Factory createTestAggregatorFactory() {
30+
String name = randomAsciiOfLengthBetween(3, 20);
31+
String childType = randomAsciiOfLengthBetween(5, 40);
32+
Factory factory = new Factory(name, childType);
33+
return factory;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)