Class IndexSearcher

java.lang.Object
org.apache.lucene.search.IndexSearcher
Direct Known Subclasses:
QueryProfilerIndexSearcher, SuggestIndexSearcher

public class IndexSearcher extends Object
Implements search over a single IndexReader.

Applications usually need only call the inherited search(Query,int) method. For performance reasons, if your index is unchanging, you should share a single IndexSearcher instance across multiple searches instead of creating a new one per-search. If your index has changed and you wish to see the changes reflected in searching, you should use DirectoryReader.openIfChanged(DirectoryReader) to obtain a new reader and then create a new IndexSearcher from that. Also, for low-latency turnaround it's best to use a near-real-time reader (DirectoryReader.open(IndexWriter)). Once you have a new IndexReader, it's relatively cheap to create a new IndexSearcher from it.

NOTE: The search(org.apache.lucene.search.Query, int) and searchAfter(org.apache.lucene.search.ScoreDoc, org.apache.lucene.search.Query, int) methods are configured to only count top hits accurately up to 1,000 and may return a lower bound of the hit count if the hit count is greater than or equal to 1,000. On queries that match lots of documents, counting the number of hits may take much longer than computing the top hits so this trade-off allows to get some minimal information about the hit count without slowing down search too much. The TopDocs.scoreDocs array is always accurate however. If this behavior doesn't suit your needs, you should create collectors manually with either TopScoreDocCollector.create(int, int) or TopFieldCollector.create(org.apache.lucene.search.Sort, int, int) and call search(Query, Collector).

NOTE: IndexSearcher instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on the IndexSearcher instance; use your own (non-Lucene) objects instead.

  • Field Details

    • maxClauseCount

      static int maxClauseCount
    • DEFAULT_QUERY_CACHE

      private static QueryCache DEFAULT_QUERY_CACHE
    • DEFAULT_CACHING_POLICY

      private static QueryCachingPolicy DEFAULT_CACHING_POLICY
    • queryTimeout

      private QueryTimeout queryTimeout
    • partialResult

      private volatile boolean partialResult
    • TOTAL_HITS_THRESHOLD

      private static final int TOTAL_HITS_THRESHOLD
      By default we count hits accurately up to 1000. This makes sure that we don't spend most time on computing hit counts
      See Also:
    • MAX_DOCS_PER_SLICE

      private static final int MAX_DOCS_PER_SLICE
      Thresholds for index slice allocation logic. To change the default, extend IndexSearcher and use custom values
      See Also:
    • MAX_SEGMENTS_PER_SLICE

      private static final int MAX_SEGMENTS_PER_SLICE
      See Also:
    • reader

      final IndexReader reader
    • readerContext

      protected final IndexReaderContext readerContext
    • leafContexts

      protected final List<LeafReaderContext> leafContexts
    • leafSlicesSupplier

      private final Supplier<IndexSearcher.LeafSlice[]> leafSlicesSupplier
      Used with executor - LeafSlice supplier where each slice holds a set of leafs executed within one thread. We are caching it instead of creating it eagerly to avoid calling a protected method from constructor, which is a bad practice. Always non-null, regardless of whether an executor is provided or not.
    • executor

      private final Executor executor
    • taskExecutor

      private final TaskExecutor taskExecutor
    • defaultSimilarity

      private static final Similarity defaultSimilarity
    • queryCache

      private QueryCache queryCache
    • queryCachingPolicy

      private QueryCachingPolicy queryCachingPolicy
    • similarity

      private Similarity similarity
      The Similarity implementation used by this searcher.
  • Constructor Details

  • Method Details