|
| 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.apache.lucene.document.Document; |
| 23 | +import org.apache.lucene.document.SortedNumericDocValuesField; |
| 24 | +import org.apache.lucene.index.DirectoryReader; |
| 25 | +import org.apache.lucene.index.IndexReader; |
| 26 | +import org.apache.lucene.index.LeafReaderContext; |
| 27 | +import org.apache.lucene.index.IndexReader; |
| 28 | +import org.apache.lucene.index.RandomIndexWriter; |
| 29 | +import org.apache.lucene.search.IndexSearcher; |
| 30 | +import org.apache.lucene.store.Directory; |
| 31 | +import org.elasticsearch.common.breaker.CircuitBreaker; |
| 32 | +import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 33 | +import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; |
| 34 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 35 | +import org.elasticsearch.search.aggregations.AggregatorTestCase; |
| 36 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 37 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 38 | +import org.elasticsearch.search.aggregations.MultiBucketConsumerService; |
| 39 | +import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; |
| 40 | +import org.elasticsearch.search.internal.SearchContext; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | + |
| 44 | +import static org.elasticsearch.search.aggregations.MultiBucketConsumerService.DEFAULT_MAX_BUCKETS; |
| 45 | + |
| 46 | +public class BucketsAggregatorTests extends AggregatorTestCase{ |
| 47 | + |
| 48 | + public BucketsAggregator buildMergeAggregator() throws IOException{ |
| 49 | + try(Directory directory = newDirectory()) { |
| 50 | + try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { |
| 51 | + Document document = new Document(); |
| 52 | + document.add(new SortedNumericDocValuesField("numeric", 0)); |
| 53 | + indexWriter.addDocument(document); |
| 54 | + } |
| 55 | + |
| 56 | + try (IndexReader indexReader = DirectoryReader.open(directory)) { |
| 57 | + IndexSearcher indexSearcher = new IndexSearcher(indexReader); |
| 58 | + |
| 59 | + SearchContext searchContext = createSearchContext( |
| 60 | + indexSearcher, |
| 61 | + createIndexSettings(), |
| 62 | + null, |
| 63 | + new MultiBucketConsumerService.MultiBucketConsumer( |
| 64 | + DEFAULT_MAX_BUCKETS, |
| 65 | + new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST) |
| 66 | + ), |
| 67 | + new NumberFieldMapper.NumberFieldType("test", NumberFieldMapper.NumberType.INTEGER) |
| 68 | + ); |
| 69 | + |
| 70 | + return new BucketsAggregator("test", AggregatorFactories.EMPTY, searchContext, null, null, null) { |
| 71 | + @Override |
| 72 | + protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { |
| 78 | + return new InternalAggregation[0]; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public InternalAggregation buildEmptyAggregation() { |
| 83 | + return null; |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void testBucketMergeNoDelete() throws IOException{ |
| 91 | + BucketsAggregator mergeAggregator = buildMergeAggregator(); |
| 92 | + |
| 93 | + mergeAggregator.grow(10); |
| 94 | + for(int i = 0; i < 10; i++){ |
| 95 | + mergeAggregator.incrementBucketDocCount(i, i); |
| 96 | + } |
| 97 | + |
| 98 | + mergeAggregator.mergeBuckets(10, bucket -> bucket % 5); |
| 99 | + |
| 100 | + for(int i=0; i<5; i++) { |
| 101 | + // The i'th bucket should now have all docs whose index % 5 = i |
| 102 | + // This is buckets i and i + 5 |
| 103 | + // i + (i+5) = 2*i + 5 |
| 104 | + assertEquals(mergeAggregator.getDocCounts().get(i), (2 * i) + 5); |
| 105 | + } |
| 106 | + for(int i=5; i<10; i++){ |
| 107 | + assertEquals(mergeAggregator.getDocCounts().get(i), 0); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void testBucketMergeAndDelete() throws IOException{ |
| 112 | + BucketsAggregator mergeAggregator = buildMergeAggregator(); |
| 113 | + |
| 114 | + mergeAggregator.grow(10); |
| 115 | + int sum = 0; |
| 116 | + for(int i = 0; i < 20; i++){ |
| 117 | + mergeAggregator.incrementBucketDocCount(i, i); |
| 118 | + if(5 <= i && i < 15) { |
| 119 | + sum += i; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Put the buckets in indices 5 ... 14 into bucket 5, and delete the rest of the buckets |
| 124 | + mergeAggregator.mergeBuckets(10, bucket -> (5 <= bucket && bucket < 15) ? 5 : -1); |
| 125 | + |
| 126 | + assertEquals(mergeAggregator.getDocCounts().size(), 10); // Confirm that the 10 other buckets were deleted |
| 127 | + for(int i=0; i<10; i++){ |
| 128 | + assertEquals(mergeAggregator.getDocCounts().get(i), i == 5 ? sum : 0); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments