18
18
*/
19
19
package org .elasticsearch .search .aggregations .bucket .sampler ;
20
20
21
+ import com .carrotsearch .randomizedtesting .annotations .Repeat ;
22
+
21
23
import org .apache .lucene .document .Document ;
22
24
import org .apache .lucene .document .DoubleDocValuesField ;
23
25
import org .apache .lucene .document .Field ;
44
46
import org .elasticsearch .search .aggregations .bucket .terms .TermsAggregationBuilder ;
45
47
46
48
import java .io .IOException ;
49
+ import java .util .ArrayList ;
50
+ import java .util .List ;
47
51
import java .util .function .Consumer ;
48
52
49
53
import static org .hamcrest .Matchers .greaterThan ;
50
54
55
+ @ Repeat (iterations =1000 )
51
56
public class DiversifiedSamplerTests extends AggregatorTestCase {
52
57
53
- public void testDiversifiedSampler ( ) throws Exception {
58
+ private void writeBooks ( RandomIndexWriter iw ) throws IOException {
54
59
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" };
67
71
68
- Directory directory = newDirectory ();
69
- RandomIndexWriter indexWriter = new RandomIndexWriter (random (), directory );
72
+ List <Document > docs = new ArrayList <>();
70
73
for (String entry : data ) {
71
74
String [] parts = entry .split ("," );
72
75
Document document = new Document ();
@@ -80,14 +83,25 @@ public void testDiversifiedSampler() throws Exception {
80
83
document .add (new StringField ("sequence" , parts [7 ], Field .Store .NO ));
81
84
document .add (new SortedDocValuesField ("genre" , new BytesRef (parts [8 ])));
82
85
document .add (new NumericDocValuesField ("genre_id" , Long .valueOf (parts [9 ])));
83
- indexWriter . addDocument (document );
86
+ docs . add (document );
84
87
}
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
+ }
85
96
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 );
86
102
indexWriter .close ();
87
103
IndexReader indexReader = DirectoryReader .open (directory );
88
104
IndexSearcher indexSearcher = new IndexSearcher (indexReader );
89
-
90
- MappedFieldType genreFieldType = new KeywordFieldMapper .KeywordFieldType ("genre" );
91
105
Consumer <InternalSampler > verify = result -> {
92
106
Terms terms = result .getAggregations ().get ("terms" );
93
107
assertEquals (2 , terms .getBuckets ().size ());
@@ -114,38 +128,9 @@ public void testDiversifiedSampler() throws Exception {
114
128
}
115
129
116
130
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
-
131
131
Directory directory = newDirectory ();
132
132
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 );
149
134
indexWriter .close ();
150
135
IndexReader indexReader = DirectoryReader .open (directory );
151
136
IndexSearcher indexSearcher = new IndexSearcher (indexReader );
0 commit comments