|
23 | 23 | import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
24 | 24 | import org.elasticsearch.action.bulk.BulkItemResponse;
|
25 | 25 | import org.elasticsearch.action.count.CountResponse;
|
| 26 | +import org.elasticsearch.action.search.SearchResponse; |
26 | 27 | import org.elasticsearch.action.updatebyquery.BulkResponseOption;
|
27 | 28 | import org.elasticsearch.action.updatebyquery.IndexUpdateByQueryResponse;
|
28 | 29 | import org.elasticsearch.action.updatebyquery.UpdateByQueryResponse;
|
|
32 | 33 | import org.elasticsearch.common.settings.Settings;
|
33 | 34 | import org.elasticsearch.common.xcontent.XContentFactory;
|
34 | 35 | import org.elasticsearch.index.query.FilterBuilders;
|
| 36 | +import org.elasticsearch.search.SearchHit; |
35 | 37 | import org.elasticsearch.test.integration.AbstractNodesTests;
|
36 | 38 | import org.testng.annotations.AfterClass;
|
37 | 39 | import org.testng.annotations.BeforeClass;
|
@@ -314,4 +316,81 @@ public void testUpdateByQuery_usingAliases() {
|
314 | 316 | assertThat(client.prepareGet("alias1", "type1", "4").execute().actionGet().isExists(), equalTo(false));
|
315 | 317 | }
|
316 | 318 |
|
| 319 | + @Test |
| 320 | + public void testUpdateByQuery_fields() throws Exception { |
| 321 | + createIndex("test"); |
| 322 | + ClusterHealthResponse clusterHealth = client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); |
| 323 | + assertThat(clusterHealth.isTimedOut(), equalTo(false)); |
| 324 | + assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); |
| 325 | + |
| 326 | + long timestamp = System.currentTimeMillis(); |
| 327 | + client.prepareIndex() |
| 328 | + .setIndex("test") |
| 329 | + .setType("type1") |
| 330 | + .setId("id1") |
| 331 | + .setRouting("routing1") |
| 332 | + .setTimestamp(String.valueOf(timestamp)) |
| 333 | + .setTTL(111211211) |
| 334 | + .setSource("field1", 1, "content", "foo") |
| 335 | + .execute().actionGet(); |
| 336 | + client.admin().indices().prepareRefresh("test").setWaitForOperations(true).execute().actionGet(); |
| 337 | + |
| 338 | + CountResponse countResponse = client.prepareCount("test") |
| 339 | + .setQuery(termQuery("field1", 1).buildAsBytes()) |
| 340 | + .execute() |
| 341 | + .actionGet(); |
| 342 | + assertThat(countResponse.getCount(), equalTo(1L)); |
| 343 | + |
| 344 | + countResponse = client.prepareCount("test") |
| 345 | + .setQuery(termQuery("field1", 2).buildAsBytes()) |
| 346 | + .execute() |
| 347 | + .actionGet(); |
| 348 | + assertThat(countResponse.getCount(), equalTo(0L)); |
| 349 | + |
| 350 | + Map<String, Object> scriptParams = new HashMap<String, Object>(); |
| 351 | + scriptParams.put("delim", "_"); |
| 352 | + UpdateByQueryResponse response = updateByQueryClientWrapper.prepareUpdateByQuery() |
| 353 | + .setIndices("test") |
| 354 | + .setTypes("type1") |
| 355 | + .setIncludeBulkResponses(BulkResponseOption.ALL) |
| 356 | + .setScript("ctx._source.field1 += 1;\n"+ |
| 357 | + "ctx._source.content = ctx._index" + |
| 358 | + " + delim + ctx._type" + |
| 359 | + " + delim + ctx._id" + |
| 360 | + " + delim + ctx._uid" + |
| 361 | + " + delim + ctx._parent" + |
| 362 | + " + delim + ctx._routing" + |
| 363 | + " + delim + ctx._timestamp" + |
| 364 | + " + delim + ctx._ttl" + |
| 365 | + " + delim + ctx._version" + |
| 366 | + " + delim + ctx._source.content;") |
| 367 | + .setScriptParams(scriptParams) |
| 368 | + .setQuery(matchAllQuery()) |
| 369 | + .execute() |
| 370 | + .actionGet(); |
| 371 | + |
| 372 | + assertThat(response, notNullValue()); |
| 373 | + assertThat(response.mainFailures().length, equalTo(0)); |
| 374 | + assertThat(response.totalHits(), equalTo(1L)); |
| 375 | + assertThat(response.updated(), equalTo(1L)); |
| 376 | + assertThat(response.indexResponses().length, equalTo(1)); |
| 377 | + assertThat(response.indexResponses()[0].countShardResponses(), equalTo(1L)); |
| 378 | + assertThat(response.indexResponses()[0].failuresByShard().isEmpty(), equalTo(true)); |
| 379 | + |
| 380 | + client.admin().indices().prepareRefresh("test").execute().actionGet(); |
| 381 | + countResponse = client.prepareCount("test") |
| 382 | + .setQuery(termQuery("field1", 2).buildAsBytes()) |
| 383 | + .execute() |
| 384 | + .actionGet(); |
| 385 | + assertThat(countResponse.getCount(), equalTo(1L)); |
| 386 | + |
| 387 | + SearchResponse searchResponse = client.prepareSearch("test").setQuery(matchAllQuery()).execute().actionGet(); |
| 388 | + assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L)); |
| 389 | + for (SearchHit hit : searchResponse.getHits().getHits()) { |
| 390 | + assertThat(hit.getType(), equalTo("type1")); |
| 391 | + assertThat(hit.getId(), equalTo("id1")); |
| 392 | + assertThat((String)hit.getSource().get("content"), equalTo("test_type1_id1_type1#id1_null_routing1_"+timestamp+"_111211211_1_foo")); |
| 393 | + } |
| 394 | + } |
| 395 | + |
317 | 396 | }
|
0 commit comments