Search in sources :

Example 51 with SearchSourceBuilder

use of org.elasticsearch.search.builder.SearchSourceBuilder in project ff4j by ff4j.

the class ElasticQueryBuilder method queryPropertyByName.

public Search queryPropertyByName(String name) {
    SearchSourceBuilder source = new SearchSourceBuilder();
    source.query(QueryBuilders.matchQuery("name", name));
    return new Search.Builder(source.toString()).addIndex(connection.getIndexName()).addType(ElasticConstants.TYPE_PROPERTY).build();
}
Also used : Search(io.searchbox.core.Search) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 52 with SearchSourceBuilder

use of org.elasticsearch.search.builder.SearchSourceBuilder in project nuxeo-drive-server by nuxeo.

the class ESAuditChangeFinder method getUpperBound.

@Override
public long getUpperBound() {
    SearchRequest request = new SearchRequest(getESIndexName()).types(ElasticSearchConstants.ENTRY_TYPE).searchType(SearchType.DFS_QUERY_THEN_FETCH);
    // TODO refactor this to use max clause
    request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).sort("id", SortOrder.DESC).size(1));
    logSearchRequest(request);
    SearchResponse searchResponse = getClient().search(request);
    logSearchResponse(searchResponse);
    List<LogEntry> entries = new ArrayList<>();
    SearchHits hits = searchResponse.getHits();
    ObjectMapper mapper = new ObjectMapper();
    for (SearchHit hit : hits) {
        try {
            entries.add(mapper.readValue(hit.getSourceAsString(), LogEntryImpl.class));
        } catch (IOException e) {
            log.error("Error while reading Audit Entry from ES", e);
        }
    }
    return entries.size() > 0 ? entries.get(0).getId() : -1;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) LogEntryImpl(org.nuxeo.ecm.platform.audit.impl.LogEntryImpl) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) LogEntry(org.nuxeo.ecm.platform.audit.api.LogEntry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 53 with SearchSourceBuilder

use of org.elasticsearch.search.builder.SearchSourceBuilder 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 54 with SearchSourceBuilder

use of org.elasticsearch.search.builder.SearchSourceBuilder 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 SearchSourceBuilder

use of org.elasticsearch.search.builder.SearchSourceBuilder 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

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)152 SearchResponse (org.elasticsearch.action.search.SearchResponse)77 SearchRequest (org.elasticsearch.action.search.SearchRequest)52 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 Matchers.containsString (org.hamcrest.Matchers.containsString)32 IOException (java.io.IOException)31 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)28 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)26 List (java.util.List)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 Search (io.searchbox.core.Search)23 Map (java.util.Map)23 Set (java.util.Set)19 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)18 Test (org.junit.Test)18 Collections (java.util.Collections)15 SearchHit (org.elasticsearch.search.SearchHit)15 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15