Search in sources :

Example 96 with DocumentQuery

use of jakarta.nosql.document.DocumentQuery 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 97 with DocumentQuery

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

the class ElasticsearchDocumentCollectionManagerTest method shouldFindOrderByNameDesc.

@Test
public void shouldFindOrderByNameDesc() throws InterruptedException {
    final DocumentEntity poliana = DocumentEntityGerator.getEntity();
    final DocumentEntity otavio = DocumentEntityGerator.getEntity();
    poliana.add("name", "poliana");
    otavio.add("name", "otavio");
    otavio.add("_id", "id2");
    entityManager.insert(Arrays.asList(poliana, otavio));
    SECONDS.sleep(1L);
    DocumentQuery query = DocumentQuery.select().from("person").orderBy("name").desc().build();
    String[] names = entityManager.select(query).map(d -> d.find("name")).filter(Optional::isPresent).map(Optional::get).map(d -> d.get(String.class)).toArray(String[]::new);
    assertArrayEquals(names, new String[] { "poliana", "otavio" });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) DocumentQuery.select(jakarta.nosql.document.DocumentQuery.select) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) ArrayList(java.util.ArrayList) TypeReference(jakarta.nosql.TypeReference) DocumentQuery(jakarta.nosql.document.DocumentQuery) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Documents(org.eclipse.jnosql.communication.document.Documents) Document(jakarta.nosql.document.Document) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) List(java.util.List) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) DocumentDeleteQuery.delete(jakarta.nosql.document.DocumentDeleteQuery.delete) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SECONDS(java.util.concurrent.TimeUnit.SECONDS) DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) Optional(java.util.Optional) Test(org.junit.jupiter.api.Test)

Example 98 with DocumentQuery

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

the class ElasticsearchDocumentCollectionManagerTest method shouldRetrieveListSubdocumentList.

@Test
public void shouldRetrieveListSubdocumentList() {
    DocumentEntity entity = entityManager.insert(createSubdocumentList());
    Document key = entity.find("_id").get();
    DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).where(key.getName()).eq(key.get()).build();
    DocumentEntity documentEntity = entityManager.singleResult(query).get();
    assertNotNull(documentEntity);
    List<List<Document>> contacts = (List<List<Document>>) documentEntity.find("contacts").get().get();
    assertEquals(3, contacts.size());
    assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) DocumentQuery.select(jakarta.nosql.document.DocumentQuery.select) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) ArrayList(java.util.ArrayList) TypeReference(jakarta.nosql.TypeReference) DocumentQuery(jakarta.nosql.document.DocumentQuery) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Documents(org.eclipse.jnosql.communication.document.Documents) Document(jakarta.nosql.document.Document) DocumentEntity(jakarta.nosql.document.DocumentEntity) Collectors(java.util.stream.Collectors) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) List(java.util.List) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) DocumentDeleteQuery.delete(jakarta.nosql.document.DocumentDeleteQuery.delete) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SECONDS(java.util.concurrent.TimeUnit.SECONDS) 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 99 with DocumentQuery

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

the class EntityConverter method executeStatement.

private static Stream<DocumentEntity> executeStatement(DocumentQuery query, RestHighLevelClient client, String index, QueryConverterResult select) throws IOException {
    SearchRequest searchRequest = new SearchRequest(index);
    setQueryBuilder(query, select, searchRequest);
    SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
    return Stream.of(response.getHits()).flatMap(h -> Stream.of(h.getHits())).map(ElasticsearchEntry::of).filter(ElasticsearchEntry::isNotEmpty).map(ElasticsearchEntry::toEntity);
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) Document(jakarta.nosql.document.Document) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) SearchRequest(org.elasticsearch.action.search.SearchRequest) DocumentEntity(jakarta.nosql.document.DocumentEntity) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ValueUtil(org.eclipse.jnosql.communication.driver.ValueUtil) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DocumentQuery(jakarta.nosql.document.DocumentQuery) ASC(jakarta.nosql.SortType.ASC) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SortOrder(org.elasticsearch.search.sort.SortOrder) StreamSupport(java.util.stream.StreamSupport) Collections.singletonMap(java.util.Collections.singletonMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 100 with DocumentQuery

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

the class MongoDBDocumentCollectionManagerTest method shouldFindDocumentLesserEqualsThan.

@Test
public void shouldFindDocumentLesserEqualsThan() {
    DocumentDeleteQuery deleteQuery = delete().from(COLLECTION_NAME).where("type").eq("V").build();
    entityManager.delete(deleteQuery);
    Iterable<DocumentEntity> entitiesSaved = entityManager.insert(getEntitiesWithValues());
    List<DocumentEntity> entities = StreamSupport.stream(entitiesSaved.spliterator(), false).collect(Collectors.toList());
    DocumentQuery query = select().from(COLLECTION_NAME).where("age").lte(23).and("type").eq("V").build();
    List<DocumentEntity> entitiesFound = entityManager.select(query).collect(Collectors.toList());
    assertTrue(entitiesFound.size() == 2);
    assertThat(entitiesFound, contains(entities.get(0), entities.get(2)));
}
Also used : DocumentQuery(jakarta.nosql.document.DocumentQuery) DocumentEntity(jakarta.nosql.document.DocumentEntity) DocumentDeleteQuery(jakarta.nosql.document.DocumentDeleteQuery) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentQuery (jakarta.nosql.document.DocumentQuery)292 Test (org.junit.jupiter.api.Test)234 DocumentEntity (jakarta.nosql.document.DocumentEntity)123 Document (jakarta.nosql.document.Document)98 DocumentCondition (jakarta.nosql.document.DocumentCondition)64 DocumentDeleteQuery (jakarta.nosql.document.DocumentDeleteQuery)55 Person (jakarta.nosql.tck.entities.Person)50 List (java.util.List)50 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)44 Arrays.asList (java.util.Arrays.asList)34 ValueSource (org.junit.jupiter.params.provider.ValueSource)30 Pagination (jakarta.nosql.mapping.Pagination)27 ArrayList (java.util.ArrayList)26 Collectors (java.util.stream.Collectors)19 DocumentQuery.select (jakarta.nosql.document.DocumentQuery.select)18 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 Matchers.contains (org.hamcrest.Matchers.contains)16 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)16 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)16 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)16