Search in sources :

Example 1 with BulkByScrollResponse

use of org.opensearch.index.reindex.BulkByScrollResponse 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 BulkByScrollResponse

use of org.opensearch.index.reindex.BulkByScrollResponse 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 BulkByScrollResponse

use of org.opensearch.index.reindex.BulkByScrollResponse 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 4 with BulkByScrollResponse

use of org.opensearch.index.reindex.BulkByScrollResponse 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 BulkByScrollResponse

use of org.opensearch.index.reindex.BulkByScrollResponse 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

BulkByScrollResponse (org.opensearch.index.reindex.BulkByScrollResponse)10 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)4 DeleteByQueryRequest (org.opensearch.index.reindex.DeleteByQueryRequest)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 OpenSearchException (org.opensearch.OpenSearchException)3 BulkRequest (org.opensearch.action.bulk.BulkRequest)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 Client (org.opensearch.client.Client)3 Settings (org.opensearch.common.settings.Settings)3 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)3 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)3 QueryBuilder (org.opensearch.index.query.QueryBuilder)3 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)2 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)1 IdsQueryBuilder (org.opensearch.index.query.IdsQueryBuilder)1 DeleteByQueryRequestBuilder (org.opensearch.index.reindex.DeleteByQueryRequestBuilder)1 ReindexRequestBuilder (org.opensearch.index.reindex.ReindexRequestBuilder)1 UpdateByQueryRequest (org.opensearch.index.reindex.UpdateByQueryRequest)1 UpdateByQueryRequestBuilder (org.opensearch.index.reindex.UpdateByQueryRequestBuilder)1