Search in sources :

Example 1 with ScoreSortBuilder

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

the class CCSDuelIT method testFieldCollapsingSortByField.

public void testFieldCollapsingSortByField() throws Exception {
    assumeMultiClusterSetup();
    SearchRequest searchRequest = initSearchRequest();
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    searchRequest.source(sourceBuilder);
    sourceBuilder.query(QueryBuilders.matchQuery("tags", "ruby"));
    sourceBuilder.sort("creationDate", SortOrder.DESC);
    sourceBuilder.sort(new ScoreSortBuilder());
    sourceBuilder.collapse(new CollapseBuilder("user.keyword"));
    duelSearch(searchRequest, response -> {
        assertHits(response);
        assertEquals(2, response.getHits().getHits()[0].getSortValues().length);
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 2 with ScoreSortBuilder

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

the class TermsAggregatorTests method testWithNestedAggregations.

public void testWithNestedAggregations() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < 10; i++) {
                int[] nestedValues = new int[i];
                for (int j = 0; j < i; j++) {
                    nestedValues[j] = j;
                }
                indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
            }
            indexWriter.commit();
            for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
                for (boolean withScore : new boolean[] { true, false }) {
                    NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.LONG).field("nested_value").collectMode(mode).order(BucketOrder.key(true)).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
                    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
                    try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
                        {
                            InternalNested result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
                            new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
                            InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                            assertNestedTopHitsScore(terms, withScore);
                        }
                        {
                            FilterAggregationBuilder filter = new FilterAggregationBuilder("filter", new MatchAllQueryBuilder()).subAggregation(nested);
                            InternalFilter result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
                            new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), filter, fieldType);
                            InternalNested nestedResult = result.getAggregations().get("nested");
                            InternalMultiBucketAggregation<?, ?> terms = nestedResult.getAggregations().get("terms");
                            assertNestedTopHitsScore(terms, withScore);
                        }
                    }
                }
            }
        }
    }
}
Also used : FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) InternalNested(org.opensearch.search.aggregations.bucket.nested.InternalNested) Aggregator(org.opensearch.search.aggregations.Aggregator) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) InternalFilter(org.opensearch.search.aggregations.bucket.filter.InternalFilter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 3 with ScoreSortBuilder

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

the class SearchDocumentationIT method testSearch.

@SuppressWarnings({ "unused", "unchecked" })
public void testSearch() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::search-request-basic
        // <1>
        SearchRequest searchRequest = new SearchRequest();
        // <2>
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // <3>
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // <4>
        searchRequest.source(searchSourceBuilder);
    // end::search-request-basic
    }
    {
        // tag::search-request-indices
        // <1>
        SearchRequest searchRequest = new SearchRequest("posts");
        // end::search-request-indices
        // tag::search-request-routing
        // <1>
        searchRequest.routing("routing");
        // end::search-request-routing
        // tag::search-request-indicesOptions
        // <1>
        searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
        // end::search-request-indicesOptions
        // tag::search-request-preference
        // <1>
        searchRequest.preference("_local");
        // end::search-request-preference
        assertNotNull(client.search(searchRequest, RequestOptions.DEFAULT));
    }
    {
        // tag::search-source-basics
        // <1>
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // <2>
        sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
        // <3>
        sourceBuilder.from(0);
        // <4>
        sourceBuilder.size(5);
        // <5>
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // end::search-source-basics
        // tag::search-source-sorting
        // <1>
        sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        // <2>
        sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
        // end::search-source-sorting
        // tag::search-source-filtering-off
        sourceBuilder.fetchSource(false);
        // end::search-source-filtering-off
        // tag::search-source-filtering-includes
        String[] includeFields = new String[] { "title", "innerObject.*" };
        String[] excludeFields = new String[] { "user" };
        sourceBuilder.fetchSource(includeFields, excludeFields);
        // end::search-source-filtering-includes
        sourceBuilder.fetchSource(true);
        // tag::search-source-setter
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("posts");
        searchRequest.source(sourceBuilder);
        // end::search-source-setter
        // tag::search-execute
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        // end::search-execute
        // tag::search-execute-listener
        ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::search-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::search-execute-async
        // <1>
        client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
        // end::search-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
        // tag::search-response-1
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();
        // end::search-response-1
        // tag::search-response-2
        int totalShards = searchResponse.getTotalShards();
        int successfulShards = searchResponse.getSuccessfulShards();
        int failedShards = searchResponse.getFailedShards();
        for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
        // failures should be handled here
        }
        // end::search-response-2
        assertNotNull(searchResponse);
        // tag::search-hits-get
        SearchHits hits = searchResponse.getHits();
        // end::search-hits-get
        // tag::search-hits-info
        TotalHits totalHits = hits.getTotalHits();
        // the total number of hits, must be interpreted in the context of totalHits.relation
        long numHits = totalHits.value;
        // whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        // end::search-hits-info
        // tag::search-hits-singleHit
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
        // do something with the SearchHit
        }
        // end::search-hits-singleHit
        for (SearchHit hit : searchHits) {
            // tag::search-hits-singleHit-properties
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            // end::search-hits-singleHit-properties
            // tag::search-hits-singleHit-source
            String sourceAsString = hit.getSourceAsString();
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String documentTitle = (String) sourceAsMap.get("title");
            List<Object> users = (List<Object>) sourceAsMap.get("user");
            Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
        // end::search-hits-singleHit-source
        }
        assertEquals(3, numHits);
        assertEquals(TotalHits.Relation.EQUAL_TO, relation);
        assertNotNull(hits.getHits()[0].getSourceAsString());
        assertNotNull(hits.getHits()[0].getSourceAsMap().get("title"));
        assertNotNull(hits.getHits()[0].getSourceAsMap().get("innerObject"));
        assertNull(hits.getHits()[0].getSourceAsMap().get("user"));
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) RatedSearchHit(org.opensearch.index.rankeval.RatedSearchHit) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RestStatus(org.opensearch.rest.RestStatus) ArrayList(java.util.ArrayList) List(java.util.List) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) SearchHits(org.opensearch.search.SearchHits) Map(java.util.Map) HashMap(java.util.HashMap) TimeValue(org.opensearch.common.unit.TimeValue)

