Skip to content

Commit ce9c5f0

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 ceaa28e commit ce9c5f0

File tree

1 file changed

+30
-48
lines changed

1 file changed

+30
-48
lines changed

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

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,29 @@
4444
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
4545

4646
import java.io.IOException;
47+
import java.util.ArrayList;
48+
import java.util.List;
4749
import java.util.function.Consumer;
4850

4951
import static org.hamcrest.Matchers.greaterThan;
5052

5153
public class DiversifiedSamplerTests extends AggregatorTestCase {
5254

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

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

94+
public void testDiversifiedSampler() throws Exception {
95+
Directory directory = newDirectory();
96+
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
97+
MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre");
98+
writeBooks(indexWriter);
8699
indexWriter.close();
87100
IndexReader indexReader = DirectoryReader.open(directory);
88101
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
89-
90-
MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre");
91102
Consumer<InternalSampler> verify = result -> {
92103
Terms terms = result.getAggregations().get("terms");
93104
assertEquals(2, terms.getBuckets().size());
@@ -114,38 +125,9 @@ public void testDiversifiedSampler() throws Exception {
114125
}
115126

116127
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-
131128
Directory directory = newDirectory();
132129
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-
130+
writeBooks(indexWriter);
149131
indexWriter.close();
150132
IndexReader indexReader = DirectoryReader.open(directory);
151133
IndexSearcher indexSearcher = new IndexSearcher(indexReader);

0 commit comments

Comments
 (0)