Search in sources :

Example 21 with DocumentEntity

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

the class CouchbaseDocumentCollectionManagerTest method shouldSaveSubDocument2.

@Test
public void shouldSaveSubDocument2() throws InterruptedException {
    DocumentEntity entity = getEntity();
    entity.add(Document.of("phones", asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Thread.sleep(1_00L);
    Document id = entitySaved.find("_id").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, contains(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231")));
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 22 with DocumentEntity

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

the class CouchbaseDocumentCollectionManagerTest method shouldSaveSubDocument.

@Test
public void shouldSaveSubDocument() throws InterruptedException {
    DocumentEntity entity = getEntity();
    entity.add(Document.of("phones", Document.of("mobile", "1231231")));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Thread.sleep(5_00L);
    Document id = entitySaved.find("_id").get();
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, contains(Document.of("mobile", "1231231")));
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(org.jnosql.diana.api.document.Document) Test(org.junit.jupiter.api.Test)

Example 23 with DocumentEntity

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

the class CouchbaseDocumentCollectionManagerTest method shouldSaveSetDocument.

@Test
public void shouldSaveSetDocument() throws InterruptedException {
    Set<String> set = new HashSet<>();
    set.add("Acarajé");
    set.add("Munguzá");
    DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
    entity.add(Document.of("_id", "id"));
    entity.add(Document.of("foods", set));
    entityManager.insert(entity);
    Document id = entity.find("_id").get();
    Thread.sleep(1_000L);
    DocumentQuery query = select().from(COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.singleResult(query).get();
    Optional<Document> foods = entityFound.find("foods");
    Set<String> setFoods = foods.get().get(new TypeReference<Set<String>>() {
    });
    assertEquals(set, setFoods);
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) HashSet(java.util.HashSet) Set(java.util.Set) Document(org.jnosql.diana.api.document.Document) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 24 with DocumentEntity

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

the class CouchbaseDocumentCollectionManagerTest method shouldConvertFromListSubdocumentList.

@Test
public void shouldConvertFromListSubdocumentList() {
    DocumentEntity entity = createSubdocumentList();
    entityManager.insert(entity);
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Test(org.junit.jupiter.api.Test)

Example 25 with DocumentEntity

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

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