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 IndexSearcher
IndexSearcher
instance;
use your own (non-Lucene) objects instead.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A class holding a subset of theIndexSearcher
s 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) ReturnsCollectionStatistics
for 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)
void
doc
(int docID, StoredFieldVisitor fieldVisitor) Sugar for.getIndexReader().document(docID, fieldVisitor)
final Document
Deprecated.Returns an Explanation that describes howdoc
scored againstquery
.static Similarity
Expert: returns a default Similarity instance.Return theIndexReader
this searches.Returns this searchers the top-levelIndexReaderContext
.Expert: called to re-write queries into primitive queries.Finds the topn
hits forquery
.Search implementation with arbitrary sorting and no filter.void
Lower-level search API.Finds the topn
hits forquery
, applyingfilter
if non-null.Search implementation with arbitrary sorting.Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed.void
Lower-level search API.searchAfter
(ScoreDoc after, Query query, int n) Finds the topn
hits forquery
where all results are after a previous result (after
).searchAfter
(ScoreDoc after, Query query, int n, Sort sort) Finds the topn
hits forquery
where all results are after a previous result (after
).searchAfter
(ScoreDoc after, Query query, Filter filter, int n) Finds the topn
hits forquery
, applyingfilter
if non-null, where all results are after a previous result (after
).searchAfter
(ScoreDoc after, Query query, Filter filter, int n, Sort sort) Finds the topn
hits forquery
, applyingfilter
if 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 topn
hits forquery
where all results are after a previous result (after
), allowing control over whether hit scores and max score should be computed.void
setSimilarity
(Similarity similarity) Expert: Set the Similarity implementation used by this IndexSearcher.termStatistics
(Term term, TermContext context) ReturnsTermStatistics
for 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-
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 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 theIndexReader
this 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 topn
hits forquery
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
-
searchAfter
Finds the topn
hits forquery
, applyingfilter
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 exceedBooleanQuery.getMaxClauseCount()
clauses.IOException
-
search
Finds the topn
hits forquery
.- Throws:
BooleanQuery.TooManyClauses
- If a query would exceedBooleanQuery.getMaxClauseCount()
clauses.IOException
-
search
Finds the topn
hits forquery
, applyingfilter
if 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 topn
hits forquery
, applyingfilter
if 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 topn
hits forquery
, applyingfilter
if non-null, and sorting the hits by the criteria insort
. IfdoDocScores
istrue
then the score of each hit will be computed and returned. IfdoMaxScore
istrue
then 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 topn
hits forquery
, applyingfilter
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 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
- TheSort
object- Returns:
- The top docs, sorted according to the supplied
Sort
instance - Throws:
IOException
- if there is a low-level I/O error
-
searchAfter
Finds the topn
hits forquery
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
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException Finds the topn
hits forquery
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. IfdoDocScores
istrue
then the score of each hit will be computed and returned. IfdoMaxScore
istrue
then 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 howdoc
scored 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 theWeight
is normalized. The returnedWeight
can then directly be used to get aScorer
.- Throws:
IOException
-
getTopReaderContext
Returns this searchers the top-levelIndexReaderContext
.- See Also:
-
toString
-
termStatistics
ReturnsTermStatistics
for a term. This can be overridden for example, to return a term's statistics across a distributed collection.- Throws:
IOException
-
collectionStatistics
ReturnsCollectionStatistics
for a field. This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
IOException
-
doc(int, Set)
instead.