use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class BucketSortIT method testSortTermsOnKeyWithSize.
public void testSortTermsOnKeyWithSize() {
SearchResponse response = client().prepareSearch(INDEX).setSize(0).addAggregation(terms("foos").field(TERM_FIELD).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("_key"))).size(3))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("foos");
assertThat(terms, notNullValue());
List<? extends Terms.Bucket> termsBuckets = terms.getBuckets();
assertEquals(3, termsBuckets.size());
String previousKey = (String) termsBuckets.get(0).getKey();
for (Terms.Bucket termBucket : termsBuckets) {
assertThat(previousKey, lessThanOrEqualTo((String) termBucket.getKey()));
previousKey = (String) termBucket.getKey();
}
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class BucketSortIT method testEmptyBuckets.
public void testEmptyBuckets() {
SearchResponse response = client().prepareSearch(INDEX).setSize(0).setQuery(QueryBuilders.existsQuery("non-field")).addAggregation(terms("foos").field(TERM_FIELD).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("_key"))))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("foos");
assertThat(terms, notNullValue());
List<? extends Terms.Bucket> termsBuckets = terms.getBuckets();
assertThat(termsBuckets.isEmpty(), is(true));
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class BucketSortIT method testSortTermsOnCountWithSecondarySort.
public void testSortTermsOnCountWithSecondarySort() {
SearchResponse response = client().prepareSearch(INDEX).setSize(0).addAggregation(terms("foos").field(TERM_FIELD).subAggregation(avg("avg_value").field(VALUE_1_FIELD)).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("_count").order(SortOrder.ASC), new FieldSortBuilder("avg_value").order(SortOrder.DESC))))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("foos");
assertThat(terms, notNullValue());
List<? extends Terms.Bucket> termsBuckets = terms.getBuckets();
long previousCount = termsBuckets.get(0).getDocCount();
double previousAvgValue = ((Avg) termsBuckets.get(0).getAggregations().get("avg_value")).getValue();
for (Terms.Bucket termBucket : termsBuckets) {
Avg avg = termBucket.getAggregations().get("avg_value");
assertThat(avg, notNullValue());
assertThat(previousCount, lessThanOrEqualTo(termBucket.getDocCount()));
if (previousCount == termBucket.getDocCount()) {
assertThat(previousAvgValue, greaterThanOrEqualTo(avg.getValue()));
}
previousCount = termBucket.getDocCount();
previousAvgValue = avg.getValue();
}
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class NestedQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link NestedQueryBuilder} with random values all over the place
*/
@Override
protected NestedQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder innerQueryBuilder = RandomQueryBuilder.createQuery(random());
NestedQueryBuilder nqb = new NestedQueryBuilder("nested1", innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
nqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
nqb.innerHit(new InnerHitBuilder(randomAlphaOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(INT_FIELD_NAME).order(SortOrder.ASC)).setIgnoreUnmapped(nqb.ignoreUnmapped()));
}
return nqb;
}
use of org.opensearch.search.sort.FieldSortBuilder in project job-scheduler by opensearch-project.
the class JobSweeper method sweepShard.
private void sweepShard(ShardId shardId, ShardNodes shardNodes, String startAfter) {
ConcurrentHashMap<String, JobDocVersion> currentJobs = this.sweptJobs.containsKey(shardId) ? this.sweptJobs.get(shardId) : new ConcurrentHashMap<>();
for (String jobId : currentJobs.keySet()) {
if (!shardNodes.isOwningNode(jobId)) {
this.scheduler.deschedule(shardId.getIndexName(), jobId);
currentJobs.remove(jobId);
}
}
String searchAfter = startAfter == null ? "" : startAfter;
while (searchAfter != null) {
SearchRequest jobSearchRequest = new SearchRequest().indices(shardId.getIndexName()).preference("_shards:" + shardId.id() + "|_only_local").source(new SearchSourceBuilder().version(true).seqNoAndPrimaryTerm(true).sort(new FieldSortBuilder("_id").unmappedType("keyword").missing("_last")).searchAfter(new String[] { searchAfter }).size(this.sweepPageMaxSize).query(QueryBuilders.matchAllQuery()));
SearchResponse response = this.retry((searchRequest) -> this.client.search(searchRequest), jobSearchRequest, this.sweepSearchBackoff).actionGet(this.sweepSearchTimeout);
if (response.status() != RestStatus.OK) {
log.error("Error sweeping shard {}, failed querying jobs on this shard", shardId);
return;
}
for (SearchHit hit : response.getHits()) {
String jobId = hit.getId();
if (shardNodes.isOwningNode(jobId)) {
this.sweep(shardId, jobId, hit.getSourceRef(), new JobDocVersion(hit.getPrimaryTerm(), hit.getSeqNo(), hit.getVersion()));
}
}
if (response.getHits() == null || response.getHits().getHits().length < 1) {
searchAfter = null;
} else {
SearchHit lastHit = response.getHits().getHits()[response.getHits().getHits().length - 1];
searchAfter = lastHit.getId();
}
}
}
Aggregations