Search in sources :

Example 1 with UpdateByQueryRequest

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

the class UpdateByQueryIT method testUpdateByQueryConflict.

public void testUpdateByQueryConflict() throws IOException {
    final String index = "testupdatebyqueryconflict";
    final Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(index, settings);
    final BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(index).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 UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
    updateByQueryRequest.indices(index);
    updateByQueryRequest.setRefresh(true);
    updateByQueryRequest.setPipeline(CONFLICT_PIPELINE_ID);
    final BulkByScrollResponse response = highLevelClient().updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
    assertThat(response.getVersionConflicts(), equalTo(1L));
    assertThat(response.getSearchFailures(), empty());
    assertThat(response.getBulkFailures(), hasSize(1));
    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(1L));
    assertThat(response.getDeleted(), equalTo(0L));
    assertThat(response.getNoops(), equalTo(0L));
    assertThat(response.getBatches(), equalTo(1));
    assertTrue(response.getTook().getMillis() > 0);
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Example 2 with UpdateByQueryRequest

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

the class RequestConvertersTests method testUpdateByQuery.

public void testUpdateByQuery() throws IOException {
    UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
    updateByQueryRequest.indices(randomIndicesNames(1, 5));
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        int batchSize = randomInt(100);
        updateByQueryRequest.setBatchSize(batchSize);
        expectedParams.put("scroll_size", Integer.toString(batchSize));
    }
    if (randomBoolean()) {
        updateByQueryRequest.setPipeline("my_pipeline");
        expectedParams.put("pipeline", "my_pipeline");
    }
    if (randomBoolean()) {
        float requestsPerSecond = (float) randomDoubleBetween(0.0, 10.0, false);
        expectedParams.put("requests_per_second", Float.toString(requestsPerSecond));
        updateByQueryRequest.setRequestsPerSecond(requestsPerSecond);
    } else {
        expectedParams.put("requests_per_second", "-1");
    }
    if (randomBoolean()) {
        updateByQueryRequest.setRouting("=cat");
        expectedParams.put("routing", "=cat");
    }
    if (randomBoolean()) {
        int size = randomIntBetween(100, 1000);
        if (randomBoolean()) {
            updateByQueryRequest.setMaxDocs(size);
        } else {
            updateByQueryRequest.setSize(size);
        }
        expectedParams.put("max_docs", Integer.toString(size));
    }
    if (randomBoolean()) {
        updateByQueryRequest.setAbortOnVersionConflict(false);
        expectedParams.put("conflicts", "proceed");
    }
    if (randomBoolean()) {
        String ts = randomTimeValue();
        updateByQueryRequest.setScroll(TimeValue.parseTimeValue(ts, "scroll"));
        expectedParams.put("scroll", ts);
    }
    if (randomBoolean()) {
        updateByQueryRequest.setQuery(new TermQueryBuilder("foo", "fooval"));
    }
    if (randomBoolean()) {
        updateByQueryRequest.setScript(new Script("ctx._source.last = \"lastname\""));
    }
    if (randomBoolean()) {
        int slices = randomIntBetween(0, 4);
        if (slices == 0) {
            expectedParams.put("slices", AbstractBulkByScrollRequest.AUTO_SLICES_VALUE);
        } else {
            expectedParams.put("slices", Integer.toString(slices));
        }
        updateByQueryRequest.setSlices(slices);
    } else {
        expectedParams.put("slices", "1");
    }
    setRandomIndicesOptions(updateByQueryRequest::setIndicesOptions, updateByQueryRequest::indicesOptions, expectedParams);
    setRandomTimeout(updateByQueryRequest::setTimeout, ReplicationRequest.DEFAULT_TIMEOUT, expectedParams);
    expectedParams.put("wait_for_completion", Boolean.TRUE.toString());
    Request request = RequestConverters.updateByQuery(updateByQueryRequest);
    StringJoiner joiner = new StringJoiner("/", "/", "");
    joiner.add(String.join(",", updateByQueryRequest.indices()));
    joiner.add("_update_by_query");
    assertEquals(joiner.toString(), request.getEndpoint());
    assertEquals(HttpPost.METHOD_NAME, request.getMethod());
    assertEquals(expectedParams, request.getParameters());
    assertToXContentBody(updateByQueryRequest, request.getEntity());
}
Also used : Script(org.opensearch.script.Script) 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) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) StringJoiner(java.util.StringJoiner)

Example 3 with UpdateByQueryRequest

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

the class RequestConverters method prepareUpdateByQueryRequest.

static Request prepareUpdateByQueryRequest(UpdateByQueryRequest updateByQueryRequest, boolean waitForCompletion) throws IOException {
    String endpoint = endpoint(updateByQueryRequest.indices(), "_update_by_query");
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);
    Params params = new Params().withRouting(updateByQueryRequest.getRouting()).withPipeline(updateByQueryRequest.getPipeline()).withRefresh(updateByQueryRequest.isRefresh()).withTimeout(updateByQueryRequest.getTimeout()).withWaitForActiveShards(updateByQueryRequest.getWaitForActiveShards()).withRequestsPerSecond(updateByQueryRequest.getRequestsPerSecond()).withIndicesOptions(updateByQueryRequest.indicesOptions()).withWaitForCompletion(waitForCompletion).withSlices(updateByQueryRequest.getSlices());
    if (updateByQueryRequest.isAbortOnVersionConflict() == false) {
        params.putParam("conflicts", "proceed");
    }
    if (updateByQueryRequest.getBatchSize() != AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE) {
        params.putParam("scroll_size", Integer.toString(updateByQueryRequest.getBatchSize()));
    }
    if (updateByQueryRequest.getScrollTime() != AbstractBulkByScrollRequest.DEFAULT_SCROLL_TIMEOUT) {
        params.putParam("scroll", updateByQueryRequest.getScrollTime());
    }
    if (updateByQueryRequest.getMaxDocs() > 0) {
        params.putParam("max_docs", Integer.toString(updateByQueryRequest.getMaxDocs()));
    }
    request.addParameters(params.asMap());
    request.setEntity(createEntity(updateByQueryRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) 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) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Aggregations

BulkRequest (org.opensearch.action.bulk.BulkRequest)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 UpdateByQueryRequest (org.opensearch.index.reindex.UpdateByQueryRequest)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 CountRequest (org.opensearch.client.core.CountRequest)2 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)2