21
21
22
22
import org .elasticsearch .action .admin .indices .alias .Alias ;
23
23
import org .elasticsearch .action .admin .indices .forcemerge .ForceMergeResponse ;
24
+ import org .elasticsearch .action .bulk .BulkRequestBuilder ;
25
+ import org .elasticsearch .action .bulk .BulkResponse ;
24
26
import org .elasticsearch .action .search .SearchResponse ;
25
27
import org .elasticsearch .action .search .SearchType ;
26
28
import org .elasticsearch .client .Client ;
41
43
import java .util .Arrays ;
42
44
import java .util .List ;
43
45
46
+ import static org .elasticsearch .index .IndexSettings .INDEX_REFRESH_INTERVAL_SETTING ;
44
47
import static org .elasticsearch .search .aggregations .AggregationBuilders .dateHistogram ;
45
48
import static org .elasticsearch .search .aggregations .AggregationBuilders .dateRange ;
46
49
import static org .elasticsearch .search .aggregations .AggregationBuilders .filter ;
@@ -185,29 +188,27 @@ public void testQueryRewriteMissingValues() throws Exception {
185
188
assertCacheState (client , "index" , 2 , 1 );
186
189
}
187
190
188
- public void testQueryRewriteDates () throws Exception {
191
+ public void testQueryRewriteDates () {
189
192
Client client = client ();
190
193
assertAcked (client .admin ().indices ().prepareCreate ("index" ).addMapping ("type" , "d" , "type=date" )
191
194
.setSettings (Settings .builder ().put (IndicesRequestCache .INDEX_CACHE_REQUEST_ENABLED_SETTING .getKey (), true )
192
- .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )).get ());
193
- indexRandom (true , client .prepareIndex ("index" , "type" , "1" ).setSource ("d" , "2014-01-01T00:00:00" ),
194
- client .prepareIndex ("index" , "type" , "2" ).setSource ("d" , "2014-02-01T00:00:00" ),
195
- client .prepareIndex ("index" , "type" , "3" ).setSource ("d" , "2014-03-01T00:00:00" ),
196
- client .prepareIndex ("index" , "type" , "4" ).setSource ("d" , "2014-04-01T00:00:00" ),
197
- client .prepareIndex ("index" , "type" , "5" ).setSource ("d" , "2014-05-01T00:00:00" ),
198
- client .prepareIndex ("index" , "type" , "6" ).setSource ("d" , "2014-06-01T00:00:00" ),
199
- client .prepareIndex ("index" , "type" , "7" ).setSource ("d" , "2014-07-01T00:00:00" ),
200
- client .prepareIndex ("index" , "type" , "8" ).setSource ("d" , "2014-08-01T00:00:00" ),
201
- client .prepareIndex ("index" , "type" , "9" ).setSource ("d" , "2014-09-01T00:00:00" ));
202
- ensureSearchable ("index" );
203
- assertCacheState (client , "index" , 0 , 0 );
195
+ .put (INDEX_REFRESH_INTERVAL_SETTING .getKey (), "-1" ) // disable automatic refreshes
196
+ .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )).get ());
204
197
205
- // Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
206
- ForceMergeResponse forceMergeResponse = client .admin ().indices ().prepareForceMerge ("index" ).setFlush (true ).get ();
207
- ElasticsearchAssertions .assertAllSuccessful (forceMergeResponse );
208
- refresh ();
198
+ BulkRequestBuilder bulkBuilder = client ().prepareBulk ();
199
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "1" ).setSource ("d" , "2014-01-01T00:00:00" ));
200
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "2" ).setSource ("d" , "2014-02-01T00:00:00" ));
201
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "3" ).setSource ("d" , "2014-03-01T00:00:00" ));
202
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "4" ).setSource ("d" , "2014-04-01T00:00:00" ));
203
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "5" ).setSource ("d" , "2014-05-01T00:00:00" ));
204
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "6" ).setSource ("d" , "2014-06-01T00:00:00" ));
205
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "7" ).setSource ("d" , "2014-07-01T00:00:00" ));
206
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "8" ).setSource ("d" , "2014-08-01T00:00:00" ));
207
+ bulkBuilder .add (client .prepareIndex ("index" , "type" , "9" ).setSource ("d" , "2014-09-01T00:00:00" ));
208
+ BulkResponse bulkResponse = bulkBuilder .execute ().actionGet ();
209
+ assertThat (bulkResponse .hasFailures () ? bulkResponse .buildFailureMessage () : "" , bulkResponse .hasFailures (), equalTo (false ));
209
210
ensureSearchable ("index" );
210
-
211
+ refresh ( "index" );
211
212
assertCacheState (client , "index" , 0 , 0 );
212
213
213
214
final SearchResponse r1 = client .prepareSearch ("index" ).setSearchType (SearchType .QUERY_THEN_FETCH ).setSize (0 )
@@ -232,9 +233,10 @@ public void testQueryRewriteDates() throws Exception {
232
233
assertCacheState (client , "index" , 2 , 1 );
233
234
}
234
235
235
- public void testQueryRewriteDatesWithNow () throws Exception {
236
+ public void testQueryRewriteDatesWithNow () {
236
237
Client client = client ();
237
238
Settings settings = Settings .builder ().put (IndicesRequestCache .INDEX_CACHE_REQUEST_ENABLED_SETTING .getKey (), true )
239
+ .put (INDEX_REFRESH_INTERVAL_SETTING .getKey (), "-1" ) // disable automatic refreshes
238
240
.put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).build ();
239
241
assertAcked (client .admin ().indices ().prepareCreate ("index-1" ).addMapping ("type" , "d" , "type=date" )
240
242
.setSettings (settings ).get ());
@@ -243,26 +245,21 @@ public void testQueryRewriteDatesWithNow() throws Exception {
243
245
assertAcked (client .admin ().indices ().prepareCreate ("index-3" ).addMapping ("type" , "d" , "type=date" )
244
246
.setSettings (settings ).get ());
245
247
ZonedDateTime now = ZonedDateTime .now (ZoneOffset .UTC );
246
- indexRandom (true , client .prepareIndex ("index-1" , "type" , "1" ).setSource ("d" , now ),
247
- client .prepareIndex ("index-1" , "type" , "2" ).setSource ("d" , now .minusDays (1 )),
248
- client .prepareIndex ("index-1" , "type" , "3" ).setSource ("d" , now .minusDays (2 )),
249
- client .prepareIndex ("index-2" , "type" , "4" ).setSource ("d" , now .minusDays (3 )),
250
- client .prepareIndex ("index-2" , "type" , "5" ).setSource ("d" , now .minusDays (4 )),
251
- client .prepareIndex ("index-2" , "type" , "6" ).setSource ("d" , now .minusDays (5 )),
252
- client .prepareIndex ("index-3" , "type" , "7" ).setSource ("d" , now .minusDays (6 )),
253
- client .prepareIndex ("index-3" , "type" , "8" ).setSource ("d" , now .minusDays (7 )),
254
- client .prepareIndex ("index-3" , "type" , "9" ).setSource ("d" , now .minusDays (8 )));
255
- ensureSearchable ("index-1" , "index-2" , "index-3" );
256
- assertCacheState (client , "index-1" , 0 , 0 );
257
- assertCacheState (client , "index-2" , 0 , 0 );
258
- assertCacheState (client , "index-3" , 0 , 0 );
259
248
260
- // Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
261
- ForceMergeResponse forceMergeResponse = client .admin ().indices ().prepareForceMerge ("index-1" , "index-2" , "index-3" ).setFlush (true )
262
- .get ();
263
- ElasticsearchAssertions .assertAllSuccessful (forceMergeResponse );
264
- refresh ();
249
+ BulkRequestBuilder bulkBuilder = client ().prepareBulk ();
250
+ bulkBuilder .add (client .prepareIndex ("index-1" , "type" , "1" ).setSource ("d" , now ));
251
+ bulkBuilder .add (client .prepareIndex ("index-1" , "type" , "2" ).setSource ("d" , now .minusDays (1 )));
252
+ bulkBuilder .add (client .prepareIndex ("index-1" , "type" , "3" ).setSource ("d" , now .minusDays (2 )));
253
+ bulkBuilder .add (client .prepareIndex ("index-2" , "type" , "4" ).setSource ("d" , now .minusDays (3 )));
254
+ bulkBuilder .add (client .prepareIndex ("index-2" , "type" , "5" ).setSource ("d" , now .minusDays (4 )));
255
+ bulkBuilder .add (client .prepareIndex ("index-2" , "type" , "6" ).setSource ("d" , now .minusDays (5 )));
256
+ bulkBuilder .add (client .prepareIndex ("index-3" , "type" , "7" ).setSource ("d" , now .minusDays (6 )));
257
+ bulkBuilder .add (client .prepareIndex ("index-3" , "type" , "8" ).setSource ("d" , now .minusDays (7 )));
258
+ bulkBuilder .add (client .prepareIndex ("index-3" , "type" , "9" ).setSource ("d" , now .minusDays (8 )));
259
+ BulkResponse bulkResponse = bulkBuilder .execute ().actionGet ();
260
+ assertThat (bulkResponse .hasFailures () ? bulkResponse .buildFailureMessage () : "" , bulkResponse .hasFailures (), equalTo (false ));
265
261
ensureSearchable ("index-1" , "index-2" , "index-3" );
262
+ refresh ("index-1" , "index-2" , "index-3" );
266
263
267
264
assertCacheState (client , "index-1" , 0 , 0 );
268
265
assertCacheState (client , "index-2" , 0 , 0 );
0 commit comments