Search in sources :

Example 6 with FieldSortBuilder

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();
    }
}
Also used : Terms(org.opensearch.search.aggregations.bucket.terms.Terms) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with FieldSortBuilder

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));
}
Also used : Terms(org.opensearch.search.aggregations.bucket.terms.Terms) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 8 with FieldSortBuilder

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();
    }
}
Also used : Avg(org.opensearch.search.aggregations.metrics.Avg) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with FieldSortBuilder

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;
}
Also used : FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder)

Example 10 with FieldSortBuilder

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();
        }
    }
}
Also used : JobScheduler(org.opensearch.jobscheduler.scheduler.JobScheduler) JobSchedulerSettings(org.opensearch.jobscheduler.JobSchedulerSettings) OpenSearchException(org.opensearch.OpenSearchException) XContentParser(org.opensearch.common.xcontent.XContentParser) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) BackoffPolicy(org.opensearch.action.bulk.BackoffPolicy) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) SearchHit(org.opensearch.search.SearchHit) Collection(java.util.Collection) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Engine(org.opensearch.index.engine.Engine) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) VisibleForTesting(org.opensearch.jobscheduler.utils.VisibleForTesting) XContentType(org.opensearch.common.xcontent.XContentType) Scheduler(org.opensearch.threadpool.Scheduler) ScheduledJobProvider(org.opensearch.jobscheduler.ScheduledJobProvider) BytesReference(org.opensearch.common.bytes.BytesReference) ThreadPool(org.opensearch.threadpool.ThreadPool) LifecycleListener(org.opensearch.common.component.LifecycleListener) ClusterStateListener(org.opensearch.cluster.ClusterStateListener) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JobDocVersion(org.opensearch.jobscheduler.spi.JobDocVersion) ClusterState(org.opensearch.cluster.ClusterState) IndexingOperationListener(org.opensearch.index.shard.IndexingOperationListener) Murmur3HashFunction(org.opensearch.cluster.routing.Murmur3HashFunction) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) LockModel(org.opensearch.jobscheduler.spi.LockModel) ExecutorService(java.util.concurrent.ExecutorService) QueryBuilders(org.opensearch.index.query.QueryBuilders) LockService(org.opensearch.jobscheduler.spi.utils.LockService) Iterator(java.util.Iterator) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) ShardRouting(org.opensearch.cluster.routing.ShardRouting) XContentHelper(org.opensearch.common.xcontent.XContentHelper) ShardId(org.opensearch.index.shard.ShardId) Sets(org.opensearch.common.util.set.Sets) TreeMap(java.util.TreeMap) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ScheduledJobRunner(org.opensearch.jobscheduler.spi.ScheduledJobRunner) LogManager(org.apache.logging.log4j.LogManager) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) JobDocVersion(org.opensearch.jobscheduler.spi.JobDocVersion) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)34 SearchResponse (org.opensearch.action.search.SearchResponse)19 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)13 ArrayList (java.util.ArrayList)8 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)6 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)6 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)5 QueryBuilder (org.opensearch.index.query.QueryBuilder)5 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)5 SearchRequest (org.opensearch.action.search.SearchRequest)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)4 SearchHit (org.opensearch.search.SearchHit)4 IOException (java.io.IOException)3 List (java.util.List)3 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)3 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)3 Map (java.util.Map)2