|
| 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; |
| 21 | + |
| 22 | +import org.apache.lucene.util.BytesRef; |
| 23 | +import org.elasticsearch.search.DocValueFormat; |
| 24 | +import org.elasticsearch.search.aggregations.bucket.terms.InternalTerms; |
| 25 | +import org.elasticsearch.search.aggregations.bucket.terms.LongTerms; |
| 26 | +import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; |
| 27 | +import org.elasticsearch.search.aggregations.metrics.InternalAvg; |
| 28 | +import org.elasticsearch.search.aggregations.support.AggregationPath; |
| 29 | +import org.elasticsearch.test.ESTestCase; |
| 30 | + |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import static org.elasticsearch.search.aggregations.InternalMultiBucketAggregation.resolvePropertyFromPath; |
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | + |
| 39 | +public class InternalMultiBucketAggregationTests extends ESTestCase { |
| 40 | + |
| 41 | + public void testResolveToAgg() { |
| 42 | + AggregationPath path = AggregationPath.parse("the_avg"); |
| 43 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 44 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 45 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 46 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 47 | + |
| 48 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 49 | + buckets.add(bucket); |
| 50 | + |
| 51 | + Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 52 | + assertThat(value[0], equalTo(agg)); |
| 53 | + } |
| 54 | + |
| 55 | + public void testResolveToAggValue() { |
| 56 | + AggregationPath path = AggregationPath.parse("the_avg.value"); |
| 57 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 58 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 59 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 60 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 61 | + |
| 62 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 63 | + buckets.add(bucket); |
| 64 | + |
| 65 | + Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 66 | + assertThat(value[0], equalTo(2.0)); |
| 67 | + } |
| 68 | + |
| 69 | + public void testResolveToNothing() { |
| 70 | + AggregationPath path = AggregationPath.parse("foo.value"); |
| 71 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 72 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 73 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 74 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 75 | + |
| 76 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 77 | + buckets.add(bucket); |
| 78 | + |
| 79 | + InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, |
| 80 | + () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms")); |
| 81 | + assertThat(e.getMessage(), equalTo("Cannot find an aggregation named [foo] in [the_long_terms]")); |
| 82 | + } |
| 83 | + |
| 84 | + public void testResolveToUnknown() { |
| 85 | + AggregationPath path = AggregationPath.parse("the_avg.unknown"); |
| 86 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 87 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 88 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 89 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 90 | + |
| 91 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 92 | + buckets.add(bucket); |
| 93 | + |
| 94 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 95 | + () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms")); |
| 96 | + assertThat(e.getMessage(), equalTo("path not supported for [the_avg]: [unknown]")); |
| 97 | + } |
| 98 | + |
| 99 | + public void testResolveToBucketCount() { |
| 100 | + AggregationPath path = AggregationPath.parse("_bucket_count"); |
| 101 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 102 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 103 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 104 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 105 | + |
| 106 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 107 | + buckets.add(bucket); |
| 108 | + |
| 109 | + Object value = resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 110 | + assertThat(value, equalTo(1)); |
| 111 | + } |
| 112 | + |
| 113 | + public void testResolveToCount() { |
| 114 | + AggregationPath path = AggregationPath.parse("_count"); |
| 115 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 116 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 117 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 118 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 119 | + |
| 120 | + LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 121 | + buckets.add(bucket); |
| 122 | + |
| 123 | + Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 124 | + assertThat(value[0], equalTo(1L)); |
| 125 | + } |
| 126 | + |
| 127 | + public void testResolveToKey() { |
| 128 | + AggregationPath path = AggregationPath.parse("_key"); |
| 129 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 130 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 131 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 132 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(agg)); |
| 133 | + |
| 134 | + LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 135 | + buckets.add(bucket); |
| 136 | + |
| 137 | + Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 138 | + assertThat(value[0], equalTo(19L)); |
| 139 | + } |
| 140 | + |
| 141 | + public void testResolveToSpecificBucket() { |
| 142 | + AggregationPath path = AggregationPath.parse("string_terms['foo']>the_avg.value"); |
| 143 | + |
| 144 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 145 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 146 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 147 | + InternalAggregations internalStringAggs = new InternalAggregations(Collections.singletonList(agg)); |
| 148 | + List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket( |
| 149 | + new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, |
| 150 | + internalStringAggs, false, 0, DocValueFormat.RAW)); |
| 151 | + |
| 152 | + InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), 1, 0, Collections.emptyList(), |
| 153 | + Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0); |
| 154 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(termsAgg)); |
| 155 | + LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 156 | + buckets.add(bucket); |
| 157 | + |
| 158 | + Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"); |
| 159 | + assertThat(value[0], equalTo(2.0)); |
| 160 | + } |
| 161 | + |
| 162 | + public void testResolveToMissingSpecificBucket() { |
| 163 | + AggregationPath path = AggregationPath.parse("string_terms['bar']>the_avg.value"); |
| 164 | + |
| 165 | + List<LongTerms.Bucket> buckets = new ArrayList<>(); |
| 166 | + InternalAggregation agg = new InternalAvg("the_avg", 2, 1, |
| 167 | + DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap()); |
| 168 | + InternalAggregations internalStringAggs = new InternalAggregations(Collections.singletonList(agg)); |
| 169 | + List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket( |
| 170 | + new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, |
| 171 | + internalStringAggs, false, 0, DocValueFormat.RAW)); |
| 172 | + |
| 173 | + InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), 1, 0, Collections.emptyList(), |
| 174 | + Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0); |
| 175 | + InternalAggregations internalAggregations = new InternalAggregations(Collections.singletonList(termsAgg)); |
| 176 | + LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW); |
| 177 | + buckets.add(bucket); |
| 178 | + |
| 179 | + InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, |
| 180 | + () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms")); |
| 181 | + assertThat(e.getMessage(), equalTo("Cannot find an key ['bar'] in [string_terms]")); |
| 182 | + } |
| 183 | +} |
0 commit comments