Search in sources :

Example 26 with DocumentEntity

use of org.jnosql.diana.api.document.DocumentEntity 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 27 with DocumentEntity

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

the class DocumentQueryTest method shouldShouldDefineLimit.

@Test
public void shouldShouldDefineLimit() {
    DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id"), Document.of("name", "name")));
    Document name = entity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(name.getName()).eq(name.get()).limit(2L).build();
    List<DocumentEntity> entities = entityManager.select(query);
    assertEquals(2, entities.size());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 28 with DocumentEntity

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

the class DocumentQueryTest method shouldFindDocumentByNameSortDesc.

@Test
public void shouldFindDocumentByNameSortDesc() {
    DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id4"), Document.of("name", "name3"), Document.of("_key", "person:id4")));
    Optional<Document> name = entity.find("name");
    DocumentQuery query = select().from(COLLECTION_NAME).orderBy("name").desc().build();
    List<DocumentEntity> entities = entityManager.select(query);
    List<String> result = entities.stream().flatMap(e -> e.getDocuments().stream()).filter(d -> "name".equals(d.getName())).map(d -> d.get(String.class)).collect(Collectors.toList());
    assertFalse(result.isEmpty());
    assertThat(result, contains("name3", "name", "name", "name"));
}
Also used : Document(org.jnosql.diana.api.document.Document) Arrays(java.util.Arrays) DocumentQueryBuilder.select(org.jnosql.diana.api.document.query.DocumentQueryBuilder.select) CouchbaseKeyValueConfiguration(org.jnosql.diana.couchbase.key.CouchbaseKeyValueConfiguration) Collectors(java.util.stream.Collectors) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) Matchers.contains(org.hamcrest.Matchers.contains) Arrays.asList(java.util.Arrays.asList) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BucketManager(org.jnosql.diana.api.key.BucketManager) CouchbaseUtil(org.jnosql.diana.couchbase.CouchbaseUtil) Optional(java.util.Optional) BucketManagerFactory(org.jnosql.diana.api.key.BucketManagerFactory) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 29 with DocumentEntity

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

the class DocumentQueryTest method shouldShouldDefineLimitAndStart.

@Test
public void shouldShouldDefineLimitAndStart() {
    DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id"), Document.of("name", "name")));
    Document name = entity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(name.getName()).eq(name.get()).start(2L).limit(2L).build();
    List<DocumentEntity> entities = entityManager.select(query);
    assertEquals(1, entities.size());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 30 with DocumentEntity

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

the class DocumentQueryTest method shouldShouldDefineStart.

@Test
public void shouldShouldDefineStart() {
    DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id"), Document.of("name", "name")));
    Document name = entity.find("name").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(name.getName()).eq(name.get()).start(1L).build();
    List<DocumentEntity> entities = entityManager.select(query);
    assertEquals(2, entities.size());
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)205 Test (org.junit.jupiter.api.Test)167 Document (org.jnosql.diana.api.document.Document)107 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)101 List (java.util.List)63 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)47 Arrays.asList (java.util.Arrays.asList)38 ArrayList (java.util.ArrayList)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 Duration (java.time.Duration)22 HashMap (java.util.HashMap)21 Consumer (java.util.function.Consumer)19 Arrays (java.util.Arrays)17 Collectors (java.util.stream.Collectors)17 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)17 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)16 Map (java.util.Map)15 Optional (java.util.Optional)15 BeforeEach (org.junit.jupiter.api.BeforeEach)15