Search in sources :

Example 1 with UpdateByQueryRequestBuilder

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

the class ReindexDocumentationIT method testUpdateByQuery.

@SuppressWarnings("unused")
public void testUpdateByQuery() {
    Client client = client();
    client.admin().indices().prepareCreate("foo").get();
    client.admin().indices().prepareCreate("bar").get();
    client.admin().indices().preparePutMapping(INDEX_NAME).setSource("cat", "type=keyword").get();
    {
        // tag::update-by-query
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("source_index").abortOnVersionConflict(false);
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query
    }
    {
        // tag::update-by-query-filter
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("source_index").filter(QueryBuilders.termQuery("level", "awesome")).maxDocs(1000).script(new Script(ScriptType.INLINE, "painless", "ctx._source.awesome = 'absolutely'", Collections.emptyMap()));
        BulkByScrollResponse response = updateByQuery.get();
        // end::update-by-query-filter
        // validate order of string params to Script constructor
        assertEquals(updateByQuery.request().getScript().getLang(), "painless");
    }
    {
        // tag::update-by-query-size
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("source_index").source().setSize(500);
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query-size
    }
    {
        // tag::update-by-query-sort
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("source_index").maxDocs(100).source().addSort("cat", SortOrder.DESC);
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query-sort
    }
    {
        // tag::update-by-query-script
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("source_index").script(new Script(ScriptType.INLINE, "painless", "if (ctx._source.awesome == 'absolutely') {" + "  ctx.op='noop'" + "} else if (ctx._source.awesome == 'lame') {" + "  ctx.op='delete'" + "} else {" + "ctx._source.awesome = 'absolutely'}", Collections.emptyMap()));
        BulkByScrollResponse response = updateByQuery.get();
        // end::update-by-query-script
        // validate order of string params to Script constructor
        assertEquals(updateByQuery.request().getScript().getLang(), "painless");
    }
    {
        // tag::update-by-query-multi-index
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source("foo", "bar");
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query-multi-index
    }
    {
        // tag::update-by-query-routing
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.source().setRouting("cat");
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query-routing
    }
    {
        // tag::update-by-query-pipeline
        UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE);
        updateByQuery.setPipeline("hurray");
        BulkByScrollResponse response = updateByQuery.get();
    // end::update-by-query-pipeline
    }
}
Also used : Script(org.opensearch.script.Script) Client(org.opensearch.client.Client) UpdateByQueryRequestBuilder(org.opensearch.index.reindex.UpdateByQueryRequestBuilder) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Aggregations

Client (org.opensearch.client.Client)1 BulkByScrollResponse (org.opensearch.index.reindex.BulkByScrollResponse)1 UpdateByQueryRequestBuilder (org.opensearch.index.reindex.UpdateByQueryRequestBuilder)1 Script (org.opensearch.script.Script)1