Class IndexSearcher
- java.lang.Object
-
- org.apache.lucene.search.IndexSearcher
-
public class IndexSearcher extends java.lang.Object
Implements 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 theIndexSearcher
IndexSearcher
instance; use your own (non-Lucene) objects instead.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IndexSearcher.LeafSlice
A class holding a subset of theIndexSearcher
s 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 CollectionStatistics
collectionStatistics(java.lang.String field)
ReturnsCollectionStatistics
for a field.Weight
createNormalizedWeight(Query query)
Creates a normalized weight for a top-levelQuery
.Document
doc(int docID)
Sugar for.getIndexReader().document(docID)
Document
doc(int docID, java.util.Set<java.lang.String> fieldsToLoad)
Sugar for.getIndexReader().document(docID, fieldsToLoad)
void
doc(int docID, StoredFieldVisitor fieldVisitor)
Sugar for.getIndexReader().document(docID, fieldVisitor)
Document
document(int docID, java.util.Set<java.lang.String> fieldsToLoad)
Deprecated.Usedoc(int, Set)
instead.Explanation
explain(Query query, int doc)
Returns an Explanation that describes howdoc
scored againstquery
.static Similarity
getDefaultSimilarity()
Expert: returns a default Similarity instance.IndexReader
getIndexReader()
Return theIndexReader
this searches.Similarity
getSimilarity()
IndexReaderContext
getTopReaderContext()
Returns this searchers the top-levelIndexReaderContext
.Query
rewrite(Query original)
Expert: called to re-write queries into primitive queries.TopDocs
search(Query query, int n)
Finds the topn
hits forquery
.TopFieldDocs
search(Query query, int n, Sort sort)
Search implementation with arbitrary sorting and no filter.void
search(Query query, Collector results)
Lower-level search API.TopDocs
search(Query query, Filter filter, int n)
Finds the topn
hits forquery
, applyingfilter
if non-null.TopFieldDocs
search(Query query, Filter filter, int n, Sort sort)
Search implementation with arbitrary sorting.TopFieldDocs
search(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.void
search(Query query, Filter filter, Collector results)
Lower-level search API.TopDocs
searchAfter(ScoreDoc after, Query query, int n)
Finds the topn
hits forquery
where all results are after a previous result (after
).TopDocs
searchAfter(ScoreDoc after, Query query, int n, Sort sort)
Finds the topn
hits forquery
where all results are after a previous result (after
).TopDocs
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
).TopDocs
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
).TopDocs
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
termStatistics(Term term, TermContext context)
ReturnsTermStatistics
for a term.java.lang.String
toString()
-
-
-
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-
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:
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 theIndexReader
this 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.IOException
Sugar 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 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.java.io.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n) throws java.io.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.java.io.IOException
-
search
public TopDocs search(Query query, int n) throws java.io.IOException
Finds the topn
hits 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 topn
hits forquery
, applyingfilter
if 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 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.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 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.java.io.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n, Sort sort) throws java.io.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.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 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.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 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.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 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:
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 theWeight
is normalized. The returnedWeight
can 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:
toString
in classjava.lang.Object
-
termStatistics
public TermStatistics termStatistics(Term term, TermContext context) throws java.io.IOException
ReturnsTermStatistics
for 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
ReturnsCollectionStatistics
for a field. This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
java.io.IOException
-
-