Class IndexSearcher


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

    Applications usually need only call the inherited search(Query,int) or search(Query,Filter,int) methods. 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,boolean)). Once you have a new IndexReader, it's relatively cheap to create a new IndexSearcher from it.

    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.

    • Constructor Detail

      • IndexSearcher

        public IndexSearcher​(IndexReader r)
        Creates a searcher searching the provided index.
      • IndexSearcher

        public IndexSearcher​(IndexReader r,
                             java.util.concurrent.ExecutorService executor)
        Runs searches for each segment separately, using the provided ExecutorService. IndexSearcher will not shutdown/awaitTermination this ExecutorService on close; you must do so, eventually, on your own. NOTE: if you are using NIOFSDirectory, do not use the shutdownNow method of ExecutorService as this uses Thread.interrupt under-the-hood which can silently close file descriptors (see LUCENE-2239).
      • IndexSearcher

        public IndexSearcher​(IndexReaderContext context,
                             java.util.concurrent.ExecutorService executor)
        Creates a searcher searching the provided top-level IndexReaderContext.

        Given a non-null ExecutorService this method runs searches for each segment separately, using the provided ExecutorService. IndexSearcher will not shutdown/awaitTermination this ExecutorService on close; you must do so, eventually, on your own. NOTE: if you are using NIOFSDirectory, do not use the shutdownNow method of ExecutorService as this uses Thread.interrupt under-the-hood which can silently close file descriptors (see LUCENE-2239).

        See Also:
        IndexReaderContext, IndexReader.getContext()
    • Method Detail

      • getDefaultSimilarity

        public static Similarity getDefaultSimilarity()
        Expert: returns a default Similarity instance. In general, this method is only called to initialize searchers and writers. User code and query implementations should respect getSimilarity().
      • doc

        public Document doc​(int docID)
                     throws java.io.IOException
        Sugar for .getIndexReader().document(docID)
        Throws:
        java.io.IOException
        See Also:
        IndexReader.document(int)
      • doc

        public Document doc​(int docID,
                            java.util.Set<java.lang.String> fieldsToLoad)
                     throws java.io.IOException
        Sugar for .getIndexReader().document(docID, fieldsToLoad)
        Throws:
        java.io.IOException
        See Also:
        IndexReader.document(int, Set)
      • document

        @Deprecated
        public final Document document​(int docID,
                                       java.util.Set<java.lang.String> fieldsToLoad)
                                throws java.io.IOException
        Deprecated.
        Use doc(int, Set) instead.
        Throws:
        java.io.IOException
      • setSimilarity

        public void setSimilarity​(Similarity similarity)
        Expert: Set the Similarity implementation used by this IndexSearcher.
      • getSimilarity

        public Similarity getSimilarity()
      • searchAfter

        public TopDocs searchAfter​(ScoreDoc after,
                                   Query query,
                                   int n)
                            throws java.io.IOException
        Finds the top n hits for query where all results are after a previous result (after).

        By passing the bottom result from a previous page as after, this method can be used for efficient 'deep-paging' across potentially large result sets.

        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • searchAfter

        public TopDocs searchAfter​(ScoreDoc after,
                                   Query query,
                                   Filter filter,
                                   int n)
                            throws java.io.IOException
        Finds the top n hits for query, applying filter if non-null, where all results are after a previous result (after).

        By passing the bottom result from a previous page as after, this method can be used for efficient 'deep-paging' across potentially large result sets.

        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • search

        public TopFieldDocs search​(Query query,
                                   Filter filter,
                                   int n,
                                   Sort sort,
                                   boolean doDocScores,
                                   boolean doMaxScore)
                            throws java.io.IOException
        Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed. Finds the top n hits for query, applying filter if non-null, and sorting the hits by the criteria in sort. If doDocScores is true then the score of each hit will be computed and returned. If doMaxScore is true then the maximum score over all collected hits will be computed.
        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • searchAfter

        public TopDocs searchAfter​(ScoreDoc after,
                                   Query query,
                                   Filter filter,
                                   int n,
                                   Sort sort)
                            throws java.io.IOException
        Finds the top n hits for query, applying filter if non-null, where all results are after a previous result (after).

        By passing the bottom result from a previous page as after, this method can be used for efficient 'deep-paging' across potentially large result sets.

        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • search

        public TopFieldDocs search​(Query query,
                                   int n,
                                   Sort sort)
                            throws java.io.IOException
        Search implementation with arbitrary sorting and no filter.
        Parameters:
        query - The query to search for
        n - Return only the top n results
        sort - The Sort object
        Returns:
        The top docs, sorted according to the supplied Sort instance
        Throws:
        java.io.IOException - if there is a low-level I/O error
      • searchAfter

        public TopDocs searchAfter​(ScoreDoc after,
                                   Query query,
                                   int n,
                                   Sort sort)
                            throws java.io.IOException
        Finds the top n hits for query where all results are after a previous result (after).

        By passing the bottom result from a previous page as after, this method can be used for efficient 'deep-paging' across potentially large result sets.

        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • searchAfter

        public TopDocs searchAfter​(ScoreDoc after,
                                   Query query,
                                   Filter filter,
                                   int n,
                                   Sort sort,
                                   boolean doDocScores,
                                   boolean doMaxScore)
                            throws java.io.IOException
        Finds the top n hits for query where all results are after a previous result (after), allowing control over whether hit scores and max score should be computed.

        By passing the bottom result from a previous page as after, this method can be used for efficient 'deep-paging' across potentially large result sets. If doDocScores is true then the score of each hit will be computed and returned. If doMaxScore is true then the maximum score over all collected hits will be computed.

        Throws:
        BooleanQuery.TooManyClauses - If a query would exceed BooleanQuery.getMaxClauseCount() clauses.
        java.io.IOException
      • explain

        public Explanation explain​(Query query,
                                   int doc)
                            throws java.io.IOException
        Returns an Explanation that describes how doc scored against query.

        This is intended to be used in developing Similarity implementations, and, for good performance, should not be displayed with every hit. Computing an explanation is as expensive as executing the query over the entire index.

        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • termStatistics

        public TermStatistics termStatistics​(Term term,
                                             TermContext context)
                                      throws java.io.IOException
        Returns TermStatistics for a term. This can be overridden for example, to return a term's statistics across a distributed collection.
        Throws:
        java.io.IOException
      • collectionStatistics

        public CollectionStatistics collectionStatistics​(java.lang.String field)
                                                  throws java.io.IOException
        Returns CollectionStatistics for a field. This can be overridden for example, to return a field's statistics across a distributed collection.
        Throws:
        java.io.IOException