Class IndexSearcher
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: 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 IndexSearcherIndexSearcher instance;
use your own (non-Lucene) objects instead.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA class holding a subset of theIndexSearchers leaf contexts to be executed within a single thread. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a searcher searching the provided index.IndexSearcher(IndexReaderContext context) Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReaderContext context, ExecutorService executor) Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReader r, ExecutorService executor) Runs searches for each segment separately, using the provided ExecutorService. -
Method Summary
Modifier and TypeMethodDescriptioncollectionStatistics(String field) ReturnsCollectionStatisticsfor a field.createNormalizedWeight(Query query) Creates a normalized weight for a top-levelQuery.doc(int docID) Sugar for.getIndexReader().document(docID)Sugar for.getIndexReader().document(docID, fieldsToLoad)voiddoc(int docID, StoredFieldVisitor fieldVisitor) Sugar for.getIndexReader().document(docID, fieldVisitor)final DocumentDeprecated.Returns an Explanation that describes howdocscored againstquery.static SimilarityExpert: returns a default Similarity instance.Return theIndexReaderthis searches.Returns this searchers the top-levelIndexReaderContext.Expert: called to re-write queries into primitive queries.Finds the topnhits forquery.Search implementation with arbitrary sorting and no filter.voidLower-level search API.Finds the topnhits forquery, applyingfilterif non-null.Search implementation with arbitrary sorting.Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed.voidLower-level search API.searchAfter(ScoreDoc after, Query query, int n) Finds the topnhits forquerywhere all results are after a previous result (after).searchAfter(ScoreDoc after, Query query, int n, Sort sort) Finds the topnhits forquerywhere all results are after a previous result (after).searchAfter(ScoreDoc after, Query query, Filter filter, int n) Finds the topnhits forquery, applyingfilterif non-null, where all results are after a previous result (after).searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort) Finds the topnhits forquery, applyingfilterif non-null, where all results are after a previous result (after).searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) Finds the topnhits forquerywhere all results are after a previous result (after), allowing control over whether hit scores and max score should be computed.voidsetSimilarity(Similarity similarity) Expert: Set the Similarity implementation used by this IndexSearcher.termStatistics(Term term, TermContext context) ReturnsTermStatisticsfor a term.toString()
-
Constructor Details
-
IndexSearcher
Creates a searcher searching the provided index. -
IndexSearcher
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 usingNIOFSDirectory, 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
Creates a searcher searching the provided top-levelIndexReaderContext.Given a non-
nullExecutorServicethis 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 usingNIOFSDirectory, 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:
-
IndexSearcher
Creates a searcher searching the provided top-levelIndexReaderContext.- See Also:
-
-
Method Details
-
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 respectgetSimilarity(). -
getIndexReader
Return theIndexReaderthis searches. -
doc
Sugar for.getIndexReader().document(docID)- Throws:
IOException- See Also:
-
doc
Sugar for.getIndexReader().document(docID, fieldVisitor)- Throws:
IOException- See Also:
-
doc
Sugar for.getIndexReader().document(docID, fieldsToLoad)- Throws:
IOException- See Also:
-
document
Deprecated.Usedoc(int, Set)instead.- Throws:
IOException
-
setSimilarity
Expert: Set the Similarity implementation used by this IndexSearcher. -
getSimilarity
-
searchAfter
Finds the topnhits forquerywhere 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 exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
searchAfter
Finds the topnhits forquery, applyingfilterif 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 exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Finds the topnhits forquery.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Finds the topnhits forquery, applyingfilterif non-null.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Lower-level search API.Collector.collect(int)is called for every matching document.- Parameters:
query- to match documentsfilter- if non-null, used to permit documents to be collected.results- to receive hits- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Lower-level search API.Collector.collect(int)is called for every matching document.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Search implementation with arbitrary sorting. Finds the topnhits forquery, applyingfilterif non-null, and sorting the hits by the criteria insort.NOTE: this does not compute scores by default; use
search(Query,Filter,int,Sort,boolean,boolean)to control scoring.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
public TopFieldDocs search(Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed. Finds the topnhits forquery, applyingfilterif non-null, and sorting the hits by the criteria insort. IfdoDocScoresistruethen the score of each hit will be computed and returned. IfdoMaxScoreistruethen the maximum score over all collected hits will be computed.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort) throws IOException Finds the topnhits forquery, applyingfilterif 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 exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Search implementation with arbitrary sorting and no filter.- Parameters:
query- The query to search forn- Return only the top n resultssort- TheSortobject- Returns:
- The top docs, sorted according to the supplied
Sortinstance - Throws:
IOException- if there is a low-level I/O error
-
searchAfter
Finds the topnhits forquerywhere 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 exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException Finds the topnhits forquerywhere 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. IfdoDocScoresistruethen the score of each hit will be computed and returned. IfdoMaxScoreistruethen the maximum score over all collected hits will be computed.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
rewrite
Expert: called to re-write queries into primitive queries.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
explain
Returns an Explanation that describes howdocscored againstquery.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:
IOException
-
createNormalizedWeight
Creates a normalized weight for a top-levelQuery. The query is rewritten by this method andQuery.createWeight(org.apache.lucene.search.IndexSearcher)called, afterwards theWeightis normalized. The returnedWeightcan then directly be used to get aScorer.- Throws:
IOException
-
getTopReaderContext
Returns this searchers the top-levelIndexReaderContext.- See Also:
-
toString
-
termStatistics
ReturnsTermStatisticsfor a term. This can be overridden for example, to return a term's statistics across a distributed collection.- Throws:
IOException
-
collectionStatistics
ReturnsCollectionStatisticsfor a field. This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
IOException
-
doc(int, Set)instead.