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));
}
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));
}
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()));
}
}
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));
}
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));
}
Aggregations