Search in sources :

Example 31 with DocumentQuery

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

the class QueryAQLConverterTest method shouldRunEqualsQueryLimit.

@Test
public void shouldRunEqualsQueryLimit() {
    DocumentQuery query = select().from("collection").where("name").eq("value").limit(5).build();
    AQLQueryResult convert = QueryAQLConverter.select(query);
    String aql = convert.getQuery();
    Map<String, Object> values = convert.getValues();
    assertEquals("value", values.get("name"));
    assertEquals("FOR c IN collection FILTER  c.name == @name LIMIT 5 RETURN c", aql);
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Test(org.junit.jupiter.api.Test)

Example 32 with DocumentQuery

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

the class QueryAQLConverterTest method shouldRunEqualsQuery.

@Test
public void shouldRunEqualsQuery() {
    DocumentQuery query = select().from("collection").where("name").eq("value").build();
    AQLQueryResult convert = QueryAQLConverter.select(query);
    String aql = convert.getQuery();
    Map<String, Object> values = convert.getValues();
    assertEquals("value", values.get("name"));
    assertEquals("FOR c IN collection FILTER  c.name == @name RETURN c", aql);
}
Also used : DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Test(org.junit.jupiter.api.Test)

Example 33 with DocumentQuery

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

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

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

Aggregations

DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)201 Test (org.junit.jupiter.api.Test)186 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)95 Document (org.jnosql.diana.api.document.Document)84 List (java.util.List)50 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)46 Person (org.jnosql.artemis.model.Person)37 DocumentCondition (org.jnosql.diana.api.document.DocumentCondition)35 Arrays.asList (java.util.Arrays.asList)27 ArrayList (java.util.ArrayList)21 Duration (java.time.Duration)20 DocumentQueryBuilder.select (org.jnosql.diana.api.document.query.DocumentQueryBuilder.select)20 Optional (java.util.Optional)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 DocumentQueryBuilder.delete (org.jnosql.diana.api.document.query.DocumentQueryBuilder.delete)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 Consumer (java.util.function.Consumer)16 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)16