Search in sources :

Example 96 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class SortingTest method sort_on_multiple_fields.

@Test
public void sort_on_multiple_fields() throws Exception {
    // asc => file asc, line asc, severity desc, key asc
    Sorting sorting = new Sorting();
    sorting.add("fileLine", "file");
    sorting.add("fileLine", "line");
    sorting.add("fileLine", "severity").reverse();
    sorting.add("fileLine", "key").missingLast();
    SearchRequestBuilder request = new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE);
    sorting.fill(request, "fileLine", true);
    List<SortBuilder> fields = fields(request);
    assertThat(fields).hasSize(4);
    expectField(fields.get(0), "file", "_first", SortOrder.ASC);
    expectField(fields.get(1), "line", "_first", SortOrder.ASC);
    expectField(fields.get(2), "severity", "_first", SortOrder.DESC);
    expectField(fields.get(3), "key", "_last", SortOrder.ASC);
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Client(org.elasticsearch.client.Client) Test(org.junit.Test)

Example 97 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project graylog2-server by Graylog2.

the class Searches method terms.

public TermsResult terms(String field, int size, String query, String filter, TimeRange range, Sorting.Direction sorting) {
    Terms.Order termsOrder;
    if (size == 0) {
        size = 50;
    }
    if (sorting == Sorting.Direction.DESC) {
        termsOrder = Terms.Order.count(false);
    } else {
        termsOrder = Terms.Order.count(true);
    }
    SearchRequestBuilder srb;
    if (filter == null) {
        srb = standardSearchRequest(query, determineAffectedIndices(range, null), range);
    } else {
        srb = filteredSearchRequest(query, filter, determineAffectedIndices(range, filter), range);
    }
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.terms(AGG_TERMS).field(field).size(size).order(termsOrder)).subAggregation(AggregationBuilders.missing("missing").field(field)).filter(standardAggregationFilters(range, filter));
    srb.addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new TermsResult(f.getAggregations().get(AGG_TERMS), f.getAggregations().get("missing"), f.getDocCount(), query, request.source(), r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TermsResult(org.graylog2.indexer.results.TermsResult) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 98 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project graylog2-server by Graylog2.

the class Searches method count.

public CountResult count(String query, TimeRange range, String filter) {
    Set<String> indices = determineAffectedIndices(range, filter);
    final SearchRequestBuilder srb;
    if (filter == null) {
        srb = standardSearchRequest(query, indices, range);
    } else {
        srb = filteredSearchRequest(query, filter, indices, range);
    }
    srb.setSize(0);
    final SearchResponse r = c.search(srb.request()).actionGet();
    recordEsMetrics(r, range);
    return CountResult.create(r.getHits().getTotalHits(), r.getTookInMillis());
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 99 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project graylog2-server by Graylog2.

the class Searches method histogram.

public HistogramResult histogram(String query, DateHistogramInterval interval, String filter, TimeRange range) {
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.dateHistogram(AGG_HISTOGRAM).field("timestamp").interval(interval.toESInterval())).filter(standardAggregationFilters(range, filter));
    QueryStringQueryBuilder qs = queryStringQuery(query);
    qs.allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
    final Set<String> affectedIndices = determineAffectedIndices(range, filter);
    final SearchRequestBuilder srb = c.prepareSearch(affectedIndices.toArray(new String[affectedIndices.size()])).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(qs).addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new DateHistogramResult(f.getAggregations().get(AGG_HISTOGRAM), query, request.source(), interval, r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DateHistogramResult(org.graylog2.indexer.results.DateHistogramResult) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 100 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method query.

@Override
public List<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    SearchRequestBuilder srb = client.prepareSearch(indexName);
    srb.setTypes(query.getStore());
    srb.setQuery(QueryBuilders.matchAllQuery());
    srb.setPostFilter(getFilter(query.getCondition(), informations.get(query.getStore())));
    if (!query.getOrder().isEmpty()) {
        List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (int i = 0; i < orders.size(); i++) {
            IndexQuery.OrderEntry orderEntry = orders.get(i);
            FieldSortBuilder fsb = new FieldSortBuilder(orders.get(i).getKey()).order(orderEntry.getOrder() == Order.ASC ? SortOrder.ASC : SortOrder.DESC);
            if (useDeprecatedIgnoreUnmapped) {
                fsb.ignoreUnmapped(true);
            } else {
                Class<?> datatype = orderEntry.getDatatype();
                fsb.unmappedType(convertToEsDataType(datatype));
            }
            srb.addSort(fsb);
        }
    }
    srb.setFrom(0);
    if (query.hasLimit())
        srb.setSize(query.getLimit());
    else
        srb.setSize(maxResultsSize);
    srb.setNoFields();
    //srb.setExplain(true);
    SearchResponse response = srb.execute().actionGet();
    log.debug("Executed query [{}] in {} ms", query.getCondition(), response.getTookInMillis());
    SearchHits hits = response.getHits();
    if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
        log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
    List<String> result = new ArrayList<String>(hits.hits().length);
    for (SearchHit hit : hits) {
        result.add(hit.id());
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7