|
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;
|
@@ -689,6 +693,69 @@ public void testBulk() throws IOException {
|
689 | 693 | validateBulkResponses(nbItems, errors, bulkResponse, bulkRequest);
|
690 | 694 | }
|
691 | 695 |
|
| 696 | + public void testReindex() throws IOException { |
| 697 | + final String sourceIndex = "source1"; |
| 698 | + final String destinationIndex = "dest"; |
| 699 | + { |
| 700 | + // Prepare |
| 701 | + Settings settings = Settings.builder() |
| 702 | + .put("number_of_shards", 1) |
| 703 | + .put("number_of_replicas", 0) |
| 704 | + .build(); |
| 705 | + createIndex(sourceIndex, settings); |
| 706 | + createIndex(destinationIndex, settings); |
| 707 | + assertEquals( |
| 708 | + RestStatus.OK, |
| 709 | + highLevelClient().bulk( |
| 710 | + new BulkRequest() |
| 711 | + .add(new IndexRequest(sourceIndex, "type", "1") |
| 712 | + .source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) |
| 713 | + .add(new IndexRequest(sourceIndex, "type", "2") |
| 714 | + .source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) |
| 715 | + .setRefreshPolicy(RefreshPolicy.IMMEDIATE), |
| 716 | + RequestOptions.DEFAULT |
| 717 | + ).status() |
| 718 | + ); |
| 719 | + } |
| 720 | + { |
| 721 | + // test1: create one doc in dest |
| 722 | + ReindexRequest reindexRequest = new ReindexRequest(); |
| 723 | + reindexRequest.setSourceIndices(sourceIndex); |
| 724 | + reindexRequest.setDestIndex(destinationIndex); |
| 725 | + reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1").types("type")); |
| 726 | + reindexRequest.setRefresh(true); |
| 727 | + BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); |
| 728 | + assertEquals(1, bulkResponse.getCreated()); |
| 729 | + assertEquals(1, bulkResponse.getTotal()); |
| 730 | + assertEquals(0, bulkResponse.getDeleted()); |
| 731 | + assertEquals(0, bulkResponse.getNoops()); |
| 732 | + assertEquals(0, bulkResponse.getVersionConflicts()); |
| 733 | + assertEquals(1, bulkResponse.getBatches()); |
| 734 | + assertTrue(bulkResponse.getTook().getMillis() > 0); |
| 735 | + assertEquals(1, bulkResponse.getBatches()); |
| 736 | + assertEquals(0, bulkResponse.getBulkFailures().size()); |
| 737 | + assertEquals(0, bulkResponse.getSearchFailures().size()); |
| 738 | + } |
| 739 | + { |
| 740 | + // test2: create 1 and update 1 |
| 741 | + ReindexRequest reindexRequest = new ReindexRequest(); |
| 742 | + reindexRequest.setSourceIndices(sourceIndex); |
| 743 | + reindexRequest.setDestIndex(destinationIndex); |
| 744 | + BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); |
| 745 | + assertEquals(1, bulkResponse.getCreated()); |
| 746 | + assertEquals(2, bulkResponse.getTotal()); |
| 747 | + assertEquals(1, bulkResponse.getUpdated()); |
| 748 | + assertEquals(0, bulkResponse.getDeleted()); |
| 749 | + assertEquals(0, bulkResponse.getNoops()); |
| 750 | + assertEquals(0, bulkResponse.getVersionConflicts()); |
| 751 | + assertEquals(1, bulkResponse.getBatches()); |
| 752 | + assertTrue(bulkResponse.getTook().getMillis() > 0); |
| 753 | + assertEquals(1, bulkResponse.getBatches()); |
| 754 | + assertEquals(0, bulkResponse.getBulkFailures().size()); |
| 755 | + assertEquals(0, bulkResponse.getSearchFailures().size()); |
| 756 | + } |
| 757 | + } |
| 758 | + |
692 | 759 | public void testBulkProcessorIntegration() throws IOException {
|
693 | 760 | int nbItems = randomIntBetween(10, 100);
|
694 | 761 | boolean[] errors = new boolean[nbItems];
|
|
0 commit comments