use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project titan by thinkaurelius.
the class ElasticSearchIndex method query.
@Override
public List<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
SearchRequestBuilder srb = client.prepareSearch(indexName);
srb.setTypes(query.getStore());
srb.setQuery(QueryBuilders.matchAllQuery());
srb.setFilter(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++) {
srb.addSort(new FieldSortBuilder(orders.get(i).getKey()).order(orders.get(i).getOrder() == Order.ASC ? SortOrder.ASC : SortOrder.DESC).ignoreUnmapped(true));
}
}
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;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project sonarqube by SonarSource.
the class RuleIndex method setSorting.
private static void setSorting(RuleQuery query, SearchSourceBuilder esSearch) {
/* integrate Query Sort */
String queryText = query.getQueryText();
if (query.getSortField() != null) {
FieldSortBuilder sort = SortBuilders.fieldSort(appendSortSuffixIfNeeded(query.getSortField()));
if (query.isAscendingSort()) {
sort.order(SortOrder.ASC);
} else {
sort.order(SortOrder.DESC);
}
esSearch.sort(sort);
} else if (StringUtils.isNotEmpty(queryText)) {
esSearch.sort(SortBuilders.scoreSort());
} else {
esSearch.sort(appendSortSuffixIfNeeded(FIELD_RULE_UPDATED_AT), SortOrder.DESC);
// deterministic sort when exactly the same updated_at (same millisecond)
esSearch.sort(appendSortSuffixIfNeeded(FIELD_RULE_KEY), SortOrder.ASC);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class SearchModule method registerSorts.
private void registerSorts() {
namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, GeoDistanceSortBuilder.NAME, GeoDistanceSortBuilder::new));
namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, ScoreSortBuilder.NAME, ScoreSortBuilder::new));
namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, ScriptSortBuilder.NAME, ScriptSortBuilder::new));
namedWriteables.add(new NamedWriteableRegistry.Entry(SortBuilder.class, FieldSortBuilder.NAME, FieldSortBuilder::new));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class HasChildQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link HasChildQueryBuilder} with random values all over the place
*/
@Override
protected HasChildQueryBuilder doCreateTestQueryBuilder() {
int min = randomIntBetween(0, Integer.MAX_VALUE / 2);
int max = randomIntBetween(min, Integer.MAX_VALUE);
QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
if (randomBoolean()) {
requiresRewrite = true;
innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
}
HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_TYPE, innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
hqb.minMaxChildren(min, max);
hqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
hqb.innerHit(new InnerHitBuilder().setName(randomAsciiOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(STRING_FIELD_NAME_2).order(SortOrder.ASC)), hqb.ignoreUnmapped());
}
return hqb;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.sort.FieldSortBuilder in project elasticsearch by elastic.
the class NestedQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link HasChildQueryBuilder} with random values all over the place
*/
@Override
protected NestedQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
if (randomBoolean()) {
requiresRewrite = true;
innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
}
NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
nqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
nqb.innerHit(new InnerHitBuilder().setName(randomAsciiOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(INT_FIELD_NAME).order(SortOrder.ASC)), nqb.ignoreUnmapped());
}
return nqb;
}
Aggregations