From 970a0c8957fa39eedc730677a412f80fa9f75500 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Thu, 30 Jul 2020 08:56:21 -0400 Subject: [PATCH] [7.x] Aggregation tests for Wildcard Field (#58507) (#60423) --- .../aggregations/AggregatorTestCase.java | 10 +- .../mapper/WildcardFieldAggregationTests.java | 155 ++++++++++++++++++ 2 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index fb0c1a50b08..7ba59c124f7 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -552,11 +552,11 @@ public abstract class AggregatorTestCase extends ESTestCase { } protected void testCase( - T aggregationBuilder, - Query query, - CheckedConsumer buildIndex, - Consumer verify, - MappedFieldType... fieldTypes) throws IOException { + T aggregationBuilder, + Query query, + CheckedConsumer buildIndex, + Consumer verify, + MappedFieldType... fieldTypes) throws IOException { try (Directory directory = newDirectory()) { RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); buildIndex.accept(indexWriter); diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java new file mode 100644 index 00000000000..5b232d73d47 --- /dev/null +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java @@ -0,0 +1,155 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +package org.elasticsearch.xpack.wildcard.mapper; + +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.elasticsearch.index.mapper.ContentPath; +import org.elasticsearch.index.mapper.Mapper; +import org.elasticsearch.index.mapper.ParseContext; +import org.elasticsearch.search.aggregations.AggregatorTestCase; +import org.elasticsearch.search.aggregations.BucketOrder; +import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.composite.InternalComposite; +import org.elasticsearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder; +import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; +import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; +import org.junit.Before; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; + +public class WildcardFieldAggregationTests extends AggregatorTestCase { + private static final String WILDCARD_FIELD_NAME = "wildcard_field"; + private static final int MAX_FIELD_LENGTH = 30; + + private WildcardFieldMapper wildcardFieldMapper; + private WildcardFieldMapper.WildcardFieldType wildcardFieldType; + + + @Before + public void setup() { + WildcardFieldMapper.Builder builder = new WildcardFieldMapper.Builder(WILDCARD_FIELD_NAME); + builder.ignoreAbove(MAX_FIELD_LENGTH); + wildcardFieldMapper = builder.build(new Mapper.BuilderContext(createIndexSettings().getSettings(), new ContentPath(0))); + + wildcardFieldType = wildcardFieldMapper.fieldType(); + } + + private void addFields(ParseContext.Document parseDoc, Document doc, String docContent) throws IOException { + ArrayList fields = new ArrayList<>(); + wildcardFieldMapper.createFields(docContent, parseDoc, fields); + + for (IndexableField indexableField : fields) { + doc.add(indexableField); + } + } + + private void indexDoc(ParseContext.Document parseDoc, Document doc, RandomIndexWriter iw) throws IOException { + IndexableField field = parseDoc.getByKey(wildcardFieldMapper.name()); + if (field != null) { + doc.add(field); + } + iw.addDocument(doc); + } + + private void indexStrings(RandomIndexWriter iw, String... values) throws IOException { + Document doc = new Document(); + ParseContext.Document parseDoc = new ParseContext.Document(); + for (String value : values) { + addFields(parseDoc, doc, value); + } + indexDoc(parseDoc, doc, iw); + } + + public void testTermsAggregation() throws IOException { + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") + .field(WILDCARD_FIELD_NAME) + .order(BucketOrder.key(true)); + + testCase(aggregationBuilder, + new MatchAllDocsQuery(), + iw -> { + indexStrings(iw, "a"); + indexStrings(iw, "a"); + indexStrings(iw, "b"); + indexStrings(iw, "b"); + indexStrings(iw, "b"); + indexStrings(iw, "c"); + }, + (StringTerms result) -> { + assertTrue(AggregationInspectionHelper.hasValue(result)); + + assertEquals(3, result.getBuckets().size()); + assertEquals("a", result.getBuckets().get(0).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(0).getDocCount()); + assertEquals("b", result.getBuckets().get(1).getKeyAsString()); + assertEquals(3L, result.getBuckets().get(1).getDocCount()); + assertEquals("c", result.getBuckets().get(2).getKeyAsString()); + assertEquals(1L, result.getBuckets().get(2).getDocCount()); + }, + wildcardFieldType); + } + + public void testCompositeTermsAggregation() throws IOException { + CompositeAggregationBuilder aggregationBuilder = new CompositeAggregationBuilder( + "name", + Collections.singletonList( + new TermsValuesSourceBuilder("terms_key").field(WILDCARD_FIELD_NAME) + ) + ); + + testCase(aggregationBuilder, + new MatchAllDocsQuery(), + iw -> { + indexStrings(iw, "a"); + indexStrings(iw, "c"); + indexStrings(iw, "a"); + indexStrings(iw, "d"); + indexStrings(iw, "c"); + }, + (InternalComposite result) -> { + assertTrue(AggregationInspectionHelper.hasValue(result)); + + assertEquals(3, result.getBuckets().size()); + assertEquals("{terms_key=d}", result.afterKey().toString()); + assertEquals("{terms_key=a}", result.getBuckets().get(0).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(0).getDocCount()); + assertEquals("{terms_key=c}", result.getBuckets().get(1).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(1).getDocCount()); + assertEquals("{terms_key=d}", result.getBuckets().get(2).getKeyAsString()); + assertEquals(1L, result.getBuckets().get(2).getDocCount()); + }, + wildcardFieldType); + } + + public void testCompositeTermsSearchAfter() throws IOException { + + TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("terms_key").field(WILDCARD_FIELD_NAME); + CompositeAggregationBuilder aggregationBuilder = new CompositeAggregationBuilder("name", Collections.singletonList(terms)) + .aggregateAfter(Collections.singletonMap("terms_key", "a")); + + testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + indexStrings(iw, "a"); + indexStrings(iw, "c"); + indexStrings(iw, "a"); + indexStrings(iw, "d"); + indexStrings(iw, "c"); + }, (InternalComposite result) -> { + assertEquals(2, result.getBuckets().size()); + assertEquals("{terms_key=d}", result.afterKey().toString()); + assertEquals("{terms_key=c}", result.getBuckets().get(0).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(0).getDocCount()); + assertEquals("{terms_key=d}", result.getBuckets().get(1).getKeyAsString()); + assertEquals(1L, result.getBuckets().get(1).getDocCount()); + }, wildcardFieldType); + } +}