Search in sources :

Example 1 with DeleteByQueryRequest

use of org.opensearch.index.reindex.DeleteByQueryRequest in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method unindexEntity.

@Override
public void unindexEntity(final String href) throws CalFacadeException {
    try {
        final DeleteByQueryRequest dqr = new DeleteByQueryRequest(targetIndex);
        final QueryBuilder qb = getFilters(null).singleEntityQuery(href, PropertyInfoIndex.HREF);
        dqr.setConflicts("proceed");
        dqr.setRefresh(true);
        dqr.setQuery(qb);
        BulkByScrollResponse bulkResponse = getClient().deleteByQuery(dqr, RequestOptions.DEFAULT);
        markUpdated();
    // TODO check response?
    } catch (final OpenSearchException ese) {
        // Failed somehow
        error(ese);
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    } finally {
        lastIndexTime = System.currentTimeMillis();
    }
}
Also used : DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) OpenSearchException(org.opensearch.OpenSearchException) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Example 2 with DeleteByQueryRequest

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

the class RequestConverters method prepareDeleteByQueryRequest.

private static Request prepareDeleteByQueryRequest(DeleteByQueryRequest deleteByQueryRequest, boolean waitForCompletion) throws IOException {
    String endpoint = endpoint(deleteByQueryRequest.indices(), "_delete_by_query");
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);
    Params params = new Params().withRouting(deleteByQueryRequest.getRouting()).withRefresh(deleteByQueryRequest.isRefresh()).withTimeout(deleteByQueryRequest.getTimeout()).withWaitForActiveShards(deleteByQueryRequest.getWaitForActiveShards()).withRequestsPerSecond(deleteByQueryRequest.getRequestsPerSecond()).withIndicesOptions(deleteByQueryRequest.indicesOptions()).withWaitForCompletion(waitForCompletion).withSlices(deleteByQueryRequest.getSlices());
    if (deleteByQueryRequest.isAbortOnVersionConflict() == false) {
        params.putParam("conflicts", "proceed");
    }
    if (deleteByQueryRequest.getBatchSize() != AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE) {
        params.putParam("scroll_size", Integer.toString(deleteByQueryRequest.getBatchSize()));
    }
    if (deleteByQueryRequest.getScrollTime() != AbstractBulkByScrollRequest.DEFAULT_SCROLL_TIMEOUT) {
        params.putParam("scroll", deleteByQueryRequest.getScrollTime());
    }
    if (deleteByQueryRequest.getMaxDocs() > 0) {
        params.putParam("max_docs", Integer.toString(deleteByQueryRequest.getMaxDocs()));
    }
    request.addParameters(params.asMap());
    request.setEntity(createEntity(deleteByQueryRequest, 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)

Example 3 with DeleteByQueryRequest

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

the class RequestConvertersTests method testDeleteByQuery.

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

Example 4 with DeleteByQueryRequest

use of org.opensearch.index.reindex.DeleteByQueryRequest in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method unindexTombstoned.

@Override
public void unindexTombstoned(final String docType, final String href) throws CalFacadeException {
    try {
        final DeleteByQueryRequest dqr = new DeleteByQueryRequest(targetIndex);
        dqr.setConflicts("proceed");
        dqr.setRefresh(true);
        final QueryBuilder fb = getFilters(null).singleTombstonedEntityQuery(href, PropertyInfoIndex.HREF);
        dqr.setQuery(fb);
        final BulkByScrollResponse bulkResponse = getClient().deleteByQuery(dqr, RequestOptions.DEFAULT);
        markUpdated();
    // TODO check response?
    } catch (final OpenSearchException ese) {
        // Failed somehow
        error(ese);
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    } finally {
        lastIndexTime = System.currentTimeMillis();
    }
}
Also used : DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) OpenSearchException(org.opensearch.OpenSearchException) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Example 5 with DeleteByQueryRequest

use of org.opensearch.index.reindex.DeleteByQueryRequest in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method deleteEvent.

private boolean deleteEvent(final EventInfo ei) throws CalFacadeException {
    final DeleteByQueryRequest delQreq = new DeleteByQueryRequest(targetIndex);
    final var path = ei.getEvent().getColPath();
    final var uid = ei.getEvent().getUid();
    final ESQueryFilter esq = getFilters(null);
    final QueryBuilder qb = getFilters(null).allInstances(path, uid);
    delQreq.setConflicts("proceed");
    delQreq.setRefresh(true);
    delQreq.setQuery(qb);
    boolean ok = true;
    try {
        final BulkByScrollResponse bulkResponse = getClient().deleteByQuery(delQreq, RequestOptions.DEFAULT);
        for (final BulkItemResponse.Failure f : bulkResponse.getBulkFailures()) {
            warn(format("Failing shards for delete - " + "path: %s, uid: %s, index: %s", path, uid, f.getIndex()));
            ok = false;
        }
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return ok;
}
Also used : DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Aggregations

DeleteByQueryRequest (org.opensearch.index.reindex.DeleteByQueryRequest)6 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 OpenSearchException (org.opensearch.OpenSearchException)3 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)3 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)3 BulkByScrollResponse (org.opensearch.index.reindex.BulkByScrollResponse)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 BulkRequest (org.opensearch.action.bulk.BulkRequest)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 IndexRequest (org.opensearch.action.index.IndexRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2