|
| 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.metrics.avg; |
| 21 | + |
| 22 | +import org.apache.lucene.document.IntPoint; |
| 23 | +import org.apache.lucene.document.NumericDocValuesField; |
| 24 | +import org.apache.lucene.document.SortedNumericDocValuesField; |
| 25 | +import org.apache.lucene.index.DirectoryReader; |
| 26 | +import org.apache.lucene.index.IndexReader; |
| 27 | +import org.apache.lucene.index.RandomIndexWriter; |
| 28 | +import org.apache.lucene.search.FieldValueQuery; |
| 29 | +import org.apache.lucene.search.IndexSearcher; |
| 30 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 31 | +import org.apache.lucene.search.Query; |
| 32 | +import org.apache.lucene.store.Directory; |
| 33 | +import org.elasticsearch.common.CheckedConsumer; |
| 34 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 35 | +import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 36 | +import org.elasticsearch.search.aggregations.AggregatorTestCase; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.function.Consumer; |
| 41 | + |
| 42 | +import static java.util.Collections.singleton; |
| 43 | + |
| 44 | +public class AvgAggregatorTests extends AggregatorTestCase { |
| 45 | + |
| 46 | + public void testNoDocs() throws IOException { |
| 47 | + testCase(new MatchAllDocsQuery(), iw -> { |
| 48 | + // Intentionally not writing any docs |
| 49 | + }, avg -> { |
| 50 | + assertEquals(Double.NaN, avg.getValue(), 0); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + public void testNoMatchingField() throws IOException { |
| 55 | + testCase(new MatchAllDocsQuery(), iw -> { |
| 56 | + iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); |
| 57 | + iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 3))); |
| 58 | + }, avg -> { |
| 59 | + assertEquals(Double.NaN, avg.getValue(), 0); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + public void testSomeMatchesSortedNumericDocValues() throws IOException { |
| 64 | + testCase(new FieldValueQuery("number"), iw -> { |
| 65 | + iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7))); |
| 66 | + iw.addDocument(singleton(new SortedNumericDocValuesField("number", 2))); |
| 67 | + iw.addDocument(singleton(new SortedNumericDocValuesField("number", 3))); |
| 68 | + }, avg -> { |
| 69 | + assertEquals(4, avg.getValue(), 0); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + public void testSomeMatchesNumericDocValues() throws IOException { |
| 74 | + testCase(new FieldValueQuery("number"), iw -> { |
| 75 | + iw.addDocument(singleton(new NumericDocValuesField("number", 7))); |
| 76 | + iw.addDocument(singleton(new NumericDocValuesField("number", 2))); |
| 77 | + iw.addDocument(singleton(new NumericDocValuesField("number", 3))); |
| 78 | + }, avg -> { |
| 79 | + assertEquals(4, avg.getValue(), 0); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public void testQueryFiltering() throws IOException { |
| 84 | + testCase(IntPoint.newRangeQuery("number", 0, 3), iw -> { |
| 85 | + iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); |
| 86 | + iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2))); |
| 87 | + iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 3))); |
| 88 | + }, avg -> { |
| 89 | + assertEquals(2.5, avg.getValue(), 0); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public void testQueryFiltersAll() throws IOException { |
| 94 | + testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> { |
| 95 | + iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7))); |
| 96 | + iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2))); |
| 97 | + iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 7))); |
| 98 | + }, avg -> { |
| 99 | + assertEquals(Double.NaN, avg.getValue(), 0); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalAvg> verify) |
| 104 | + throws IOException { |
| 105 | + Directory directory = newDirectory(); |
| 106 | + RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); |
| 107 | + buildIndex.accept(indexWriter); |
| 108 | + indexWriter.close(); |
| 109 | + |
| 110 | + IndexReader indexReader = DirectoryReader.open(directory); |
| 111 | + IndexSearcher indexSearcher = newSearcher(indexReader, true, true); |
| 112 | + |
| 113 | + AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); |
| 114 | + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); |
| 115 | + fieldType.setName("number"); |
| 116 | + try (AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) { |
| 117 | + aggregator.preCollection(); |
| 118 | + indexSearcher.search(query, aggregator); |
| 119 | + aggregator.postCollection(); |
| 120 | + verify.accept((InternalAvg) aggregator.buildAggregation(0L)); |
| 121 | + } |
| 122 | + indexReader.close(); |
| 123 | + directory.close(); |
| 124 | + } |
| 125 | +} |
0 commit comments