Skip to content

Commit 9ac7f8d

Browse files
committed
Fix diversified sample tests
The test assumed that the aggregator only ran once but we turned that off. This turns it back on.
1 parent 9059cfb commit 9ac7f8d

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.bucket.sampler;
2020

21+
import com.carrotsearch.randomizedtesting.annotations.Repeat;
22+
2123
import org.apache.lucene.document.Document;
2224
import org.apache.lucene.document.DoubleDocValuesField;
2325
import org.apache.lucene.document.Field;
@@ -44,29 +46,30 @@
4446
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
4547

4648
import java.io.IOException;
49+
import java.util.ArrayList;
50+
import java.util.List;
4751
import java.util.function.Consumer;
4852

4953
import static org.hamcrest.Matchers.greaterThan;
5054

55+
@Repeat(iterations=1000)
5156
public class DiversifiedSamplerTests extends AggregatorTestCase {
5257

53-
public void testDiversifiedSampler() throws Exception {
58+
private void writeBooks(RandomIndexWriter iw) throws IOException {
5459
String data[] = {
55-
// "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
56-
"0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0",
57-
"0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0",
58-
"055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0",
59-
"0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1",
60-
"0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0",
61-
"0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1",
62-
"0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0",
63-
"0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0",
64-
"0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0",
65-
"080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0"
66-
};
60+
// "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
61+
"0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0",
62+
"0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0",
63+
"055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0",
64+
"0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1",
65+
"0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0",
66+
"0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1",
67+
"0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0",
68+
"0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0",
69+
"0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0",
70+
"080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" };
6771

68-
Directory directory = newDirectory();
69-
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
72+
List<Document> docs = new ArrayList<>();
7073
for (String entry : data) {
7174
String[] parts = entry.split(",");
7275
Document document = new Document();
@@ -80,14 +83,25 @@ public void testDiversifiedSampler() throws Exception {
8083
document.add(new StringField("sequence", parts[7], Field.Store.NO));
8184
document.add(new SortedDocValuesField("genre", new BytesRef(parts[8])));
8285
document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9])));
83-
indexWriter.addDocument(document);
86+
docs.add(document);
8487
}
88+
/*
89+
* Add all documents at once to force the test to aggregate all
90+
* values together at the same time. *That* is required because
91+
* the tests assume that all books are on a shard together. And
92+
* they aren't always if they end up in separate leaves.
93+
*/
94+
iw.addDocuments(docs);
95+
}
8596

97+
public void testDiversifiedSampler() throws Exception {
98+
Directory directory = newDirectory();
99+
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
100+
MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre");
101+
writeBooks(indexWriter);
86102
indexWriter.close();
87103
IndexReader indexReader = DirectoryReader.open(directory);
88104
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
89-
90-
MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre");
91105
Consumer<InternalSampler> verify = result -> {
92106
Terms terms = result.getAggregations().get("terms");
93107
assertEquals(2, terms.getBuckets().size());
@@ -114,38 +128,9 @@ public void testDiversifiedSampler() throws Exception {
114128
}
115129

116130
public void testRidiculousSize() throws Exception {
117-
String[] data = {
118-
// "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
119-
"0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0",
120-
"0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0",
121-
"055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0",
122-
"0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1",
123-
"0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0",
124-
"0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1",
125-
"0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0",
126-
"0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0",
127-
"0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0",
128-
"080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0"
129-
};
130-
131131
Directory directory = newDirectory();
132132
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
133-
for (String entry : data) {
134-
String[] parts = entry.split(",");
135-
Document document = new Document();
136-
document.add(new SortedDocValuesField("id", new BytesRef(parts[0])));
137-
document.add(new StringField("cat", parts[1], Field.Store.NO));
138-
document.add(new TextField("name", parts[2], Field.Store.NO));
139-
document.add(new DoubleDocValuesField("price", Double.valueOf(parts[3])));
140-
document.add(new StringField("inStock", parts[4], Field.Store.NO));
141-
document.add(new StringField("author", parts[5], Field.Store.NO));
142-
document.add(new StringField("series", parts[6], Field.Store.NO));
143-
document.add(new StringField("sequence", parts[7], Field.Store.NO));
144-
document.add(new SortedDocValuesField("genre", new BytesRef(parts[8])));
145-
document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9])));
146-
indexWriter.addDocument(document);
147-
}
148-
133+
writeBooks(indexWriter);
149134
indexWriter.close();
150135
IndexReader indexReader = DirectoryReader.open(directory);
151136
IndexSearcher indexSearcher = new IndexSearcher(indexReader);

0 commit comments

Comments
 (0)