Search in sources :

Example 51 with SearchRequest

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

the class DefaultElasticsearchDocumentCollectionManagerAsync method search.

@Override
public void search(QueryBuilder query, Consumer<List<DocumentEntity>> callBack, String... types) {
    requireNonNull(query, "query is required");
    requireNonNull(callBack, "callBack is required");
    SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(query);
    searchRequest.types(types);
    client.searchAsync(searchRequest, new FindQueryBuilderListener(callBack));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 52 with SearchRequest

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

the class EntityConverter method queryAsync.

static void queryAsync(DocumentQuery query, RestHighLevelClient client, String index, Consumer<List<DocumentEntity>> callBack) {
    FindAsyncListener listener = new FindAsyncListener(callBack, query.getDocumentCollection());
    QueryConverterResult select = QueryConverter.select(query);
    if (!select.getIds().isEmpty()) {
        MultiGetRequest multiGetRequest = new MultiGetRequest();
        select.getIds().stream().map(id -> new MultiGetRequest.Item(index, query.getDocumentCollection(), id)).forEach(multiGetRequest::add);
        client.multiGetAsync(multiGetRequest, listener.getIds());
    }
    if (select.hasStatement()) {
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(query.getDocumentCollection());
        if (select.hasQuery()) {
            setQueryBuilder(query, select, searchRequest);
        }
        client.searchAsync(searchRequest, listener.getSearch());
    }
}
Also used : Document(org.jnosql.diana.api.document.Document) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) StreamSupport(java.util.stream.StreamSupport) Collections.singletonMap(java.util.Collections.singletonMap) ValueUtil(org.jnosql.diana.driver.ValueUtil) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 53 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 54 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 55 with SearchRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest in project logging-log4j2 by apache.

the class LogstashIT method queryDocuments.

private static List<Map<String, Object>> queryDocuments(final RestHighLevelClient client) throws IOException {
    final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(LOG_EVENT_COUNT).fetchSource(true);
    final SearchRequest searchRequest = new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME).source(searchSourceBuilder);
    try {
        final SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        return Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList());
    } catch (ElasticsearchStatusException error) {
        if (RestStatus.NOT_FOUND.equals(error.status())) {
            return Collections.emptyList();
        }
        throw new IOException(error);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)156 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)81 SearchResponse (org.elasticsearch.action.search.SearchResponse)69 Test (org.junit.Test)37 IOException (java.io.IOException)31 SearchHit (org.elasticsearch.search.SearchHit)25 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)22 ArrayList (java.util.ArrayList)21 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 HashMap (java.util.HashMap)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 List (java.util.List)16 SearchHits (org.elasticsearch.search.SearchHits)15 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Map (java.util.Map)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10