From 12982e98df3220d72ad0f260ac70d77b70b4cc89 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Mon, 23 Jan 2017 16:24:55 +0100 Subject: [PATCH] Add unit tests for ValueCountAggregator and InternalValueCount (#22741) Adds unit tests for the value count aggregator. Relates #22278 --- .../valuecount/ValueCountAggregatorTests.java | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 core/src/test/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorTests.java diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorTests.java new file mode 100644 index 0000000000000..bc60524b28d8b --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregatorTests.java @@ -0,0 +1,156 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.metrics.valuecount; + +import org.apache.lucene.document.IntPoint; +import org.apache.lucene.document.NumericDocValuesField; +import org.apache.lucene.document.SortedDocValuesField; +import org.apache.lucene.document.SortedNumericDocValuesField; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.search.FieldValueQuery; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.CheckedConsumer; +import org.elasticsearch.index.mapper.BooleanFieldMapper; +import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.index.mapper.IpFieldMapper; +import org.elasticsearch.index.mapper.KeywordFieldMapper; +import org.elasticsearch.index.mapper.LatLonPointFieldMapper; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.search.aggregations.AggregatorTestCase; +import org.elasticsearch.search.aggregations.support.ValueType; + +import java.io.IOException; +import java.util.Arrays; +import java.util.function.Consumer; + +import static java.util.Collections.singleton; + +public class ValueCountAggregatorTests extends AggregatorTestCase { + + private static final String FIELD_NAME = "field"; + + public void testNoDocs() throws IOException { + for (ValueType valueType : ValueType.values()) { + testCase(new MatchAllDocsQuery(), valueType, iw -> { + // Intentionally not writing any docs + }, count -> assertEquals(0L, count.getValue())); + } + } + + public void testNoMatchingField() throws IOException { + testCase(new MatchAllDocsQuery(), ValueType.LONG, iw -> { + iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); + iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 1))); + }, count -> assertEquals(0L, count.getValue())); + } + + public void testSomeMatchesSortedNumericDocValues() throws IOException { + testCase(new FieldValueQuery(FIELD_NAME), ValueType.NUMERIC, iw -> { + iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7))); + iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 7))); + iw.addDocument(singleton(new SortedNumericDocValuesField(FIELD_NAME, 1))); + }, count -> assertEquals(2L, count.getValue())); + } + + public void testSomeMatchesNumericDocValues() throws IOException { + testCase(new FieldValueQuery(FIELD_NAME), ValueType.NUMBER, iw -> { + iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7))); + iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 1))); + }, count -> assertEquals(2L, count.getValue())); + } + + public void testQueryFiltering() throws IOException { + testCase(IntPoint.newRangeQuery("level", 0, 5), ValueType.STRING, iw -> { + iw.addDocument(Arrays.asList(new IntPoint("level", 0), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); + iw.addDocument(Arrays.asList(new IntPoint("level", 1), new SortedDocValuesField(FIELD_NAME, new BytesRef("bar")))); + iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); + iw.addDocument(Arrays.asList(new IntPoint("level", 5), new SortedDocValuesField(FIELD_NAME, new BytesRef("baz")))); + iw.addDocument(Arrays.asList(new IntPoint("level", 7), new SortedDocValuesField(FIELD_NAME, new BytesRef("baz")))); + }, count -> assertEquals(4L, count.getValue())); + } + + public void testQueryFiltersAll() throws IOException { + testCase(IntPoint.newRangeQuery("level", -1, 0), ValueType.STRING, iw -> { + iw.addDocument(Arrays.asList(new IntPoint("level", 3), new SortedDocValuesField(FIELD_NAME, new BytesRef("foo")))); + iw.addDocument(Arrays.asList(new IntPoint("level", 5), new SortedDocValuesField(FIELD_NAME, new BytesRef("baz")))); + }, count -> assertEquals(0L, count.getValue())); + } + + private void testCase(Query query, + ValueType valueType, + CheckedConsumer indexer, + Consumer verify) throws IOException { + + try (Directory directory = newDirectory()) { + try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { + indexer.accept(indexWriter); + } + + try (IndexReader indexReader = DirectoryReader.open(directory)) { + IndexSearcher indexSearcher = newSearcher(indexReader, true, true); + + MappedFieldType fieldType = createMappedFieldType(valueType); + fieldType.setName(FIELD_NAME); + fieldType.setHasDocValues(true); + + ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name", valueType); + aggregationBuilder.field(FIELD_NAME); + + try (ValueCountAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) { + aggregator.preCollection(); + indexSearcher.search(query, aggregator); + aggregator.postCollection(); + + verify.accept((ValueCount) aggregator.buildAggregation(0L)); + } + } + } + } + + private static MappedFieldType createMappedFieldType(ValueType valueType) { + switch (valueType) { + case BOOLEAN: + return new BooleanFieldMapper.BooleanFieldType(); + case STRING: + return new KeywordFieldMapper.KeywordFieldType(); + case DOUBLE: + return new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + case NUMBER: + case NUMERIC: + case LONG: + return new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + case DATE: + return new DateFieldMapper.Builder("_name").fieldType(); + case IP: + return new IpFieldMapper.Builder("_name").fieldType(); + case GEOPOINT: + return new LatLonPointFieldMapper.Builder("_name").fieldType(); + default: + throw new IllegalArgumentException("Test does not support value type [" + valueType + "]"); + } + } +}