public class IndexSearcher extends Searcher
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 IndexReader.openIfChanged(org.apache.lucene.index.IndexReader)
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 (IndexReader.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 IndexSearcher
IndexSearcher
instance;
use your own (non-Lucene) objects instead.
Modifier and Type | Field and Description |
---|---|
protected int[] |
docStarts |
protected IndexReader[] |
subReaders |
protected IndexSearcher[] |
subSearchers |
Constructor and Description |
---|
IndexSearcher(Directory path)
Deprecated.
use
IndexSearcher(IndexReader) instead. |
IndexSearcher(Directory path,
boolean readOnly)
Deprecated.
Use
IndexSearcher(IndexReader) instead. |
IndexSearcher(IndexReader r)
Creates a searcher searching the provided index.
|
IndexSearcher(IndexReader r,
ExecutorService executor)
Runs searches for each segment separately, using the
provided ExecutorService.
|
IndexSearcher(IndexReader reader,
IndexReader[] subReaders,
int[] docStarts)
Expert: directly specify the reader, subReaders and
their docID starts.
|
IndexSearcher(IndexReader reader,
IndexReader[] subReaders,
int[] docStarts,
ExecutorService executor)
Expert: directly specify the reader, subReaders and
their docID starts, and an ExecutorService.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Note that the underlying IndexReader is not closed, if
IndexSearcher was constructed with IndexSearcher(IndexReader r).
|
Weight |
createNormalizedWeight(Query query)
Creates a normalized weight for a top-level
Query . |
Document |
doc(int docID)
Returns the stored fields of document
i . |
Document |
doc(int docID,
FieldSelector fieldSelector)
Get the
Document at the n th position. |
int |
docFreq(Term term)
Returns total docFreq for this term.
|
Explanation |
explain(Query query,
int doc)
Returns an Explanation that describes how
doc scored against
query . |
Explanation |
explain(Weight weight,
int doc)
Expert: low-level implementation method
Returns an Explanation that describes how
doc scored against
weight . |
protected void |
gatherSubReaders(List<IndexReader> allSubReaders,
IndexReader r) |
IndexReader |
getIndexReader()
Return the
IndexReader this searches. |
Similarity |
getSimilarity()
Expert: Return the Similarity implementation used by this Searcher.
|
IndexReader[] |
getSubReaders()
Returns the atomic subReaders used by this searcher.
|
int |
maxDoc()
Expert: Returns one greater than the largest possible document number.
|
Query |
rewrite(Query original)
Expert: called to re-write queries into primitive queries.
|
void |
search(Query query,
Collector results)
Lower-level search API.
|
void |
search(Query query,
Filter filter,
Collector results)
Lower-level search API.
|
TopDocs |
search(Query query,
Filter filter,
int n)
Finds the top
n
hits for query , applying filter if non-null. |
TopFieldDocs |
search(Query query,
Filter filter,
int n,
Sort sort)
Search implementation with arbitrary sorting.
|
TopDocs |
search(Query query,
int n)
Finds the top
n
hits for query . |
TopFieldDocs |
search(Query query,
int n,
Sort sort)
Search implementation with arbitrary sorting and no filter.
|
void |
search(Weight weight,
Filter filter,
Collector collector)
Lower-level search API.
|
TopDocs |
search(Weight weight,
Filter filter,
int nDocs)
Expert: Low-level search implementation.
|
TopFieldDocs |
search(Weight weight,
Filter filter,
int nDocs,
Sort sort)
Expert: Low-level search implementation with arbitrary sorting.
|
protected TopFieldDocs |
search(Weight weight,
Filter filter,
int nDocs,
Sort sort,
boolean fillFields)
Just like
search(Weight, Filter, int, Sort) , but you choose
whether or not the fields in the returned FieldDoc instances should
be set by specifying fillFields. |
protected TopDocs |
search(Weight weight,
Filter filter,
ScoreDoc after,
int nDocs)
Expert: Low-level search implementation.
|
TopDocs |
searchAfter(ScoreDoc after,
Query query,
Filter filter,
int n)
Finds the top
n
hits for query , applying filter if non-null,
where all results are after a previous result (after ). |
TopDocs |
searchAfter(ScoreDoc after,
Query query,
int n)
Finds the top
n
hits for query where all results are after a previous
result (after ). |
void |
setDefaultFieldSortScoring(boolean doTrackScores,
boolean doMaxScore)
By default, no scores are computed when sorting by
field (using
search(Query,Filter,int,Sort) ). |
void |
setSimilarity(Similarity similarity)
Expert: Set the Similarity implementation used by this Searcher.
|
String |
toString() |
createWeight, docFreqs
protected final IndexReader[] subReaders
protected final int[] docStarts
protected final IndexSearcher[] subSearchers
@Deprecated public IndexSearcher(Directory path) throws CorruptIndexException, IOException
IndexSearcher(IndexReader)
instead.path
- directory where IndexReader will be openedCorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO error@Deprecated public IndexSearcher(Directory path, boolean readOnly) throws CorruptIndexException, IOException
IndexSearcher(IndexReader)
instead.path
- directory where IndexReader will be openedreadOnly
- if true, the underlying IndexReader
will be opened readOnlyCorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO errorpublic IndexSearcher(IndexReader r)
public IndexSearcher(IndexReader r, ExecutorService executor)
NIOFSDirectory
, 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).public IndexSearcher(IndexReader reader, IndexReader[] subReaders, int[] docStarts)
public IndexSearcher(IndexReader reader, IndexReader[] subReaders, int[] docStarts, ExecutorService executor)
NIOFSDirectory
, 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).protected void gatherSubReaders(List<IndexReader> allSubReaders, IndexReader r)
public IndexReader getIndexReader()
IndexReader
this searches.public IndexReader[] getSubReaders()
public int maxDoc()
maxDoc
in interface Searchable
maxDoc
in class Searcher
IndexReader.maxDoc()
public int docFreq(Term term) throws IOException
docFreq
in interface Searchable
docFreq
in class Searcher
IOException
IndexReader.docFreq(Term)
public Document doc(int docID) throws CorruptIndexException, IOException
Searchable
i
.doc
in interface Searchable
doc
in class Searcher
CorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO errorIndexReader.document(int)
public Document doc(int docID, FieldSelector fieldSelector) throws CorruptIndexException, IOException
Searchable
Document
at the n
th position. The FieldSelector
may be used to determine what Field
s to load and how they should be loaded.
NOTE: If the underlying Reader (more specifically, the underlying FieldsReader
) is closed before the lazy Field
is
loaded an exception may be thrown. If you want the value of a lazy Field
to be available after closing you must
explicitly load it or fetch the Document again with a new loader.doc
in interface Searchable
doc
in class Searcher
docID
- Get the document at the n
th positionfieldSelector
- The FieldSelector
to use to determine what Fields should be loaded on the Document. May be null, in which case all Fields will be loaded.Document
at the nth positionCorruptIndexException
- if the index is corruptIOException
- if there is a low-level IO errorIndexReader.document(int, FieldSelector)
,
Fieldable
,
FieldSelector
,
SetBasedFieldSelector
,
LoadFirstFieldSelector
public void setSimilarity(Similarity similarity)
setSimilarity
in class Searcher
Similarity.setDefault(Similarity)
public Similarity getSimilarity()
Searcher
This defaults to the current value of Similarity.getDefault()
.
getSimilarity
in class Searcher
public void close() throws IOException
close
in interface Closeable
close
in interface Searchable
close
in class Searcher
IOException
public TopDocs searchAfter(ScoreDoc after, Query query, int n) throws IOException
n
hits for query
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.
public TopDocs searchAfter(ScoreDoc after, Query query, Filter filter, int n) throws IOException
n
hits for query
, applying filter
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.
public TopDocs search(Query query, int n) throws IOException
n
hits for query
.search
in class Searcher
BooleanQuery.TooManyClauses
IOException
public TopDocs search(Query query, Filter filter, int n) throws IOException
n
hits for query
, applying filter
if non-null.search
in class Searcher
BooleanQuery.TooManyClauses
IOException
public void search(Query query, Filter filter, Collector results) throws IOException
Collector.collect(int)
is called for every matching
document.
Collector-based access to remote indexes is discouraged.
Applications should only use this if they need all of the
matching documents. The high-level search API (Searcher.search(Query, Filter, int)
) is usually more efficient, as it skips
non-high-scoring hits.
search
in class Searcher
query
- to match documentsfilter
- if non-null, used to permit documents to be collected.results
- to receive hitsBooleanQuery.TooManyClauses
IOException
public void search(Query query, Collector results) throws IOException
Collector.collect(int)
is called for every matching document.
Applications should only use this if they need all of the
matching documents. The high-level search API (Searcher.search(Query, int)
) is usually more efficient, as it skips
non-high-scoring hits.
Note: The score
passed to this method is a raw score.
In other words, the score will not necessarily be a float whose value is
between 0 and 1.
search
in class Searcher
BooleanQuery.TooManyClauses
IOException
public TopFieldDocs search(Query query, Filter filter, int n, Sort sort) throws IOException
n
hits for query
, applying
filter
if non-null, and sorting the hits by the criteria in
sort
.
NOTE: this does not compute scores by default; use
setDefaultFieldSortScoring(boolean, boolean)
to
enable scoring.
search
in class Searcher
BooleanQuery.TooManyClauses
IOException
public TopFieldDocs search(Query query, int n, Sort sort) throws IOException
search
in class Searcher
query
- The query to search forn
- Return only the top n resultssort
- The Sort
objectSort
instanceIOException
public TopDocs search(Weight weight, Filter filter, int nDocs) throws IOException
n
hits for query
, applying filter
if non-null.
Applications should usually call Searcher.search(Query,int)
or
Searcher.search(Query,Filter,int)
instead.
search
in interface Searchable
search
in class Searcher
BooleanQuery.TooManyClauses
IOException
protected TopDocs search(Weight weight, Filter filter, ScoreDoc after, int nDocs) throws IOException
n
hits for query
, applying filter
if non-null,
returning results after after
.public TopFieldDocs search(Weight weight, Filter filter, int nDocs, Sort sort) throws IOException
n
hits for query
, applying
filter
if non-null, and sorting the hits by the criteria in
sort
.
Applications should usually call Searcher.search(Query,Filter,int,Sort)
instead.
search
in interface Searchable
search
in class Searcher
BooleanQuery.TooManyClauses
IOException
protected TopFieldDocs search(Weight weight, Filter filter, int nDocs, Sort sort, boolean fillFields) throws IOException
search(Weight, Filter, int, Sort)
, but you choose
whether or not the fields in the returned FieldDoc
instances should
be set by specifying fillFields.
NOTE: this does not compute scores by default. If you
need scores, create a TopFieldCollector
instance by calling TopFieldCollector.create(org.apache.lucene.search.Sort, int, boolean, boolean, boolean, boolean)
and
then pass that to search(Weight, Filter,
Collector)
.
IOException
public void search(Weight weight, Filter filter, Collector collector) throws IOException
Collector.collect(int)
is called for every document.
Collector-based access to remote indexes is discouraged.
Applications should only use this if they need all of the matching
documents. The high-level search API (Searcher.search(Query,int)
) is
usually more efficient, as it skips non-high-scoring hits.
search
in interface Searchable
search
in class Searcher
weight
- to match documentsfilter
- if non-null, used to permit documents to be collected.collector
- to receive hitsBooleanQuery.TooManyClauses
IOException
public Query rewrite(Query original) throws IOException
rewrite
in interface Searchable
rewrite
in class Searcher
BooleanQuery.TooManyClauses
IOException
public Explanation explain(Query query, int doc) throws IOException
doc
scored against
query
.
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.
explain
in class Searcher
IOException
public Explanation explain(Weight weight, int doc) throws IOException
doc
scored against
weight
.
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 Searcher.explain(Query, int)
.
explain
in interface Searchable
explain
in class Searcher
BooleanQuery.TooManyClauses
IOException
public void setDefaultFieldSortScoring(boolean doTrackScores, boolean doMaxScore)
search(Query,Filter,int,Sort)
).
You can change that, per IndexSearcher instance, by
calling this method. Note that this will incur a CPU
cost.doTrackScores
- If true, then scores are
returned for every matching document in TopFieldDocs
.doMaxScore
- If true, then the max score for all
matching docs is computed.public Weight createNormalizedWeight(Query query) throws IOException
Query
.
The query is rewritten by this method and Query.createWeight(org.apache.lucene.search.Searcher)
called,
afterwards the Weight
is normalized. The returned Weight
can then directly be used to get a Scorer
.createNormalizedWeight
in class Searcher
IOException