Search in sources :

Example 1 with ReindexRequest

use of org.opensearch.index.reindex.ReindexRequest in project OpenSearch by opensearch-project.

the class RequestConvertersTests method testReindex.

public void testReindex() throws IOException {
    ReindexRequest reindexRequest = new ReindexRequest();
    reindexRequest.setSourceIndices("source_idx");
    reindexRequest.setDestIndex("dest_idx");
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
        RemoteInfo remoteInfo = new RemoteInfo("http", "remote-host", 9200, null, BytesReference.bytes(matchAllQuery().toXContent(builder, ToXContent.EMPTY_PARAMS)), "user", "pass", emptyMap(), RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT);
        reindexRequest.setRemoteInfo(remoteInfo);
    }
    if (randomBoolean()) {
        reindexRequest.setSourceBatchSize(randomInt(100));
    }
    if (randomBoolean()) {
        reindexRequest.setDestOpType("create");
    }
    if (randomBoolean()) {
        reindexRequest.setDestPipeline("my_pipeline");
    }
    if (randomBoolean()) {
        float requestsPerSecond = (float) randomDoubleBetween(0.0, 10.0, false);
        expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, Float.toString(requestsPerSecond));
        reindexRequest.setRequestsPerSecond(requestsPerSecond);
    } else {
        expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, "-1");
    }
    if (randomBoolean()) {
        reindexRequest.setDestRouting("=cat");
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            reindexRequest.setMaxDocs(randomIntBetween(100, 1000));
        } else {
            reindexRequest.setSize(randomIntBetween(100, 1000));
        }
    }
    if (randomBoolean()) {
        reindexRequest.setAbortOnVersionConflict(false);
    }
    if (randomBoolean()) {
        String ts = randomTimeValue();
        reindexRequest.setScroll(TimeValue.parseTimeValue(ts, "scroll"));
    }
    if (reindexRequest.getRemoteInfo() == null && randomBoolean()) {
        reindexRequest.setSourceQuery(new TermQueryBuilder("foo", "fooval"));
    }
    if (randomBoolean()) {
        int slices = randomIntBetween(0, 4);
        reindexRequest.setSlices(slices);
        if (slices == 0) {
            expectedParams.put("slices", AbstractBulkByScrollRequest.AUTO_SLICES_VALUE);
        } else {
            expectedParams.put("slices", Integer.toString(slices));
        }
    } else {
        expectedParams.put("slices", "1");
    }
    setRandomTimeout(reindexRequest::setTimeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams);
    setRandomWaitForActiveShards(reindexRequest::setWaitForActiveShards, ActiveShardCount.DEFAULT, expectedParams);
    expectedParams.put("scroll", reindexRequest.getScrollTime().getStringRep());
    expectedParams.put("wait_for_completion", Boolean.TRUE.toString());
    Request request = RequestConverters.reindex(reindexRequest);
    assertEquals("/_reindex", request.getEndpoint());
    assertEquals(HttpPost.METHOD_NAME, request.getMethod());
    assertEquals(expectedParams, request.getParameters());
    assertToXContentBody(reindexRequest, request.getEntity());
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) RemoteInfo(org.opensearch.index.reindex.RemoteInfo) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with ReindexRequest

use of org.opensearch.index.reindex.ReindexRequest in project OpenSearch by opensearch-project.

the class ReindexIT method testReindex.

public void testReindex() throws IOException {
    final String sourceIndex = "source1";
    final String destinationIndex = "dest";
    {
        // Prepare
        Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
        createIndex(sourceIndex, settings);
        createIndex(destinationIndex, settings);
        BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    }
    {
        // reindex one document with id 1 from source to destination
        ReindexRequest reindexRequest = new ReindexRequest();
        reindexRequest.setSourceIndices(sourceIndex);
        reindexRequest.setDestIndex(destinationIndex);
        reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1"));
        reindexRequest.setRefresh(true);
        BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync);
        assertEquals(1, bulkResponse.getCreated());
        assertEquals(1, bulkResponse.getTotal());
        assertEquals(0, bulkResponse.getDeleted());
        assertEquals(0, bulkResponse.getNoops());
        assertEquals(0, bulkResponse.getVersionConflicts());
        assertEquals(1, bulkResponse.getBatches());
        assertTrue(bulkResponse.getTook().getMillis() > 0);
        assertEquals(1, bulkResponse.getBatches());
        assertEquals(0, bulkResponse.getBulkFailures().size());
        assertEquals(0, bulkResponse.getSearchFailures().size());
    }
    {
        // set require_alias=true, but there exists no alias
        ReindexRequest reindexRequest = new ReindexRequest();
        reindexRequest.setSourceIndices(sourceIndex);
        reindexRequest.setDestIndex(destinationIndex);
        reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1"));
        reindexRequest.setRefresh(true);
        reindexRequest.setRequireAlias(true);
        OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
            execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync);
        });
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [dest] and [require_alias] request flag is [true] and [dest] is not an alias]", exception.getMessage());
    }
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 3 with ReindexRequest

use of org.opensearch.index.reindex.ReindexRequest in project OpenSearch by opensearch-project.

the class ReindexIT method testReindexTask.

