use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class BucketSortIT method testSortHistogram_GivenGapsAndGapPolicyIsInsertZeros.
public void testSortHistogram_GivenGapsAndGapPolicyIsInsertZeros() {
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.INSERT_ZEROS))).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));
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class BucketSortIT method testSortTermsOnSubAggregation.
public void testSortTermsOnSubAggregation() {
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("avg_value").order(SortOrder.DESC))))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("foos");
assertThat(terms, notNullValue());
List<? extends Terms.Bucket> termsBuckets = terms.getBuckets();
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(previousAvgValue, greaterThanOrEqualTo(avg.getValue()));
previousAvgValue = avg.getValue();
}
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("avg_value").order(SortOrder.DESC))).size(2).from(3))).get();
assertSearchResponse(response);
Terms size2From3Terms = response.getAggregations().get("foos");
assertThat(size2From3Terms, notNullValue());
List<? extends Terms.Bucket> size2From3TermsBuckets = size2From3Terms.getBuckets();
for (int i = 0; i < size2From3TermsBuckets.size(); ++i) {
assertThat(size2From3TermsBuckets.get(i).getKey(), equalTo(termsBuckets.get(i + 3).getKey()));
}
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class HasChildQueryBuilderTests method testFromJson.
public void testFromJson() throws IOException {
String query = "{\n" + " \"has_child\" : {\n" + " \"query\" : {\n" + " \"range\" : {\n" + " \"mapped_string\" : {\n" + " \"from\" : \"agJhRET\",\n" + " \"to\" : \"zvqIq\",\n" + " \"include_lower\" : true,\n" + " \"include_upper\" : true,\n" + " \"boost\" : 1.0\n" + " }\n" + " }\n" + " },\n" + " \"type\" : \"child\",\n" + " \"score_mode\" : \"avg\",\n" + " \"min_children\" : 883170873,\n" + " \"max_children\" : 1217235442,\n" + " \"ignore_unmapped\" : false,\n" + " \"boost\" : 2.0,\n" + " \"_name\" : \"WNzYMJKRwePuRBh\",\n" + " \"inner_hits\" : {\n" + " \"name\" : \"inner_hits_name\",\n" + " \"ignore_unmapped\" : false,\n" + " \"from\" : 0,\n" + " \"size\" : 100,\n" + " \"version\" : false,\n" + " \"seq_no_primary_term\" : false,\n" + " \"explain\" : false,\n" + " \"track_scores\" : false,\n" + " \"sort\" : [ {\n" + " \"mapped_string\" : {\n" + " \"order\" : \"asc\"\n" + " }\n" + " } ]\n" + " }\n" + " }\n" + "}";
HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query);
checkGeneratedJson(query, queryBuilder);
assertEquals(query, queryBuilder.maxChildren(), 1217235442);
assertEquals(query, queryBuilder.minChildren(), 883170873);
assertEquals(query, queryBuilder.boost(), 2.0f, 0.0f);
assertEquals(query, queryBuilder.queryName(), "WNzYMJKRwePuRBh");
assertEquals(query, queryBuilder.childType(), "child");
assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg);
assertNotNull(query, queryBuilder.innerHit());
InnerHitBuilder expected = new InnerHitBuilder("child").setName("inner_hits_name").setSize(100).addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC));
assertEquals(query, queryBuilder.innerHit(), expected);
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class HasChildQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link HasChildQueryBuilder} with random values all over the place
*/
@Override
protected HasChildQueryBuilder doCreateTestQueryBuilder() {
int min = randomIntBetween(1, Integer.MAX_VALUE / 2);
int max = randomIntBetween(min, Integer.MAX_VALUE);
QueryBuilder innerQueryBuilder = new MatchAllQueryBuilder();
if (randomBoolean()) {
requiresRewrite = true;
innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
}
HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_DOC, innerQueryBuilder, RandomPicks.randomFrom(random(), ScoreMode.values()));
hqb.minMaxChildren(min, max);
hqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
hqb.innerHit(new InnerHitBuilder().setName(randomAlphaOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC)));
}
return hqb;
}
use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.
the class HasParentQueryBuilderTests method doCreateTestQueryBuilder.
/**
* @return a {@link HasChildQueryBuilder} with random values all over the place
*/
@Override
protected HasParentQueryBuilder doCreateTestQueryBuilder() {
QueryBuilder innerQueryBuilder = new MatchAllQueryBuilder();
if (randomBoolean()) {
requiresRewrite = true;
innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString());
}
HasParentQueryBuilder hqb = new HasParentQueryBuilder(PARENT_DOC, innerQueryBuilder, randomBoolean());
hqb.ignoreUnmapped(randomBoolean());
if (randomBoolean()) {
hqb.innerHit(new InnerHitBuilder().setName(randomAlphaOfLengthBetween(1, 10)).setSize(randomIntBetween(0, 100)).addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC)));
}
return hqb;
}
Aggregations