Search in sources :

Example 91 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project jnosql-diana-driver by eclipse.

the class EntityConverter method executeStatement.

private static void executeStatement(DocumentQuery query, RestHighLevelClient client, String index, QueryConverterResult select, List<DocumentEntity> entities) throws IOException {
    SearchRequest searchRequest = new SearchRequest(index);
    searchRequest.types(query.getDocumentCollection());
    if (select.hasQuery()) {
        setQueryBuilder(query, select, searchRequest);
    }
    SearchResponse response = client.search(searchRequest);
    Stream.of(response.getHits()).flatMap(h -> Stream.of(h.getHits())).map(ElasticsearchEntry::of).filter(ElasticsearchEntry::isNotEmpty).map(ElasticsearchEntry::toEntity).forEach(entities::add);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 92 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project main by JohnPeng739.

the class ElasticAccessorRest method search.

private <T extends Base> SearchResponse search(List<GeneralAccessor.ConditionTuple> tuples, Class<T> clazz, Pagination pagination) throws UserInterfaceDalErrorException {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    if (tuples == null || tuples.isEmpty()) {
        builder.query(QueryBuilders.matchAllQuery());
    } else {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        tuples.forEach(tuple -> query.must(QueryBuilders.termQuery(tuple.field, tuple.value)));
        builder.query(query);
    }
    if (pagination != null) {
        builder.from((pagination.getPage() - 1) * pagination.getSize());
        builder.size(pagination.getSize());
    }
    SearchRequest request = new SearchRequest(index);
    request.types(clazz.getName());
    request.source(builder);
    try {
        SearchResponse response = client.search(request);
        return response;
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Search fail from elastic.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 93 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.

the class ElasticsearchRestClientInstrumentationIT method testDocumentScenario.

@Test
public void testDocumentScenario() throws IOException, ExecutionException, InterruptedException {
    // Index a document
    IndexResponse ir = doIndex(new IndexRequest(INDEX, DOC_TYPE, DOC_ID).source(jsonBuilder().startObject().field(FOO, BAR).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
    assertThat(ir.status().getStatus()).isEqualTo(201);
    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    validateSpanContent(spans.get(0), String.format("Elasticsearch: PUT /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 201, "PUT");
    // Search the index
    reporter.reset();
    SearchRequest searchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    sourceBuilder.from(0);
    sourceBuilder.size(5);
    searchRequest.source(sourceBuilder);
    SearchResponse sr = doSearch(searchRequest);
    assertThat(sr.getHits().totalHits).isEqualTo(1L);
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAR);
    spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span searchSpan = spans.get(0);
    validateSpanContent(searchSpan, String.format("Elasticsearch: GET /%s/_search", INDEX), 200, "GET");
    validateDbContextContent(searchSpan, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
    // Now update and re-search
    reporter.reset();
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put(FOO, BAZ);
    UpdateResponse ur = doUpdate(new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
    assertThat(ur.status().getStatus()).isEqualTo(200);
    sr = doSearch(new SearchRequest(INDEX));
    assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);
    spans = reporter.getSpans();
    assertThat(spans).hasSize(2);
    boolean updateSpanFound = false;
    for (Span span : spans) {
        if (span.getNameAsString().contains("_update")) {
            updateSpanFound = true;
            break;
        }
    }
    assertThat(updateSpanFound).isTrue();
    // Finally - delete the document
    reporter.reset();
    DeleteResponse dr = doDelete(new DeleteRequest(INDEX, DOC_TYPE, DOC_ID));
    validateSpanContent(spans.get(0), String.format("Elasticsearch: DELETE /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 200, "DELETE");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Span(co.elastic.apm.agent.impl.transaction.Span) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) AbstractEsClientInstrumentationTest(co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest) Test(org.junit.Test)

Example 94 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.

the class AbstractEs6_4ClientInstrumentationTest method testMultiSearchRequest_validateSpanContentAndDbContext.

@Test
public void testMultiSearchRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
    createDocument();
    reporter.reset();
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest firstSearchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery(FOO, BAR));
    firstSearchRequest.source(searchSourceBuilder);
    multiSearchRequest.add(firstSearchRequest);
    MultiSearchResponse response = doMultiSearch(multiSearchRequest);
    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span span = spans.get(0);
    validateSpanContent(span, "Elasticsearch: POST /_msearch", 200, "POST");
    verifyMultiSearchSpanContent(span);
    deleteDocument();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Span(co.elastic.apm.agent.impl.transaction.Span) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) AbstractEsClientInstrumentationTest(co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest) Test(org.junit.Test)

Example 95 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.

the class AbstractEs6_4ClientInstrumentationTest method testRollupSearch_validateSpanContentAndDbContext.

@Test
public void testRollupSearch_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
    createDocument();
    reporter.reset();
    SearchRequest rollupSearchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder rollupSearchBuilder = new SearchSourceBuilder();
    rollupSearchBuilder.query(QueryBuilders.termQuery(FOO, BAR));
    rollupSearchBuilder.from(0);
    rollupSearchBuilder.size(5);
    rollupSearchRequest.source(rollupSearchBuilder);
    SearchResponse response = doRollupSearch(rollupSearchRequest);
    verifyTotalHits(response.getHits());
    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span span = spans.get(0);
    validateSpanContent(span, String.format("Elasticsearch: POST /%s/_rollup_search", INDEX), 200, "POST");
    validateDbContextContent(span, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
    deleteDocument();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Span(co.elastic.apm.agent.impl.transaction.Span) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) AbstractEsClientInstrumentationTest(co.elastic.apm.agent.esrestclient.AbstractEsClientInstrumentationTest) Test(org.junit.Test)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)609 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)420 SearchResponse (org.elasticsearch.action.search.SearchResponse)363 IOException (java.io.IOException)201 SearchHit (org.elasticsearch.search.SearchHit)115 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)99 ArrayList (java.util.ArrayList)68 Test (org.junit.Test)57 Test (org.junit.jupiter.api.Test)55 SearchHits (org.elasticsearch.search.SearchHits)53 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)49 TimeValue (org.elasticsearch.common.unit.TimeValue)45 HashMap (java.util.HashMap)42 List (java.util.List)38 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)37 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)35 IndexRequest (org.elasticsearch.action.index.IndexRequest)34 Map (java.util.Map)33 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)30 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)30