Search in sources :

Example 16 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortIT method testSortHistogram_GivenGapsAndGapPolicyIsSkip.

public void testSortHistogram_GivenGapsAndGapPolicyIsSkip() {
    SearchResponse response = client().prepareSearch(INDEX_WITH_GAPS).addAggregation(histogram("time_buckets").field(TIME_FIELD).interval(1).subAggregation(avg("avg_value").field(VALUE_1_FIELD)).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("avg_value").order(SortOrder.DESC))).gapPolicy(BucketHelpers.GapPolicy.SKIP))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> timeBuckets = histo.getBuckets();
    assertThat(timeBuckets.size(), equalTo(2));
    assertThat(timeBuckets.get(0).getKey(), equalTo(3.0));
    assertThat(timeBuckets.get(1).getKey(), equalTo(1.0));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 17 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortIT method testSortHistogram_GivenGapsAndGapPolicyIsSkipAndSizeIsLessThanAvailableBuckets.

public void testSortHistogram_GivenGapsAndGapPolicyIsSkipAndSizeIsLessThanAvailableBuckets() {
    SearchResponse response = client().prepareSearch(INDEX_WITH_GAPS).addAggregation(histogram("time_buckets").field(TIME_FIELD).interval(1).subAggregation(avg("avg_value").field(VALUE_1_FIELD)).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("avg_value").order(SortOrder.DESC))).gapPolicy(BucketHelpers.GapPolicy.SKIP).size(2))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> timeBuckets = histo.getBuckets();
    assertThat(timeBuckets.size(), equalTo(2));
    assertThat(timeBuckets.get(0).getKey(), equalTo(3.0));
    assertThat(timeBuckets.get(1).getKey(), equalTo(1.0));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 18 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortIT method testSortDateHistogramDescending.

public void testSortDateHistogramDescending() {
    SearchResponse response = client().prepareSearch(INDEX).addAggregation(dateHistogram("time_buckets").field(TIME_FIELD).interval(TimeValue.timeValueHours(1).millis())).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> ascendingTimeBuckets = histo.getBuckets();
    response = client().prepareSearch(INDEX).addAggregation(dateHistogram("time_buckets").field(TIME_FIELD).interval(TimeValue.timeValueHours(1).millis()).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("_key").order(SortOrder.DESC))))).get();
    assertSearchResponse(response);
    histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> descendingTimeBuckets = histo.getBuckets();
    assertThat(ascendingTimeBuckets.size(), equalTo(descendingTimeBuckets.size()));
    int bucketCount = ascendingTimeBuckets.size();
    for (int i = 0; i < bucketCount; ++i) {
        assertThat(ascendingTimeBuckets.get(i).getKey(), equalTo(descendingTimeBuckets.get(bucketCount - i - 1).getKey()));
    }
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 19 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortIT method testSortHistogram_GivenGapsAndGapPolicyIsSkipAndSecondarySortHasGaps.

public void testSortHistogram_GivenGapsAndGapPolicyIsSkipAndSecondarySortHasGaps() {
    SearchResponse response = client().prepareSearch(INDEX_WITH_GAPS).addAggregation(histogram("time_buckets").field(TIME_FIELD).interval(1).subAggregation(avg("avg_value_1").field(VALUE_1_FIELD)).subAggregation(avg("avg_value_2").field(VALUE_2_FIELD)).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("avg_value_2").order(SortOrder.DESC), new FieldSortBuilder("avg_value_1").order(SortOrder.ASC))).gapPolicy(BucketHelpers.GapPolicy.SKIP))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> timeBuckets = histo.getBuckets();
    assertThat(timeBuckets.size(), equalTo(3));
    assertThat(timeBuckets.get(0).getKey(), equalTo(1.0));
    assertThat(timeBuckets.get(1).getKey(), equalTo(3.0));
    assertThat(timeBuckets.get(2).getKey(), equalTo(2.0));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 20 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortIT method testSortHistogram_GivenGapsAndGapPolicyIsSkipAndPrimarySortHasGaps.

public void testSortHistogram_GivenGapsAndGapPolicyIsSkipAndPrimarySortHasGaps() {
    SearchResponse response = client().prepareSearch(INDEX_WITH_GAPS).addAggregation(histogram("time_buckets").field(TIME_FIELD).interval(1).subAggregation(avg("avg_value_1").field(VALUE_1_FIELD)).subAggregation(avg("avg_value_2").field(VALUE_2_FIELD)).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("avg_value_1").order(SortOrder.DESC), new FieldSortBuilder("avg_value_2").order(SortOrder.DESC))).gapPolicy(BucketHelpers.GapPolicy.SKIP))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("time_buckets");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("time_buckets"));
    List<? extends Histogram.Bucket> timeBuckets = histo.getBuckets();
    assertThat(timeBuckets.size(), equalTo(3));
    assertThat(timeBuckets.get(0).getKey(), equalTo(3.0));
    assertThat(timeBuckets.get(1).getKey(), equalTo(1.0));
    assertThat(timeBuckets.get(2).getKey(), equalTo(2.0));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) 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