Example 4 with ScoreSortBuilder

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

the class RareTermsAggregatorTests method testWithNestedScoringAggregations.

public void testWithNestedScoringAggregations() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < 10; i++) {
                int[] nestedValues = new int[i];
                for (int j = 0; j < i; j++) {
                    nestedValues[j] = j;
                }
                indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
            }
            indexWriter.commit();
            for (boolean withScore : new boolean[] { true, false }) {
                NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new RareTermsAggregationBuilder("terms").field("nested_value").maxDocCount(2).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
                MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
                try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
                    if (withScore) {
                        IllegalStateException e = expectThrows(IllegalStateException.class, () -> searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType));
                        assertThat(e.getMessage(), equalTo("RareTerms agg [terms] is the child of the nested agg [nested], " + "and also has a scoring child agg [top_hits].  This combination is not supported because it requires " + "executing in [depth_first] mode, which the RareTerms agg cannot do."));
                    } else {
                        InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
                        InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                        assertThat(terms.getBuckets().size(), equalTo(2));
                        long counter = 1;
                        for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
                            InternalTopHits topHits = bucket.getAggregations().get("top_hits");
                            TotalHits hits = topHits.getHits().getTotalHits();
                            assertNotNull(hits);
                            assertThat(hits.value, equalTo(counter));
                            assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
                            counter += 1;
                        }
                    }
                }
            }
        }
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) InternalNested(org.opensearch.search.aggregations.bucket.nested.InternalNested) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) LongPoint(org.apache.lucene.document.LongPoint) InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)

Aggregations

ScoreSortBuilder (org.opensearch.search.sort.ScoreSortBuilder)4 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)3 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 DocValuesFieldExistsQuery (org.apache.lucene.search.DocValuesFieldExistsQuery)2 TotalHits (org.apache.lucene.search.TotalHits)2 Directory (org.apache.lucene.store.Directory)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 InternalNested (org.opensearch.search.aggregations.bucket.nested.InternalNested)2 NestedAggregationBuilder (org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)2 TopHitsAggregationBuilder (org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)1