|
26 | 26 | import org.apache.lucene.search.Scorer; |
27 | 27 | import org.apache.lucene.search.Weight; |
28 | 28 | 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; |
29 | 32 | import org.elasticsearch.common.lease.Releasables; |
30 | 33 | import org.elasticsearch.common.lucene.Lucene; |
31 | 34 | import org.elasticsearch.common.util.LongArray; |
32 | 35 | import org.elasticsearch.common.util.LongObjectPagedHashMap; |
| 36 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
33 | 37 | import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData; |
34 | 38 | import org.elasticsearch.index.mapper.DocumentMapper; |
35 | 39 | import org.elasticsearch.index.mapper.internal.ParentFieldMapper; |
|
43 | 47 | import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
44 | 48 | import org.elasticsearch.search.aggregations.support.AggregationContext; |
45 | 49 | import org.elasticsearch.search.aggregations.support.FieldContext; |
| 50 | +import org.elasticsearch.search.aggregations.support.ValueType; |
46 | 51 | import org.elasticsearch.search.aggregations.support.ValuesSource; |
| 52 | +import org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild; |
47 | 53 | import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; |
48 | 54 | import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; |
49 | | -import org.elasticsearch.search.aggregations.support.ValuesSourceParser; |
50 | 55 | import org.elasticsearch.search.aggregations.support.ValuesSourceType; |
51 | 56 |
|
52 | 57 | import java.io.IOException; |
53 | 58 | import java.util.Arrays; |
54 | 59 | import java.util.LinkedHashSet; |
55 | 60 | import java.util.List; |
56 | 61 | import java.util.Map; |
| 62 | +import java.util.Objects; |
57 | 63 | import java.util.Set; |
58 | 64 |
|
59 | 65 | // The RecordingPerReaderBucketCollector assumes per segment recording which isn't the case for this |
60 | 66 | // aggregation, for this reason that collector can't be used |
61 | 67 | public class ParentToChildrenAggregator extends SingleBucketAggregator { |
62 | 68 |
|
| 69 | + static final ParseField TYPE_FIELD = new ParseField("type"); |
| 70 | + |
63 | 71 | private final String parentType; |
64 | 72 | private final Weight childFilter; |
65 | 73 | private final Weight parentFilter; |
@@ -200,8 +208,14 @@ public static class Factory extends ValuesSourceAggregatorFactory<ValuesSource.B |
200 | 208 | private Query parentFilter; |
201 | 209 | private Query childFilter; |
202 | 210 |
|
| 211 | + /** |
| 212 | + * @param name |
| 213 | + * the name of this aggregation |
| 214 | + * @param childType |
| 215 | + * the type of children documents |
| 216 | + */ |
203 | 217 | 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); |
205 | 219 | this.childType = childType; |
206 | 220 | } |
207 | 221 |
|
@@ -259,5 +273,35 @@ private void resolveConfig(AggregationContext aggregationContext) { |
259 | 273 | } |
260 | 274 | } |
261 | 275 |
|
| 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 | + |
262 | 306 | } |
263 | 307 | } |
0 commit comments