Search in sources :

Example 1 with FieldSortBuilder

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

the class InnerHitsIT method testRandomNested.

public void testRandomNested() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
    int numDocs = scaledRandomIntBetween(25, 100);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int[] field1InnerObjects = new int[numDocs];
    int[] field2InnerObjects = new int[numDocs];
    for (int i = 0; i < numDocs; i++) {
        int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        XContentBuilder source = jsonBuilder().startObject().field("foo", i).startArray("field1");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
        source.endArray().startArray("field2");
        for (int j = 0; j < numInnerObjects; j++) {
            source.startObject().field("x", "y").endObject();
        }
        source.endArray().endObject();
        requestBuilders.add(client().prepareIndex("idx").setId(Integer.toString(i)).setSource(source));
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.ASC))));
    boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.ASC)).setSize(size)));
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("foo", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    for (int i = 0; i < numDocs; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits().value, equalTo((long) field1InnerObjects[i]));
        for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits().value, equalTo((long) field2InnerObjects[i]));
        for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
            SearchHit innerHit = inner.getAt(j);
            assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
            assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
            assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
        }
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchHit(org.opensearch.search.SearchHit) OpenSearchAssertions.assertSearchHit(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchHits(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with FieldSortBuilder

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

the class RemoteRequestBuilders method initialSearch.

static Request initialSearch(SearchRequest searchRequest, BytesReference query, Version remoteVersion) {
    // It is nasty to build paths with StringBuilder but we'll be careful....
    StringBuilder path = new StringBuilder("/");
    addIndices(path, searchRequest.indices());
    path.append("_search");
    Request request = new Request("POST", path.toString());
    if (searchRequest.scroll() != null) {
        TimeValue keepAlive = searchRequest.scroll().keepAlive();
        // V_5_0_0
        if (remoteVersion.before(Version.fromId(5000099))) {
            /* Versions of Elasticsearch before 5.0 couldn't parse nanos or micros
                 * so we toss out that resolution, rounding up because more scroll
                 * timeout seems safer than less. */
            keepAlive = timeValueMillis((long) Math.ceil(keepAlive.millisFrac()));
        }
        request.addParameter("scroll", keepAlive.getStringRep());
    }
    request.addParameter("size", Integer.toString(searchRequest.source().size()));
    if (searchRequest.source().version() == null || searchRequest.source().version() == false) {
        request.addParameter("version", Boolean.FALSE.toString());
    } else {
        request.addParameter("version", Boolean.TRUE.toString());
    }
    if (searchRequest.source().sorts() != null) {
        boolean useScan = false;
        // Detect if we should use search_type=scan rather than a sort
        if (remoteVersion.before(Version.fromId(2010099))) {
            for (SortBuilder<?> sort : searchRequest.source().sorts()) {
                if (sort instanceof FieldSortBuilder) {
                    FieldSortBuilder f = (FieldSortBuilder) sort;
                    if (f.getFieldName().equals(FieldSortBuilder.DOC_FIELD_NAME)) {
                        useScan = true;
                        break;
                    }
                }
            }
        }
        if (useScan) {
            request.addParameter("search_type", "scan");
        } else {
            StringBuilder sorts = new StringBuilder(sortToUri(searchRequest.source().sorts().get(0)));
            for (int i = 1; i < searchRequest.source().sorts().size(); i++) {
                sorts.append(',').append(sortToUri(searchRequest.source().sorts().get(i)));
            }
            request.addParameter("sort", sorts.toString());
        }
    }
    if (remoteVersion.before(Version.fromId(2000099))) {
        // Versions before 2.0.0 need prompting to return interesting fields. Note that timestamp isn't available at all....
        searchRequest.source().storedField("_parent").storedField("_routing").storedField("_ttl");
        if (remoteVersion.before(Version.fromId(1000099))) {
            // Versions before 1.0.0 don't support `"_source": true` so we have to ask for the _source in a funny way.
            if (false == searchRequest.source().storedFields().fieldNames().contains("_source")) {
                searchRequest.source().storedField("_source");
            }
        }
    }
    if (searchRequest.source().storedFields() != null && false == searchRequest.source().storedFields().fieldNames().isEmpty()) {
        StringBuilder fields = new StringBuilder(searchRequest.source().storedFields().fieldNames().get(0));
        for (int i = 1; i < searchRequest.source().storedFields().fieldNames().size(); i++) {
            fields.append(',').append(searchRequest.source().storedFields().fieldNames().get(i));
        }
        // V_5_0_0
        String storedFieldsParamName = remoteVersion.before(Version.fromId(5000099)) ? "fields" : "stored_fields";
        request.addParameter(storedFieldsParamName, fields.toString());
    }
    if (remoteVersion.onOrAfter(Version.fromId(6030099))) {
        // allow_partial_results introduced in 6.3, running remote reindex against earlier versions still silently discards RED shards.
        request.addParameter("allow_partial_search_results", "false");
    }
    // EMPTY is safe here because we're not calling namedObject
    try (XContentBuilder entity = JsonXContent.contentBuilder();
        XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, query)) {
        entity.startObject();
        entity.field("query");
        {
            /* We're intentionally a bit paranoid here - copying the query
                 * as xcontent rather than writing a raw field. We don't want
                 * poorly written queries to escape. Ever. */
            entity.copyCurrentStructure(queryParser);
            XContentParser.Token shouldBeEof = queryParser.nextToken();
            if (shouldBeEof != null) {
                throw new OpenSearchException("query was more than a single object. This first token after the object is [" + shouldBeEof + "]");
            }
        }
        if (searchRequest.source().fetchSource() != null) {
            entity.field("_source", searchRequest.source().fetchSource());
        } else {
            if (remoteVersion.onOrAfter(Version.fromId(1000099))) {
                // Versions before 1.0 don't support `"_source": true` so we have to ask for the source as a stored field.
                entity.field("_source", true);
            }
        }
        entity.endObject();
        request.setJsonEntity(Strings.toString(entity));
    } catch (IOException e) {
        throw new OpenSearchException("unexpected error building entity", e);
    }
    return request;
}
Also used : Request(org.opensearch.client.Request) SearchRequest(org.opensearch.action.search.SearchRequest) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) IOException(java.io.IOException) OpenSearchException(org.opensearch.OpenSearchException) TimeValue(org.opensearch.common.unit.TimeValue) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with FieldSortBuilder

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

