use of org.elasticsearch.search.sort.FieldSortBuilder in project uavstack by uavorg.
the class SlowOperQueryHandler method buildSort.
@SuppressWarnings({ "rawtypes" })
private SortBuilder[] buildSort(UAVHttpMessage data) {
SortBuilder[] sorts = null;
String sort = data.getRequest("sort");
if (sort != null) {
String[] sortFieldStrs = sort.split(",");
List<SortBuilder> ls = new ArrayList<SortBuilder>();
for (String sortFieldStr : sortFieldStrs) {
String[] sortExp = sortFieldStr.split("=");
SortBuilder stimeSort = new FieldSortBuilder(sortExp[0]);
stimeSort.order(SortOrder.fromString(sortExp[1]));
ls.add(stimeSort);
}
sorts = new SortBuilder[ls.size()];
sorts = ls.toArray(sorts);
} else {
return null;
}
return sorts;
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project uavstack by uavorg.
the class ThreadAnalysisQueryHandler method buildSorts.
@SuppressWarnings("rawtypes")
private List<SortBuilder> buildSorts(UAVHttpMessage data) {
List<SortBuilder> list = new ArrayList<>();
String sort = data.getRequest("sort");
if (sort != null) {
String[] sortFieldStrs = sort.split(",");
for (String sortFieldStr : sortFieldStrs) {
String[] sortExp = sortFieldStr.split("=");
SortBuilder stimeSort = new FieldSortBuilder(sortExp[0]);
stimeSort.order(SortOrder.fromString(sortExp[1]));
list.add(stimeSort);
}
} else {
SortBuilder stimeSort = new FieldSortBuilder("time");
stimeSort.order(SortOrder.DESC);
list.add(stimeSort);
}
return list;
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project alien4cloud by alien4cloud.
the class AbstractToscaIndexSearchService method search.
public FacetedSearchResult search(Class<? extends T> clazz, String query, Integer size, Map<String, String[]> filters) {
TopHitsBuilder topHitAggregation = AggregationBuilders.topHits("highest_version").setSize(1).addSort(new FieldSortBuilder("nestedVersion.majorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.minorVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.incrementalVersion").order(SortOrder.DESC)).addSort(new FieldSortBuilder("nestedVersion.qualifier").order(SortOrder.DESC).missing("_first"));
AggregationBuilder aggregation = AggregationBuilders.terms("query_aggregation").field(getAggregationField()).size(size).subAggregation(topHitAggregation);
FacetedSearchResult<? extends T> searchResult = alienDAO.buildSearchQuery(clazz, query).setFilters(FilterUtil.singleKeyFilter(filters, "workspace", AlienConstants.GLOBAL_WORKSPACE_ID)).prepareSearch().setFetchContext(FetchContext.SUMMARY, topHitAggregation).facetedSearch(new IAggregationQueryManager() {
@Override
public AggregationBuilder getQueryAggregation() {
return aggregation;
}
@Override
@SneakyThrows({ IOException.class })
public void setData(ObjectMapper objectMapper, Function getClassFromType, FacetedSearchResult result, Aggregation aggregation) {
List<Object> resultData = Lists.newArrayList();
List<String> resultTypes = Lists.newArrayList();
if (aggregation == null) {
result.setData(getArray(0));
result.setTypes(new String[0]);
}
for (Terms.Bucket bucket : safe(((Terms) aggregation).getBuckets())) {
TopHits topHits = bucket.getAggregations().get("highest_version");
for (SearchHit hit : topHits.getHits()) {
resultTypes.add(hit.getType());
resultData.add(objectMapper.readValue(hit.getSourceAsString(), ((Function<String, Class>) getClassFromType).apply(hit.getType())));
}
}
result.setData(resultData.toArray(getArray(resultData.size())));
result.setTypes(resultTypes.toArray(new String[resultTypes.size()]));
result.setFrom(0);
result.setTo(resultData.size());
if (size == Integer.MAX_VALUE || resultData.size() < size) {
result.setTotalResults(resultData.size());
} else {
// just to show that there is more results to fetch but iteration is not possible through aggregations.
result.setTotalResults(resultData.size() + ((Terms) aggregation).getSumOfOtherDocCounts());
}
}
});
return searchResult;
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project Anserini by castorini.
the class SearchElastic method searchTweets.
public <K> ScoredDocuments searchTweets(String queryString, long t) {
SearchHits results = null;
String specials = "+-=&|><!(){}[]^\"~*?:\\/";
for (int i = 0; i < specials.length(); i++) {
char c = specials.charAt(i);
queryString = queryString.replace(String.valueOf(c), " ");
}
// Do not consider the tweets with tweet ids that are beyond the queryTweetTime
// <querytweettime> tag contains the timestamp of the query in terms of the
// chronologically nearest tweet id within the corpus
RangeQueryBuilder queryTweetTime = QueryBuilders.rangeQuery(TweetGenerator.TweetField.ID_LONG.name).from(0L).to(t);
QueryStringQueryBuilder queryTerms = QueryBuilders.queryStringQuery(queryString).defaultField("contents").analyzer("english");
BoolQueryBuilder query = QueryBuilders.boolQuery().filter(queryTweetTime).should(queryTerms);
SearchRequest searchRequest = new SearchRequest(args.esIndex);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query);
sourceBuilder.size(args.hits);
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
sourceBuilder.sort(new FieldSortBuilder(TweetGenerator.TweetField.ID_LONG.name).order(SortOrder.DESC));
searchRequest.source(sourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, COMMON_OPTIONS);
results = searchResponse.getHits();
} catch (Exception e) {
LOG.error("Exception during ES query: ", e);
}
ScoreTiesAdjusterReranker reranker = new ScoreTiesAdjusterReranker();
return reranker.rerank(ScoredDocuments.fromESDocs(results), null);
}
use of org.elasticsearch.search.sort.FieldSortBuilder in project graylog2-server by Graylog2.
the class ESMessageList method doGenerateQueryPart.
@Override
public void doGenerateQueryPart(SearchJob job, Query query, MessageList messageList, ESGeneratedQueryContext queryContext) {
final SearchSourceBuilder searchSourceBuilder = queryContext.searchSourceBuilder(messageList).size(messageList.limit()).from(messageList.offset());
applyHighlightingIfActivated(searchSourceBuilder, job, query);
final Set<String> effectiveStreamIds = messageList.effectiveStreams().isEmpty() ? query.usedStreamIds() : messageList.effectiveStreams();
if (!messageList.fields().isEmpty()) {
searchSourceBuilder.fetchSource(messageList.fields().toArray(new String[0]), new String[0]);
}
final List<Sort> sorts = firstNonNull(messageList.sort(), Collections.singletonList(Sort.create(Message.FIELD_TIMESTAMP, Sort.Order.DESC)));
sorts.forEach(sort -> {
final FieldSortBuilder fieldSort = SortBuilders.fieldSort(sort.field()).order(toSortOrder(sort.order()));
final Optional<String> fieldType = queryContext.fieldType(effectiveStreamIds, sort.field());
searchSourceBuilder.sort(fieldType.map(fieldSort::unmappedType).orElse(fieldSort));
});
}
Aggregations