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. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<AtomicReaderContext>protected final IndexSearcher.LeafSlice[]used with executor - each slice holds a set of leafs executed within one threadprotected final IndexReaderContext -
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.protected ExplanationExpert: low-level implementation method Returns an Explanation that describes howdocscored againstweight.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.protected voidsearch(List<AtomicReaderContext> leaves, Weight weight, Collector collector) Lower-level search API.protected TopFieldDocssearch(List<AtomicReaderContext> leaves, Weight weight, FieldDoc after, int nDocs, Sort sort, boolean fillFields, boolean doDocScores, boolean doMaxScore) Just likesearch(Weight, int, Sort, boolean, boolean), but you choose whether or not the fields in the returnedFieldDocinstances should be set by specifying fillFields.protected TopDocssearch(List<AtomicReaderContext> leaves, Weight weight, ScoreDoc after, int nDocs) Expert: Low-level search implementation.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.protected TopFieldDocsExpert: Low-level search implementation with arbitrary sorting and control over whether hit scores and max score should be computed.protected TopFieldDocssearch(Weight weight, FieldDoc after, int nDocs, Sort sort, boolean fillFields, boolean doDocScores, boolean doMaxScore) Just likesearch(Weight, int, Sort, boolean, boolean), but you choose whether or not the fields in the returnedFieldDocinstances should be set by specifying fillFields.protected TopDocsExpert: Low-level search implementation.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.protected IndexSearcher.LeafSlice[]slices(List<AtomicReaderContext> leaves) Expert: Creates an array of leaf slices each holding a subset of the given leaves.termStatistics(Term term, TermContext context) ReturnsTermStatisticsfor a term.toString()protected QuerywrapFilter(Query query, Filter filter)
-
Field Details
-
readerContext
-
leafContexts
-
leafSlices
used with executor - each slice holds a set of leafs executed within one thread
-
-
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(). -
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 (AtomicReaderContext). -
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
-
wrapFilter
-
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
-
search
Expert: Low-level search implementation. Finds the topnhits forquery, applyingfilterif non-null.Applications should usually call
search(Query,int)orsearch(Query,Filter,int)instead.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
protected TopDocs search(List<AtomicReaderContext> leaves, Weight weight, ScoreDoc after, int nDocs) throws IOException Expert: Low-level search implementation. Finds the topnhits forquery.Applications should usually call
search(Query,int)orsearch(Query,Filter,int)instead.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
protected TopFieldDocs search(Weight weight, int nDocs, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException Expert: Low-level search implementation with arbitrary sorting and control over whether hit scores and max score should be computed. Finds the topnhits forqueryand sorting the hits by the criteria insort.Applications should usually call
search(Query,Filter,int,Sort)instead.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.IOException
-
search
protected TopFieldDocs search(Weight weight, FieldDoc after, int nDocs, Sort sort, boolean fillFields, boolean doDocScores, boolean doMaxScore) throws IOException Just likesearch(Weight, int, Sort, boolean, boolean), but you choose whether or not the fields in the returnedFieldDocinstances should be set by specifying fillFields.- Throws:
IOException
-
search
protected TopFieldDocs search(List<AtomicReaderContext> leaves, Weight weight, FieldDoc after, int nDocs, Sort sort, boolean fillFields, boolean doDocScores, boolean doMaxScore) throws IOException Just likesearch(Weight, int, Sort, boolean, boolean), but you choose whether or not the fields in the returnedFieldDocinstances should be set by specifying fillFields.- Throws:
IOException
-
search
protected void search(List<AtomicReaderContext> leaves, Weight weight, Collector collector) throws IOException Lower-level search API.Collector.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
-
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.