Search in sources :

Example 66 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchEntry method toEntity.

DocumentEntity toEntity() {
    Document id = Document.of(ID_FIELD, this.id);
    List<Document> documents = map.keySet().stream().map(k -> toDocument(k, map)).collect(Collectors.toList());
    DocumentEntity entity = DocumentEntity.of(collection, documents);
    entity.remove(ID_FIELD);
    entity.add(id);
    return entity;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) GetResponse(org.elasticsearch.action.get.GetResponse) ID_FIELD(org.eclipse.jnosql.communication.elasticsearch.document.EntityConverter.ID_FIELD) Document(jakarta.nosql.document.Document) DocumentEntity(jakarta.nosql.document.DocumentEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ENTITY(org.eclipse.jnosql.communication.elasticsearch.document.EntityConverter.ENTITY) StreamSupport.stream(java.util.stream.StreamSupport.stream) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document)

Example 67 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerTest method shouldRemoveEntityById.

@Test
public void shouldRemoveEntityById() {
    DocumentEntity documentEntity = entityManager.insert(DocumentEntityGerator.getEntity());
    Document id = documentEntity.find("_id").get();
    DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(DocumentEntityGerator.COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    entityManager.delete(deleteQuery);
    assertTrue(entityManager.select(query).collect(Collectors.toList()).isEmpty());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 68 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerTest method shouldRemoveEntityByName.

@Test
public void shouldRemoveEntityByName() throws InterruptedException {
    DocumentEntity documentEntity = entityManager.insert(DocumentEntityGerator.getEntity());
    Document name = documentEntity.find("name").get();
    DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).where(name.getName()).eq(name.get()).build();
    DocumentDeleteQuery deleteQuery = delete().from(DocumentEntityGerator.COLLECTION_NAME).where(name.getName()).eq(name.get()).build();
    SECONDS.sleep(1L);
    entityManager.delete(deleteQuery);
    SECONDS.sleep(1L);
    List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
    System.out.println(entities);
    assertTrue(entities.isEmpty());
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Example 69 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerTest method shouldSaveSubDocument2.

@Test
public void shouldSaveSubDocument2() {
    DocumentEntity entity = DocumentEntityGerator.getEntity();
    entity.add(Document.of("phones", Arrays.asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
    DocumentEntity entitySaved = entityManager.insert(entity);
    Document id = entitySaved.find("_id").get();
    DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).where(id.getName()).eq(id.get()).build();
    DocumentEntity entityFound = entityManager.select(query).collect(Collectors.toList()).get(0);
    Document subDocument = entityFound.find("phones").get();
    List<Document> documents = subDocument.get(new TypeReference<List<Document>>() {
    });
    assertThat(documents, containsInAnyOrder(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231")));
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Example 70 with Document

use of jakarta.nosql.document.Document in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentCollectionManagerTest method shouldUpdateSave.

@Test
public void shouldUpdateSave() {
    DocumentEntity entity = DocumentEntityGerator.getEntity();
    entityManager.insert(entity);
    Document newField = Documents.of("newField", "10");
    entity.add(newField);
    DocumentEntity updated = entityManager.update(entity);
    assertEquals(newField, updated.find("newField").get());
}
Also used : DocumentEntity(jakarta.nosql.document.DocumentEntity) Document(jakarta.nosql.document.Document) Test(org.junit.jupiter.api.Test)

Aggregations

Document (jakarta.nosql.document.Document)196 Test (org.junit.jupiter.api.Test)140 DocumentEntity (jakarta.nosql.document.DocumentEntity)115 DocumentQuery (jakarta.nosql.document.DocumentQuery)93 DocumentCondition (jakarta.nosql.document.DocumentCondition)60 List (java.util.List)60 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)44 Arrays.asList (java.util.Arrays.asList)33 ArrayList (java.util.ArrayList)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 ValueSource (org.junit.jupiter.params.provider.ValueSource)26 TypeReference (jakarta.nosql.TypeReference)21 HashMap (java.util.HashMap)20 Map (java.util.Map)17 Collectors (java.util.stream.Collectors)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)16 DocumentQuery.select (jakarta.nosql.document.DocumentQuery.select)14 Matchers.contains (org.hamcrest.Matchers.contains)14 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14