public void testReindexTask() throws Exception {
    final String sourceIndex = "source123";
    final String destinationIndex = "dest2";
    {
        // Prepare
        Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
        createIndex(sourceIndex, settings);
        createIndex(destinationIndex, settings);
        BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    }
    {
        // tag::submit-reindex-task
        // <1>
        ReindexRequest reindexRequest = new ReindexRequest();
        reindexRequest.setSourceIndices(sourceIndex);
        reindexRequest.setDestIndex(destinationIndex);
        reindexRequest.setRefresh(true);
        TaskSubmissionResponse reindexSubmission = highLevelClient().submitReindexTask(reindexRequest, // <2>
        RequestOptions.DEFAULT);
        // <3>
        String taskId = reindexSubmission.getTask();
        // end::submit-reindex-task
        assertBusy(checkTaskCompletionStatus(client(), taskId));
    }
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings)

Example 4 with ReindexRequest

use of org.opensearch.index.reindex.ReindexRequest in project OpenSearch by opensearch-project.

the class TasksIT method testGetValidTask.

public void testGetValidTask() throws Exception {
    // Run a Reindex to create a task
    final String sourceIndex = "source1";
    final String destinationIndex = "dest";
    Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(sourceIndex, settings);
    createIndex(destinationIndex, settings);
    BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
    final ReindexRequest reindexRequest = new ReindexRequest().setSourceIndices(sourceIndex).setDestIndex(destinationIndex);
    final TaskSubmissionResponse taskSubmissionResponse = highLevelClient().submitReindexTask(reindexRequest, RequestOptions.DEFAULT);
    final String taskId = taskSubmissionResponse.getTask();
    assertNotNull(taskId);
    TaskId childTaskId = new TaskId(taskId);
    GetTaskRequest gtr = new GetTaskRequest(childTaskId.getNodeId(), childTaskId.getId());
    gtr.setWaitForCompletion(randomBoolean());
    Optional<GetTaskResponse> getTaskResponse = execute(gtr, highLevelClient().tasks()::get, highLevelClient().tasks()::getAsync);
    assertTrue(getTaskResponse.isPresent());
    GetTaskResponse taskResponse = getTaskResponse.get();
    if (gtr.getWaitForCompletion()) {
        assertTrue(taskResponse.isCompleted());
    }
    org.opensearch.tasks.TaskInfo info = taskResponse.getTaskInfo();
    assertTrue(info.isCancellable());
    assertEquals("reindex from [source1] to [dest]", info.getDescription());
    assertEquals("indices:data/write/reindex", info.getAction());
    if (taskResponse.isCompleted() == false) {
        assertBusy(checkTaskCompletionStatus(client(), taskId));
    }
}
Also used : GetTaskRequest(org.opensearch.client.tasks.GetTaskRequest) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) TaskId(org.opensearch.client.tasks.TaskId) GetTaskResponse(org.opensearch.client.tasks.GetTaskResponse) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Settings(org.opensearch.common.settings.Settings)

Example 5 with ReindexRequest

use of org.opensearch.index.reindex.ReindexRequest in project OpenSearch by opensearch-project.

the class ReindexIT method testReindexConflict.

public void testReindexConflict() throws IOException {
    final String sourceIndex = "testreindexconflict_source";
    final String destIndex = "testreindexconflict_dest";
    final Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(sourceIndex, settings);
    createIndex(destIndex, settings);
    final BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    assertThat(highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status(), equalTo(RestStatus.OK));
    putConflictPipeline();
    final ReindexRequest reindexRequest = new ReindexRequest();
    reindexRequest.setSourceIndices(sourceIndex);
    reindexRequest.setDestIndex(destIndex);
    reindexRequest.setRefresh(true);
    reindexRequest.setDestPipeline(CONFLICT_PIPELINE_ID);
    final BulkByScrollResponse response = highLevelClient().reindex(reindexRequest, RequestOptions.DEFAULT);
    assertThat(response.getVersionConflicts(), equalTo(2L));
    assertThat(response.getSearchFailures(), empty());
    assertThat(response.getBulkFailures(), hasSize(2));
    assertThat(response.getBulkFailures().stream().map(BulkItemResponse.Failure::getMessage).collect(Collectors.toSet()), everyItem(containsString("version conflict")));
    assertThat(response.getTotal(), equalTo(2L));
    assertThat(response.getCreated(), equalTo(0L));
    assertThat(response.getUpdated(), equalTo(0L));
    assertThat(response.getDeleted(), equalTo(0L));
    assertThat(response.getNoops(), equalTo(0L));
    assertThat(response.getBatches(), equalTo(1));
    assertTrue(response.getTook().getMillis() > 0);
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Aggregations

BulkRequest (org.opensearch.action.bulk.BulkRequest)6 IndexRequest (org.opensearch.action.index.IndexRequest)6 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)6 Settings (org.opensearch.common.settings.Settings)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)2 DeleteRequest (org.opensearch.action.delete.DeleteRequest)2 ExplainRequest (org.opensearch.action.explain.ExplainRequest)2 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)2 GetRequest (org.opensearch.action.get.GetRequest)2 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)2 WriteRequest (org.opensearch.action.support.WriteRequest)2 UpdateRequest (org.opensearch.action.update.UpdateRequest)2