Class IndexSearcher
- java.lang.Object
-
- org.apache.lucene.search.IndexSearcher
-
public class IndexSearcher extends 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.
-
Field Summary
Fields Modifier and Type Field Description protected List<AtomicReaderContext>
leafContexts
protected IndexSearcher.LeafSlice[]
leafSlices
used with executor - each slice holds a set of leafs executed within one threadprotected IndexReaderContext
readerContext
-
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, 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
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CollectionStatistics
collectionStatistics(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, Set<String> fieldsToLoad)
Sugar for.getIndexReader().document(docID, fieldsToLoad)
void
doc(int docID, StoredFieldVisitor fieldVisitor)
Sugar for.getIndexReader().document(docID, fieldVisitor)
Document
document(int docID, Set<String> fieldsToLoad)
Deprecated.Usedoc(int, Set)
instead.Explanation
explain(Query query, int doc)
Returns an Explanation that describes howdoc
scored againstquery
.protected Explanation
explain(Weight weight, int doc)
Expert: low-level implementation method Returns an Explanation that describes howdoc
scored againstweight
.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.protected void
search(List<AtomicReaderContext> leaves, Weight weight, Collector collector)
Lower-level search API.protected TopFieldDocs
search(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 returnedFieldDoc
instances should be set by specifying fillFields.protected TopDocs
search(List<AtomicReaderContext> leaves, Weight weight, ScoreDoc after, int nDocs)
Expert: Low-level search implementation.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.protected TopFieldDocs
search(Weight weight, int nDocs, Sort sort, boolean doDocScores, boolean doMaxScore)
Expert: Low-level search implementation with arbitrary sorting and control over whether hit scores and max score should be computed.protected TopFieldDocs
search(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 returnedFieldDoc
instances should be set by specifying fillFields.protected TopDocs
search(Weight weight, ScoreDoc after, int nDocs)
Expert: Low-level search implementation.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.protected IndexSearcher.LeafSlice[]
slices(List<AtomicReaderContext> leaves)
Expert: Creates an array of leaf slices each holding a subset of the given leaves.TermStatistics
termStatistics(Term term, TermContext context)
ReturnsTermStatistics
for a term.String
toString()
protected Query
wrapFilter(Query query, Filter filter)
-
-
-
Field Detail
-
readerContext
protected final IndexReaderContext readerContext
-
leafContexts
protected final List<AtomicReaderContext> leafContexts
-
leafSlices
protected final IndexSearcher.LeafSlice[] leafSlices
used with executor - each slice holds a set of leafs executed within one thread
-
-
Constructor Detail
-
IndexSearcher
public IndexSearcher(IndexReader r)
Creates a searcher searching the provided index.
-
IndexSearcher
public IndexSearcher(IndexReader r, 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, 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()
.
-
slices
protected IndexSearcher.LeafSlice[] slices(List<AtomicReaderContext> leaves)
Expert: Creates an array of leaf slices each holding a subset of the given leaves. EachIndexSearcher.LeafSlice
is executed in a single thread. By default there will be oneIndexSearcher.LeafSlice
per leaf (AtomicReaderContext
).
-
getIndexReader
public IndexReader getIndexReader()
Return theIndexReader
this searches.
-
doc
public Document doc(int docID) throws IOException
Sugar for.getIndexReader().document(docID)
- Throws:
IOException
- See Also:
IndexReader.document(int)
-
doc
public void doc(int docID, StoredFieldVisitor fieldVisitor) throws IOException
Sugar for.getIndexReader().document(docID, fieldVisitor)
- Throws:
IOException
- See Also:
IndexReader.document(int, StoredFieldVisitor)
-
doc
public Document doc(int docID, Set<String> fieldsToLoad) throws IOException
Sugar for.getIndexReader().document(docID, fieldsToLoad)
- Throws:
IOException
- See Also:
IndexReader.document(int, Set)
-
document
@Deprecated public final Document document(int docID, Set<String> fieldsToLoad) throws IOException
Deprecated.Usedoc(int, Set)
instead.- Throws:
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 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.IOException
-
searchAfter
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n) 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
public TopDocs search(Query query, int n) throws IOException
Finds the topn
hits forquery
.- Throws:
BooleanQuery.TooManyClauses
- If a query would exceedBooleanQuery.getMaxClauseCount()
clauses.IOException
-
search
public TopDocs search(Query query, Filter filter, int n) throws IOException
Finds the topn
hits forquery
, applyingfilter
if non-null.- Throws:
BooleanQuery.TooManyClauses
- If a query would exceedBooleanQuery.getMaxClauseCount()
clauses.IOException
-
search
public void search(Query query, Filter filter, Collector results) throws 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.IOException
-
search
public void search(Query query, Collector results) throws IOException
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
public TopFieldDocs search(Query query, Filter filter, int n, Sort sort) throws 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.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
public TopFieldDocs search(Query query, int n, Sort sort) throws IOException
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
public TopDocs searchAfter(ScoreDoc after, Query query, int n, Sort sort) throws 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.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
-
search
protected TopDocs search(Weight weight, ScoreDoc after, int nDocs) throws IOException
Expert: Low-level search implementation. Finds the topn
hits forquery
, applyingfilter
if 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 topn
hits 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 topn
hits forquery
and 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 returnedFieldDoc
instances 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 returnedFieldDoc
instances 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
public Query rewrite(Query original) throws IOException
Expert: called to re-write queries into primitive queries.- Throws:
BooleanQuery.TooManyClauses
- If a query would exceedBooleanQuery.getMaxClauseCount()
clauses.IOException
-
explain
public Explanation explain(Query query, int doc) throws 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:
IOException
-
explain
protected Explanation explain(Weight weight, int doc) throws IOException
Expert: low-level implementation method Returns an Explanation that describes howdoc
scored 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
public Weight createNormalizedWeight(Query query) throws 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:
IOException
-
getTopReaderContext
public IndexReaderContext getTopReaderContext()
Returns this searchers the top-levelIndexReaderContext
.- See Also:
IndexReader.getContext()
-
termStatistics
public TermStatistics termStatistics(Term term, TermContext context) throws IOException
ReturnsTermStatistics
for a term. This can be overridden for example, to return a term's statistics across a distributed collection.- Throws:
IOException
-
collectionStatistics
public CollectionStatistics collectionStatistics(String field) throws IOException
ReturnsCollectionStatistics
for a field. This can be overridden for example, to return a field's statistics across a distributed collection.- Throws:
IOException
-
-