From ce9c5f0e465c6a40ba2008a21918fbc8b1d4b87d Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 11 Aug 2020 17:32:13 -0400 Subject: [PATCH] Fix diversified sample tests The test assumed that the aggregator only ran once but we turned that off. This turns it back on. --- .../sampler/DiversifiedSamplerTests.java | 78 +++++++------------ 1 file changed, 30 insertions(+), 48 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java index 15ab2cc21a9..8452e294240 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java @@ -44,29 +44,29 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; import static org.hamcrest.Matchers.greaterThan; public class DiversifiedSamplerTests extends AggregatorTestCase { - public void testDiversifiedSampler() throws Exception { + private void writeBooks(RandomIndexWriter iw) throws IOException { String data[] = { - // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id", - "0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", - "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", - "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", - "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", - "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", - "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", - "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", - "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", - "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", - "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" - }; + // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id", + "0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", + "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", + "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", + "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", + "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", + "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", + "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", + "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", + "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", + "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" }; - Directory directory = newDirectory(); - RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); + List docs = new ArrayList<>(); for (String entry : data) { String[] parts = entry.split(","); Document document = new Document(); @@ -80,14 +80,25 @@ public class DiversifiedSamplerTests extends AggregatorTestCase { document.add(new StringField("sequence", parts[7], Field.Store.NO)); document.add(new SortedDocValuesField("genre", new BytesRef(parts[8]))); document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9]))); - indexWriter.addDocument(document); + docs.add(document); } + /* + * Add all documents at once to force the test to aggregate all + * values together at the same time. *That* is required because + * the tests assume that all books are on a shard together. And + * they aren't always if they end up in separate leaves. + */ + iw.addDocuments(docs); + } + public void testDiversifiedSampler() throws Exception { + Directory directory = newDirectory(); + RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); + MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre"); + writeBooks(indexWriter); indexWriter.close(); IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); - - MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre"); Consumer verify = result -> { Terms terms = result.getAggregations().get("terms"); assertEquals(2, terms.getBuckets().size()); @@ -114,38 +125,9 @@ public class DiversifiedSamplerTests extends AggregatorTestCase { } public void testRidiculousSize() throws Exception { - String[] data = { - // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id", - "0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", - "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", - "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", - "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", - "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", - "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", - "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", - "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", - "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", - "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" - }; - Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); - for (String entry : data) { - String[] parts = entry.split(","); - Document document = new Document(); - document.add(new SortedDocValuesField("id", new BytesRef(parts[0]))); - document.add(new StringField("cat", parts[1], Field.Store.NO)); - document.add(new TextField("name", parts[2], Field.Store.NO)); - document.add(new DoubleDocValuesField("price", Double.valueOf(parts[3]))); - document.add(new StringField("inStock", parts[4], Field.Store.NO)); - document.add(new StringField("author", parts[5], Field.Store.NO)); - document.add(new StringField("series", parts[6], Field.Store.NO)); - document.add(new StringField("sequence", parts[7], Field.Store.NO)); - document.add(new SortedDocValuesField("genre", new BytesRef(parts[8]))); - document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9]))); - indexWriter.addDocument(document); - } - + writeBooks(indexWriter); indexWriter.close(); IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader);