Class IndexSearcher
- Direct Known Subclasses:
SuggestIndexSearcher
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: 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. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static QueryCachingPolicyprivate static QueryCacheprivate static final Similarityprivate final Executorprotected final List<LeafReaderContext> private final IndexSearcher.LeafSlice[]used with executor - each slice holds a set of leafs executed within one threadprivate QueryCacheprivate QueryCachingPolicy(package private) final IndexReaderprotected final IndexReaderContextprivate SimilarityThe Similarity implementation used by this searcher.private static final intBy default we count hits accurately up to 1000. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a searcher searching the provided index.IndexSearcher(IndexReaderContext context) Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReaderContext context, Executor executor) Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReader r, Executor executor) Runs searches for each segment separately, using the provided Executor. -
Method Summary
Modifier and TypeMethodDescriptioncollectionStatistics(String field) ReturnsCollectionStatisticsfor a field, ornullif the field does not exist (has no indexed terms) This can be overridden for example, to return a field's statistics across a distributed collection.intCount how many documents match the given query.createWeight(Query query, ScoreMode scoreMode, float boost) Creates aWeightfor the given query, potentially adding caching if possible and configured.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)Returns an Explanation that describes howdocscored againstquery.protected ExplanationExpert: low-level implementation method Returns an Explanation that describes howdocscored againstweight.static QueryCacheExpert: Get the defaultQueryCacheornullif the cache is disabled.static QueryCachingPolicyExpert: Get the defaultQueryCachingPolicy.static SimilarityExpert: returns a default Similarity instance.Returns this searchers executor ornullif no executor was providedReturn theIndexReaderthis searches.Return the query cache of thisIndexSearcher.Return the query cache of thisIndexSearcher.Expert: Get theSimilarityto use to compute scores.Returns the leaf slices used for concurrent searching, or null if noExecutorwas passed to the constructor.Returns this searchers the top-levelIndexReaderContext.Expert: called to re-write queries into primitive queries.protected voidsearch(List<LeafReaderContext> leaves, Weight weight, Collector collector) Lower-level search API.Finds the topnhits forquery.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.<C extends Collector,T>
Tsearch(Query query, CollectorManager<C, T> collectorManager) Lower-level search API.private TopFieldDocssearchAfter(FieldDoc after, Query query, int numHits, Sort sort, boolean doDocScores) searchAfter(ScoreDoc after, Query query, int numHits) 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, int numHits, Sort sort, boolean doDocScores) Finds the topnhits forquerywhere all results are after a previous result (after), allowing control over whether hit scores and max score should be computed.static voidsetDefaultQueryCache(QueryCache defaultQueryCache) Expert: set the defaultQueryCacheinstance.static voidsetDefaultQueryCachingPolicy(QueryCachingPolicy defaultQueryCachingPolicy) Expert: set the defaultQueryCachingPolicyinstance.voidsetQueryCache(QueryCache queryCache) Set theQueryCacheto use when scores are not needed.voidsetQueryCachingPolicy(QueryCachingPolicy queryCachingPolicy) Set theQueryCachingPolicyto use for query caching.voidsetSimilarity(Similarity similarity) Expert: Set the Similarity implementation used by this IndexSearcher.protected IndexSearcher.LeafSlice[]slices(List<LeafReaderContext> leaves) Expert: Creates an array of leaf slices each holding a subset of the given leaves.termStatistics(Term term, int docFreq, long totalTermFreq) ReturnsTermStatisticsfor a term.final TermStatisticstermStatistics(Term term, TermStates context) Deprecated.toString()
-
Field Details
-
DEFAULT_QUERY_CACHE
-
DEFAULT_CACHING_POLICY
-
TOTAL_HITS_THRESHOLD
private static final int TOTAL_HITS_THRESHOLDBy default we count hits accurately up to 1000. This makes sure that we don't spend most time on computing hit counts- See Also:
-
reader
-
readerContext
-
leafContexts
-
leafSlices
used with executor - each slice holds a set of leafs executed within one thread -
executor
-
defaultSimilarity
-
queryCache
-
queryCachingPolicy
-
similarity
The Similarity implementation used by this searcher.
-
-
Constructor Details
-
IndexSearcher
Creates a searcher searching the provided index. -
IndexSearcher
Runs searches for each segment separately, using the provided Executor. 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-
nullExecutorthis method runs searches for each segment separately, using the provided Executor. 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(). -
getDefaultQueryCache
Expert: Get the defaultQueryCacheornullif the cache is disabled. -
setDefaultQueryCache
Expert: set the defaultQueryCacheinstance. -
getDefaultQueryCachingPolicy
Expert: Get the defaultQueryCachingPolicy. -
setDefaultQueryCachingPolicy
Expert: set the defaultQueryCachingPolicyinstance. -
setQueryCache
Set theQueryCacheto use when scores are not needed. A value ofnullindicates that query matches should never be cached. This method should be called before starting using thisIndexSearcher.NOTE: When using a query cache, queries should not be modified after they have been passed to IndexSearcher.
- See Also:
-
getQueryCache
Return the query cache of thisIndexSearcher. This will be either thedefault query cacheor the query cache that was last set throughsetQueryCache(QueryCache). A return value ofnullindicates that caching is disabled. -
setQueryCachingPolicy
Set theQueryCachingPolicyto use for query caching. This method should be called before starting using thisIndexSearcher.- See Also:
-
getQueryCachingPolicy
Return the query cache of thisIndexSearcher. This will be either thedefault policyor the policy that was last set throughsetQueryCachingPolicy(QueryCachingPolicy). -
slices
Expert: Creates an array of leaf slices each holding a subset of the given leaves. EachIndexSearcher.LeafSliceis executed in a single thread. By default there will be oneIndexSearcher.LeafSliceper leaf (LeafReaderContext). -
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:
-
setSimilarity
Expert: Set the Similarity implementation used by this IndexSearcher. -
getSimilarity
Expert: Get theSimilarityto use to compute scores. This returns theSimilaritythat has been set throughsetSimilarity(Similarity)or the defaultSimilarityif none has been set explicitly. -
count
Count how many documents match the given query.- Throws:
IOException
-
getSlices
Returns the leaf slices used for concurrent searching, or null if noExecutorwas passed to the constructor. -
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
-
search
Finds the topnhits forquery.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
Lower-level search API.LeafCollector.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, plus control over whether hit scores and max score should be computed. Finds the topnhits forquery, 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
-
search
Search implementation with arbitrary sorting.- 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 TopFieldDocs searchAfter(ScoreDoc after, Query query, int numHits, Sort sort, boolean doDocScores) 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
-
searchAfter
private TopFieldDocs searchAfter(FieldDoc after, Query query, int numHits, Sort sort, boolean doDocScores) throws IOException - Throws:
IOException
-
search
public <C extends Collector,T> T search(Query query, CollectorManager<C, T> collectorManager) throws IOExceptionLower-level search API. Search all leaves using the givenCollectorManager. In contrast tosearch(Query, Collector), this method will use the searcher'sExecutorin order to parallelize execution of the collection on the configuredleafSlices.- Throws:
IOException- See Also:
-
search
protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException Lower-level search API.LeafCollector.collect(int)is called for every document.
NOTE: this method executes the searches on all given leaves exclusively. To search across all the searchers leaves use
leafContexts.- Parameters:
leaves- the searchers leaves to execute the searches onweight- to match documentscollector- to receive hits- 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
-
explain
Expert: low-level implementation method Returns an Explanation that describes howdocscored againstweight.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.
Applications should call
explain(Query, int).- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
createWeight
Creates aWeightfor the given query, potentially adding caching if possible and configured.- Throws:
IOException
-
getTopReaderContext
Returns this searchers the top-levelIndexReaderContext.- See Also:
-
toString
-
termStatistics
@Deprecated public final TermStatistics termStatistics(Term term, TermStates context) throws IOException Deprecated.in favor oftermStatistics(Term, int, long).ReturnsTermStatisticsfor a term, ornullif the term does not exist.- Throws:
IOException
-
termStatistics
ReturnsTermStatisticsfor a term.This can be overridden for example, to return a term's statistics across a distributed collection.
- Parameters:
docFreq- The document frequency of the term. It must be greater or equal to 1.totalTermFreq- The total term frequency.- Returns:
- A
TermStatistics(never null). - Throws:
IOException
-
collectionStatistics
ReturnsCollectionStatisticsfor a field, ornullif the field does not exist (has no indexed terms) This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
IOException
-
getExecutor
Returns this searchers executor ornullif no executor was provided
-
termStatistics(Term, int, long).