Class IndexSearcher
- java.lang.Object
-
- org.apache.lucene.search.IndexSearcher
-
public class IndexSearcher extends java.lang.ObjectImplements search over a single IndexReader.Applications usually need only call the inherited
search(Query,int)orsearch(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 useDirectoryReader.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 newIndexReader, 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 theIndexSearcherIndexSearcherinstance; use your own (non-Lucene) objects instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIndexSearcher.LeafSliceA class holding a subset of theIndexSearchers leaf contexts to be executed within a single thread.
-
Constructor Summary
Constructors Constructor Description IndexSearcher(IndexReader r)Creates a searcher searching the provided index.IndexSearcher(IndexReaderContext context)Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReaderContext context, java.util.concurrent.ExecutorService executor)Creates a searcher searching the provided top-levelIndexReaderContext.IndexSearcher(IndexReader r, java.util.concurrent.ExecutorService executor)Runs searches for each segment separately, using the provided ExecutorService.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CollectionStatisticscollectionStatistics(java.lang.String field)ReturnsCollectionStatisticsfor a field.WeightcreateNormalizedWeight(Query query)Creates a normalized weight for a top-levelQuery.Documentdoc(int docID)Sugar for.getIndexReader().document(docID)Documentdoc(int docID, java.util.Set<java.lang.String> fieldsToLoad)Sugar for.getIndexReader().document(docID, fieldsToLoad)voiddoc(int docID, StoredFieldVisitor fieldVisitor)Sugar for.getIndexReader().document(docID, fieldVisitor)Documentdocument(int docID, java.util.Set<java.lang.String> fieldsToLoad)Deprecated.Usedoc(int, Set)instead.Explanationexplain(Query query, int doc)Returns an Explanation that describes howdocscored againstquery.static SimilaritygetDefaultSimilarity()Expert: returns a default Similarity instance.IndexReadergetIndexReader()Return theIndexReaderthis searches.SimilaritygetSimilarity()IndexReaderContextgetTopReaderContext()Returns this searchers the top-levelIndexReaderContext.Queryrewrite(Query original)Expert: called to re-write queries into primitive queries.TopDocssearch(Query query, int n)Finds the topnhits forquery.TopFieldDocssearch(Query query, int n, Sort sort)Search implementation with arbitrary sorting and no filter.voidsearch(Query query, Collector results)Lower-level search API.TopDocssearch(Query query, Filter filter, int n)Finds the topnhits forquery, applyingfilterif non-null.TopFieldDocssearch(Query query, Filter filter, int n, Sort sort)Search implementation with arbitrary sorting.TopFieldDocssearch(Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore)Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed.voidsearch(Query query, Filter filter, Collector results)Lower-level search API.TopDocssearchAfter(ScoreDoc after, Query query, int n)Finds the topnhits forquerywhere all results are after a previous result (after).TopDocssearchAfter(ScoreDoc after, Query query, int n, Sort sort)Finds the topnhits forquerywhere all results are after a previous result (after).TopDocssearchAfter(ScoreDoc after, Query query, Filter filter, int n)Finds the topnhits forquery, applyingfilterif non-null, where all results are after a previous result (after).TopDocssearchAfter(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).TopDocssearchAfter(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.TermStatisticstermStatistics(Term term, TermContext context)ReturnsTermStatisticsfor a term.java.lang.StringtoString()
-
-
-
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 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
public IndexSearcher(IndexReaderContext context, java.util.concurrent.ExecutorService executor)
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:
IndexReaderContext,IndexReader.getContext()
-
IndexSearcher
public IndexSearcher(IndexReaderContext context)
Creates a searcher searching the provided top-levelIndexReaderContext.- 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 respectgetSimilarity().
-
getIndexReader
public IndexReader getIndexReader()
Return theIndexReaderthis searches.
-
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 void doc(int docID, StoredFieldVisitor fieldVisitor) throws java.io.IOExceptionSugar for.getIndexReader().document(docID, fieldVisitor)- Throws:
java.io.IOException- See Also:
IndexReader.document(int, StoredFieldVisitor)
-
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.Usedoc(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 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.java.io.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n) throws java.io.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.java.io.IOException
-
search
public TopDocs search(Query query, int n) throws java.io.IOException
Finds the topnhits forquery.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.java.io.IOException
-
search
public TopDocs search(Query query, Filter filter, int n) throws java.io.IOException
Finds the topnhits forquery, applyingfilterif non-null.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.java.io.IOException
-
search
public void search(Query query, Filter filter, Collector results) throws java.io.IOException
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.java.io.IOException
-
search
public void search(Query query, Collector results) throws java.io.IOException
Lower-level search API.Collector.collect(int)is called for every matching document.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.java.io.IOException
-
search
public TopFieldDocs search(Query query, Filter filter, int n, Sort sort) throws java.io.IOException
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.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 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.java.io.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort) throws java.io.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.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.
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, int n, Sort sort) throws java.io.IOException
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.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 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.java.io.IOException
-
rewrite
public Query rewrite(Query original) throws java.io.IOException
Expert: called to re-write queries into primitive queries.- Throws:
BooleanQuery.TooManyClauses- If a query would exceedBooleanQuery.getMaxClauseCount()clauses.java.io.IOException
-
explain
public Explanation explain(Query query, int doc) throws java.io.IOException
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:
java.io.IOException
-
createNormalizedWeight
public Weight createNormalizedWeight(Query query) throws java.io.IOException
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:
java.io.IOException
-
getTopReaderContext
public IndexReaderContext getTopReaderContext()
Returns this searchers the top-levelIndexReaderContext.- See Also:
IndexReader.getContext()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
termStatistics
public TermStatistics termStatistics(Term term, TermContext context) throws java.io.IOException
ReturnsTermStatisticsfor 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
ReturnsCollectionStatisticsfor a field. This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
java.io.IOException
-
-