Search in sources :

Example 16 with DocumentDeleteQuery

use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.

the class DefaultElasticsearchDocumentCollectionManager method delete.

@Override
public void delete(DocumentDeleteQuery query) throws NullPointerException {
    requireNonNull(query, "query is required");
    query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
    DocumentQuery select = new ElasticsearchDocumentQuery(query);
    List<DocumentEntity> entities = select(select);
    if (entities.isEmpty()) {
        return;
    }
    BulkRequest bulk = new BulkRequest();
    entities.stream().map(entity -> entity.find(ID_FIELD).get().get(String.class)).map(id -> new DeleteRequest(index, query.getDocumentCollection(), id)).forEach(bulk::add);
    try {
        client.bulk(bulk);
    } catch (IOException e) {
        throw new ElasticsearchException("An error to delete entities on elasticsearch", e);
    }
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.jnosql.diana.api.document.Document) EntityConverter.getMap(org.jnosql.diana.elasticsearch.document.EntityConverter.getMap) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) Collectors(java.util.stream.Collectors) ID_FIELD(org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Objects(java.util.Objects) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) List(java.util.List) StreamSupport.stream(java.util.stream.StreamSupport.stream) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IOException(java.io.IOException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 17 with DocumentDeleteQuery

use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.

the class DefaultElasticsearchDocumentCollectionManagerAsync method delete.

@Override
public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) {
    requireNonNull(query, "query is required");
    requireNonNull(callBack, "callBack is required");
    query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
    DocumentQuery select = new ElasticsearchDocumentQuery(query);
    List<DocumentEntity> entities = EntityConverter.query(select, client, index);
    if (entities.isEmpty()) {
        callBack.accept(null);
        return;
    }
    BulkRequest bulk = new BulkRequest();
    entities.stream().map(entity -> entity.find(ID_FIELD).get().get(String.class)).map(id -> new DeleteRequest(index, query.getDocumentCollection(), id)).forEach(bulk::add);
    ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {

        @Override
        public void onResponse(BulkResponse bulkItemResponses) {
            callBack.accept(null);
        }

        @Override
        public void onFailure(Exception e) {
            throw new ExecuteAsyncQueryException("An error when delete on elasticsearch", e);
        }
    };
    client.bulkAsync(bulk, listener);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.jnosql.diana.api.document.Document) JsonbBuilder(javax.json.bind.JsonbBuilder) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) EntityConverter.getMap(org.jnosql.diana.elasticsearch.document.EntityConverter.getMap) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) ID_FIELD(org.jnosql.diana.elasticsearch.document.EntityConverter.ID_FIELD) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Consumer(java.util.function.Consumer) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) List(java.util.List) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Jsonb(javax.json.bind.Jsonb) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionListener(org.elasticsearch.action.ActionListener) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) IOException(java.io.IOException) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ActionListener(org.elasticsearch.action.ActionListener) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 18 with DocumentDeleteQuery

use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.

the class OrientDBDocumentCollectionManagerAsyncTest method shouldRemoveEntityAsync.

@Test
public void shouldRemoveEntityAsync() throws InterruptedException {
    DocumentEntity documentEntity = entityManager.insert(getEntity());
    Document id = documentEntity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    AtomicBoolean condition = new AtomicBoolean(false);
    entityManagerAsync.delete(deleteQuery, c -> condition.set(true));
    await().untilTrue(condition);
    assertTrue(entityManager.select(query).isEmpty());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 19 with DocumentDeleteQuery

use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.

the class OrientDBDocumentCollectionManagerAsyncTest method shouldRemoveEntityAsyncWithoutCallback.

@Test
public void shouldRemoveEntityAsyncWithoutCallback() throws InterruptedException {
    DocumentEntity entity = entityManager.insert(getEntity());
    Document id = entity.find(OrientDBConverter.RID_FIELD).get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    entityManagerAsync.delete(deleteQuery);
    Thread.sleep(1000L);
    assertTrue(entityManager.select(query).isEmpty());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 20 with DocumentDeleteQuery

use of org.jnosql.diana.api.document.DocumentDeleteQuery in project jnosql-diana-driver by eclipse.

the class OrientDBDocumentCollectionManagerAsyncTest method removePersons.

@AfterEach
void removePersons() {
    DocumentDeleteQuery query = delete().from(COLLECTION_NAME).build();
    entityManager.delete(query);
}
Also used : DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) AfterEach(org.junit.jupiter.api.AfterEach)

Aggregations

DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)86 Test (org.junit.jupiter.api.Test)74 Document (org.jnosql.diana.api.document.Document)35 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)31 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)31 DocumentCondition (org.jnosql.diana.api.document.DocumentCondition)20 List (java.util.List)15 Person (org.jnosql.artemis.model.Person)13 Duration (java.time.Duration)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Optional (java.util.Optional)4 Consumer (java.util.function.Consumer)4 Collectors (java.util.stream.Collectors)4 DocumentQueryBuilder.delete (org.jnosql.diana.api.document.query.DocumentQueryBuilder.delete)4 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)4 Arrays (java.util.Arrays)3