Interface IndexSearcher

All Known Implementing Classes:
IndexSearcherWithStats, LuceneIndexSearcher, OpenSearchIndexSearcher

@PublicApi public interface IndexSearcher
Provides a mean to retrieve documents from search index. e.g.

     Document doc = indexSearcher.get(GetRequest.builder()
          .id("253")
          .fields(TITLE, BODY, CREATION)
          .build()).get();
     Collection<String> titles = TITLE.get(doc);
     Collection<Long> creations = CREATION.get(doc);
 
Since:
10.4
  • Method Details

    • search

      SearchResponse search(SearchRequest request, PageRequest pageRequest) throws SearchException
      Retrieves a search response that includes a collection of document hits and aggregated results.

      By default, the hits are sorted in descending order of relevance score produced by the query ranking. The amount of hits returned is controlled by PageRequest.

      Parameters:
      request - request describing the query, optional sort order, fields to retrieve and aggregation request
      pageRequest - the page request
      Returns:
      SearchResponse containing the ordered list of document hits and any relevant aggregated data.
      Throws:
      SearchException
    • searchStream

      SearchStreamResponse searchStream(SearchRequest request) throws SearchException
      Retrieves a search response that provides streamable access to document hits and aggregated results.

      By default, the hits are sorted in descending order of relevance score produced by the query ranking.

      It is important to close the resulting response after use in order to clean up its resources. We recommend using try-with-resources.

      Performance note: this operation has an overhead over the normal search, so use it only when you specifically need to iterate through a large number of data, for example for data exports or migrations.

      Also consider a search with aggregations instead of computing it with this stream operation. Search aggregation API off-loads the computation to the search index, which is vastly more efficient than streaming all the documents, especially on OpenSearch.

      Sorting is not supported when streaming results and will result in a SearchException being thrown.

      Parameters:
      request - request describing the query, optional sort order, fields to retrieve and aggregation request
      Returns:
      SearchStreamResponse containing the ordered stream of document hits and any relevant aggregated data.
      Throws:
      SearchException
    • getHitCount

      long getHitCount(SearchRequest request, @Nullable Long timeout) throws SearchException
      Return the number of documents that match the request
      Parameters:
      request - request describing the query on which document should be counted
      timeout - request timeout in milliseconds, or null if no timeout
      Returns:
      the number of documents satisfying the query
      Throws:
      SearchException - if search requests takes longer than timeout