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);
}
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());
}
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());
}
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());
}
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;
}
Aggregations