the class SearchProgressActionListenerIT method testSearchProgressWithShardSort.

public void testSearchProgressWithShardSort() throws Exception {
    SearchRequest request = new SearchRequest("index-*").source(new SearchSourceBuilder().size(0).sort(new FieldSortBuilder("number").order(SortOrder.DESC)));
    request.setPreFilterShardSize(1);
    List<SearchShard> sortShards = new ArrayList<>(shards);
    Collections.sort(sortShards, Comparator.reverseOrder());
    testCase((NodeClient) client(), request, sortShards, false);
}
Also used : ArrayList(java.util.ArrayList) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 4 with FieldSortBuilder

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

the class BucketSortIT method testSortTermsOnKey.

public void testSortTermsOnKey() {
    SearchResponse response = client().prepareSearch(INDEX).setSize(0).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();
    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 5 with FieldSortBuilder

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

the class BucketSortIT method testInvalidPath.

public void testInvalidPath() {
    Exception e = expectThrows(ActionRequestValidationException.class, () -> client().prepareSearch(INDEX).addAggregation(terms("foos").field(TERM_FIELD).subAggregation(bucketSort("bucketSort", Arrays.asList(new FieldSortBuilder("invalid"))))).get());
    assertThat(e.getMessage(), containsString("No aggregation found for path [invalid]"));
}
Also used : FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IOException(java.io.IOException)

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