Search in sources :

Example 21 with FieldSortBuilder

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));
}
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 22 with FieldSortBuilder

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()));
    }
}
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 23 with FieldSortBuilder

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);
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 24 with FieldSortBuilder

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;
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 25 with FieldSortBuilder

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;
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

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