|
| 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.significant; |
| 21 | + |
| 22 | +import org.elasticsearch.common.CheckedBiConsumer; |
| 23 | +import org.elasticsearch.common.CheckedFunction; |
| 24 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
| 28 | +import org.elasticsearch.search.aggregations.Aggregation; |
| 29 | +import org.elasticsearch.search.aggregations.Aggregations; |
| 30 | +import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Iterator; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.function.Function; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +public abstract class ParsedSignificantTerms extends ParsedMultiBucketAggregation<ParsedSignificantTerms.ParsedBucket> |
| 41 | + implements SignificantTerms { |
| 42 | + |
| 43 | + private Map<String, ParsedBucket> bucketMap; |
| 44 | + protected long subsetSize; |
| 45 | + |
| 46 | + protected long getSubsetSize() { |
| 47 | + return subsetSize; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<? extends SignificantTerms.Bucket> getBuckets() { |
| 52 | + return buckets; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public SignificantTerms.Bucket getBucketByKey(String term) { |
| 57 | + if (bucketMap == null) { |
| 58 | + bucketMap = buckets.stream().collect(Collectors.toMap(SignificantTerms.Bucket::getKeyAsString, Function.identity())); |
| 59 | + } |
| 60 | + return bucketMap.get(term); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Iterator<SignificantTerms.Bucket> iterator() { |
| 65 | + return buckets.stream().map(bucket -> (SignificantTerms.Bucket) bucket).collect(Collectors.toList()).iterator(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { |
| 70 | + builder.field(CommonFields.DOC_COUNT.getPreferredName(), subsetSize); |
| 71 | + builder.startArray(CommonFields.BUCKETS.getPreferredName()); |
| 72 | + for (SignificantTerms.Bucket bucket : buckets) { |
| 73 | + bucket.toXContent(builder, params); |
| 74 | + } |
| 75 | + builder.endArray(); |
| 76 | + return builder; |
| 77 | + } |
| 78 | + |
| 79 | + static void declareParsedSignificantTermsFields(final ObjectParser<? extends ParsedSignificantTerms, Void> objectParser, |
| 80 | + final CheckedFunction<XContentParser, ParsedSignificantTerms.ParsedBucket, IOException> bucketParser) { |
| 81 | + declareMultiBucketAggregationFields(objectParser, bucketParser::apply, bucketParser::apply); |
| 82 | + objectParser.declareLong((parsedTerms, value) -> parsedTerms.subsetSize = value , CommonFields.DOC_COUNT); |
| 83 | + } |
| 84 | + |
| 85 | + public abstract static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket implements SignificantTerms.Bucket { |
| 86 | + |
| 87 | + protected long subsetDf; |
| 88 | + protected long supersetDf; |
| 89 | + protected double score; |
| 90 | + |
| 91 | + @Override |
| 92 | + public long getDocCount() { |
| 93 | + return getSubsetDf(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public long getSubsetDf() { |
| 98 | + return subsetDf; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public long getSupersetDf() { |
| 103 | + return supersetDf; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public double getSignificanceScore() { |
| 108 | + return score; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public long getSupersetSize() { |
| 113 | + throw new UnsupportedOperationException(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public long getSubsetSize() { |
| 118 | + throw new UnsupportedOperationException(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 123 | + builder.startObject(); |
| 124 | + keyToXContent(builder); |
| 125 | + builder.field(CommonFields.DOC_COUNT.getPreferredName(), getDocCount()); |
| 126 | + builder.field(InternalSignificantTerms.SCORE, getSignificanceScore()); |
| 127 | + builder.field(InternalSignificantTerms.BG_COUNT, getSupersetDf()); |
| 128 | + getAggregations().toXContentInternal(builder, params); |
| 129 | + builder.endObject(); |
| 130 | + return builder; |
| 131 | + } |
| 132 | + |
| 133 | + protected abstract XContentBuilder keyToXContent(XContentBuilder builder) throws IOException; |
| 134 | + |
| 135 | + static <B extends ParsedBucket> B parseSignificantTermsBucketXContent(final XContentParser parser, final B bucket, |
| 136 | + final CheckedBiConsumer<XContentParser, B, IOException> keyConsumer) throws IOException { |
| 137 | + |
| 138 | + final List<Aggregation> aggregations = new ArrayList<>(); |
| 139 | + XContentParser.Token token; |
| 140 | + String currentFieldName = parser.currentName(); |
| 141 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 142 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 143 | + currentFieldName = parser.currentName(); |
| 144 | + } else if (token.isValue()) { |
| 145 | + if (CommonFields.KEY_AS_STRING.getPreferredName().equals(currentFieldName)) { |
| 146 | + bucket.setKeyAsString(parser.text()); |
| 147 | + } else if (CommonFields.KEY.getPreferredName().equals(currentFieldName)) { |
| 148 | + keyConsumer.accept(parser, bucket); |
| 149 | + } else if (CommonFields.DOC_COUNT.getPreferredName().equals(currentFieldName)) { |
| 150 | + long value = parser.longValue(); |
| 151 | + bucket.subsetDf = value; |
| 152 | + bucket.setDocCount(value); |
| 153 | + } else if (InternalSignificantTerms.SCORE.equals(currentFieldName)) { |
| 154 | + bucket.score = parser.longValue(); |
| 155 | + } else if (InternalSignificantTerms.BG_COUNT.equals(currentFieldName)) { |
| 156 | + bucket.supersetDf = parser.longValue(); |
| 157 | + } |
| 158 | + } else if (token == XContentParser.Token.START_OBJECT) { |
| 159 | + aggregations.add(XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class)); |
| 160 | + } |
| 161 | + } |
| 162 | + bucket.setAggregations(new Aggregations(aggregations)); |
| 163 | + return bucket; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments