|
41 | 41 | import org.elasticsearch.action.update.UpdateResponse;
|
42 | 42 | import org.elasticsearch.common.Strings;
|
43 | 43 | import org.elasticsearch.common.bytes.BytesReference;
|
| 44 | +import org.elasticsearch.common.settings.Settings; |
44 | 45 | import org.elasticsearch.common.unit.ByteSizeUnit;
|
45 | 46 | import org.elasticsearch.common.unit.ByteSizeValue;
|
46 | 47 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
47 | 48 | import org.elasticsearch.common.xcontent.XContentType;
|
48 | 49 | import org.elasticsearch.index.VersionType;
|
49 | 50 | import org.elasticsearch.index.get.GetResult;
|
| 51 | +import org.elasticsearch.index.query.IdsQueryBuilder; |
| 52 | +import org.elasticsearch.index.reindex.BulkByScrollResponse; |
| 53 | +import org.elasticsearch.index.reindex.ReindexRequest; |
50 | 54 | import org.elasticsearch.rest.RestStatus;
|
51 | 55 | import org.elasticsearch.script.Script;
|
52 | 56 | import org.elasticsearch.script.ScriptType;
|
@@ -624,6 +628,69 @@ public void testBulk() throws IOException {
|
624 | 628 | validateBulkResponses(nbItems, errors, bulkResponse, bulkRequest);
|
625 | 629 | }
|
626 | 630 |
|
| 631 | + public void testReindex() throws IOException { |
| 632 | + final String sourceIndex = "source1"; |
| 633 | + final String destinationIndex = "dest"; |
| 634 | + { |
| 635 | + // Prepare |
| 636 | + Settings settings = Settings.builder() |
| 637 | + .put("number_of_shards", 1) |
| 638 | + .put("number_of_replicas", 0) |
| 639 | + .build(); |
| 640 | + createIndex(sourceIndex, settings); |
| 641 | + createIndex(destinationIndex, settings); |
| 642 | + assertEquals( |
| 643 | + RestStatus.OK, |
| 644 | + highLevelClient().bulk( |
| 645 | + new BulkRequest() |
| 646 | + .add(new IndexRequest(sourceIndex, "type", "1") |
| 647 | + .source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) |
| 648 | + .add(new IndexRequest(sourceIndex, "type", "2") |
| 649 | + .source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) |
| 650 | + .setRefreshPolicy(RefreshPolicy.IMMEDIATE), |
| 651 | + RequestOptions.DEFAULT |
| 652 | + ).status() |
| 653 | + ); |
| 654 | + } |
| 655 | + { |
| 656 | + // test1: create one doc in dest |
| 657 | + ReindexRequest reindexRequest = new ReindexRequest(); |
| 658 | + reindexRequest.setSourceIndices(sourceIndex); |
| 659 | + reindexRequest.setDestIndex(destinationIndex); |
| 660 | + reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1").types("type")); |
| 661 | + reindexRequest.setRefresh(true); |
| 662 | + BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); |
| 663 | + assertEquals(1, bulkResponse.getCreated()); |
| 664 | + assertEquals(1, bulkResponse.getTotal()); |
| 665 | + assertEquals(0, bulkResponse.getDeleted()); |
| 666 | + assertEquals(0, bulkResponse.getNoops()); |
| 667 | + assertEquals(0, bulkResponse.getVersionConflicts()); |
| 668 | + assertEquals(1, bulkResponse.getBatches()); |
| 669 | + assertTrue(bulkResponse.getTook().getMillis() > 0); |
| 670 | + assertEquals(1, bulkResponse.getBatches()); |
| 671 | + assertEquals(0, bulkResponse.getBulkFailures().size()); |
| 672 | + assertEquals(0, bulkResponse.getSearchFailures().size()); |
| 673 | + } |
| 674 | + { |
| 675 | + // test2: create 1 and update 1 |
| 676 | + ReindexRequest reindexRequest = new ReindexRequest(); |
| 677 | + reindexRequest.setSourceIndices(sourceIndex); |
| 678 | + reindexRequest.setDestIndex(destinationIndex); |
| 679 | + BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); |
| 680 | + assertEquals(1, bulkResponse.getCreated()); |
| 681 | + assertEquals(2, bulkResponse.getTotal()); |
| 682 | + assertEquals(1, bulkResponse.getUpdated()); |
| 683 | + assertEquals(0, bulkResponse.getDeleted()); |
| 684 | + assertEquals(0, bulkResponse.getNoops()); |
| 685 | + assertEquals(0, bulkResponse.getVersionConflicts()); |
| 686 | + assertEquals(1, bulkResponse.getBatches()); |
| 687 | + assertTrue(bulkResponse.getTook().getMillis() > 0); |
| 688 | + assertEquals(1, bulkResponse.getBatches()); |
| 689 | + assertEquals(0, bulkResponse.getBulkFailures().size()); |
| 690 | + assertEquals(0, bulkResponse.getSearchFailures().size()); |
| 691 | + } |
| 692 | + } |
| 693 | + |
627 | 694 | public void testBulkProcessorIntegration() throws IOException {
|
628 | 695 | int nbItems = randomIntBetween(10, 100);
|
629 | 696 | boolean[] errors = new boolean[nbItems];
|
|
0 commit comments