org.apache.lucene.search |
Code to search indices.
Table Of Contents
- Search Basics
- The Query Classes
- Changing the Scoring
Search
Search over indices.
Applications usually call {@link
org.apache.lucene.search.Searcher#search(Query)} or {@link
org.apache.lucene.search.Searcher#search(Query,Filter)}.
Query Classes
Of the various implementations of
Query, the
TermQuery
is the easiest to understand and the most often used in applications. A TermQuery matches all the documents that contain the
specified
Term,
which is a word that occurs in a certain
Field.
Thus, a TermQuery identifies and scores all
Documents that have a Field with the specified string in it.
Constructing a TermQuery
is as simple as:
TermQuery tq = new TermQuery(new Term("fieldName", "term"));
In this example, the Query identifies all Documents that have the Field named "fieldName"
containing the word "term".
Things start to get interesting when one combines multiple
TermQuery instances into a BooleanQuery.
A BooleanQuery contains multiple
BooleanClauses,
where each clause contains a sub-query (Query
instance) and an operator (from BooleanClause.Occur)
describing how that sub-query is combined with the other clauses:
SHOULD — Use this operator when a clause can occur in the result set, but is not required.
If a query is made up of all SHOULD clauses, then every document in the result
set matches at least one of these clauses.
MUST — Use this operator when a clause is required to occur in the result set. Every
document in the result set will match
all such clauses.
MUST NOT — Use this operator when a
clause must not occur in the result set. No
document in the result set will match
any such clauses.
Boolean queries are constructed by adding two or more
BooleanClause
instances. If too many clauses are added, a TooManyClauses
exception will be thrown during searching. This most often occurs
when a Query
is rewritten into a BooleanQuery with many
TermQuery clauses,
for example by WildcardQuery.
The default setting for the maximum number
of clauses 1024, but this can be changed via the
static method setMaxClauseCount
in BooleanQuery.
Phrases
Another common search is to find documents containing certain phrases. This
is handled two different ways:
-
PhraseQuery
— Matches a sequence of
Terms.
PhraseQuery uses a slop factor to determine
how many positions may occur between any two terms in the phrase and still be considered a match.
-
SpanNearQuery
— Matches a sequence of other
SpanQuery
instances. SpanNearQuery allows for
much more
complicated phrase queries since it is constructed from other SpanQuery
instances, instead of only TermQuery
instances.
The
RangeQuery
matches all documents that occur in the
exclusive range of a lower
Term
and an upper
Term.
For example, one could find all documents
that have terms beginning with the letters a through c. This type of Query is frequently used to
find
documents that occur in a specific date range.
While the
PrefixQuery
has a different implementation, it is essentially a special case of the
WildcardQuery.
The PrefixQuery allows an application
to identify all documents with terms that begin with a certain string. The WildcardQuery generalizes this by allowing
for the use of * (matches 0 or more characters) and ? (matches exactly one character) wildcards.
Note that the WildcardQuery can be quite slow. Also
note that
WildcardQuery should
not start with * and ?, as these are extremely slow.
To remove this protection and allow a wildcard at the beginning of a term, see method
setAllowLeadingWildcard in
QueryParser.
A
FuzzyQuery
matches documents that contain terms similar to the specified term. Similarity is
determined using
Levenshtein (edit) distance.
This type of query can be useful when accounting for spelling variations in the collection.
Changing Similarity
Chances are DefaultSimilarity is sufficient for all
your searching needs.
However, in some applications it may be necessary to customize your Similarity implementation. For instance, some
applications do not need to
distinguish between shorter and longer documents (see a "fair" similarity).
To change Similarity, one must do so for both indexing and
searching, and the changes must happen before
either of these actions take place. Although in theory there is nothing stopping you from changing mid-stream, it
just isn't well-defined what is going to happen.
To make this change, implement your own Similarity (likely
you'll want to simply subclass
DefaultSimilarity) and then use the new
class by calling
IndexWriter.setSimilarity
before indexing and
Searcher.setSimilarity
before searching.
If you are interested in use cases for changing your similarity, see the Lucene users's mailing list at Overriding Similarity.
In summary, here are a few use cases:
SweetSpotSimilarity — SweetSpotSimilarity gives small increases
as the frequency increases a small amount
and then greater increases when you hit the "sweet spot", i.e. where you think the frequency of terms is
more significant.
Overriding tf — In some applications, it doesn't matter what the score of a document is as long as a
matching term occurs. In these
cases people have overridden Similarity to return 1 from the tf() method.
Changing Length Normalization — By overriding lengthNorm,
it is possible to discount how the length of a field contributes
to a score. In DefaultSimilarity,
lengthNorm = 1 / (numTerms in field)^0.5, but if one changes this to be
1 / (numTerms in field), all fields will be treated
"fairly".
In general, Chris Hostetter sums it up best in saying (from the Lucene users's mailing list):
[One would override the Similarity in] ... any situation where you know more about your data then just
that
it's "text" is a situation where it *might* make sense to to override your
Similarity method.
Changing Scoring — Expert Level
Changing scoring is an expert level task, so tread carefully and be prepared to share your code if
you want help.
With the warning out of the way, it is possible to change a lot more than just the Similarity
when it comes to scoring in Lucene. Lucene's scoring is a complex mechanism that is grounded by
three main classes:
-
Query — The abstract object representation of the
user's information need.
-
Weight — The internal interface representation of
the user's Query, so that Query objects may be reused.
-
Scorer — An abstract class containing common
functionality for scoring. Provides both scoring and explanation capabilities.
Details on each of these classes, and their children, can be found in the subsections below.
The Query Class
In some sense, the
Query
class is where it all begins. Without a Query, there would be
nothing to score. Furthermore, the Query class is the catalyst for the other scoring classes as it
is often responsible
for creating them or coordinating the functionality between them. The
Query class has several methods that are important for
derived classes:
- createWeight(Searcher searcher) — A
Weight is the internal representation of the
Query, so each Query implementation must
provide an implementation of Weight. See the subsection on The Weight Interface below for details on implementing the Weight
interface.
- rewrite(IndexReader reader) — Rewrites queries into primitive queries. Primitive queries are:
TermQuery,
BooleanQuery, OTHERS????
The Weight Interface
The
Weight
interface provides an internal representation of the Query so that it can be reused. Any
Searcher
dependent state should be stored in the Weight implementation,
not in the Query class. The interface defines six methods that must be implemented:
-
Weight#getQuery() — Pointer to the
Query that this Weight represents.
-
Weight#getValue() — The weight for
this Query. For example, the TermQuery.TermWeight value is
equal to the idf^2 * boost * queryNorm
-
Weight#sumOfSquaredWeights() — The sum of squared weights. For TermQuery, this is (idf *
boost)^2
-
Weight#normalize(float) — Determine the query normalization factor. The query normalization may
allow for comparing scores between queries.
-
Weight#scorer(IndexReader) — Construct a new
Scorer
for this Weight. See
The Scorer Class
below for help defining a Scorer. As the name implies, the
Scorer is responsible for doing the actual scoring of documents given the Query.
-
Weight#explain(IndexReader, int) — Provide a means for explaining why a given document was
scored
the way it was.
The Scorer Class
The
Scorer
abstract class provides common scoring functionality for all Scorer implementations and
is the heart of the Lucene scoring process. The Scorer defines the following abstract methods which
must be implemented:
-
Scorer#next() — Advances to the next
document that matches this Query, returning true if and only
if there is another document that matches.
-
Scorer#doc() — Returns the id of the
Document
that contains the match. It is not valid until next() has been called at least once.
-
Scorer#score() — Return the score of the
current document. This value can be determined in any
appropriate way for an application. For instance, the
TermScorer
returns the tf * Weight.getValue() * fieldNorm.
-
Scorer#skipTo(int) — Skip ahead in
the document matches to the document whose id is greater than
or equal to the passed in value. In many instances, skipTo can be
implemented more efficiently than simply looping through all the matching documents until
the target document is identified.
-
Scorer#explain(int) — Provides
details on why the score came about.
Why would I want to add my own Query?
In a nutshell, you want to add your own custom Query implementation when you think that Lucene's
aren't appropriate for the
task that you want to do. You might be doing some cutting edge research or you need more information
back
out of Lucene (similar to Doug adding SpanQuery functionality).
Examples
FILL IN HERE
|
Java Source File Name | Type | Comment |
BaseTestRangeFilter.java | Class | |
BooleanClause.java | Class | A clause in a BooleanQuery. |
BooleanFilter.java | Class | A container Filter that allows Boolean composition of Filters. |
BooleanFilterTest.java | Class | |
BooleanQuery.java | Class | A Query that matches documents matching boolean combinations of other
queries, e.g. |
BooleanScorer.java | Class | |
BooleanScorer2.java | Class | An alternative to BooleanScorer that also allows a minimum number
of optional scorers that should match. |
BoostingQuery.java | Class | The BoostingQuery class can be used to effectively demote results that match a given query. |
CachingSpanFilter.java | Class | Wraps another SpanFilter's result and caches it. |
CachingWrapperFilter.java | Class | Wraps another filter's result and caches it. |
CachingWrapperFilterHelper.java | Class | A unit test helper class to test when the filter is getting cached and when it is not. |
CheckHits.java | Class | |
ComplexExplanation.java | Class | Expert: Describes the score computation for document and query, andcan distinguish a match independent of a positive value. |
ConjunctionScorer.java | Class | Scorer for conjunctions, sets of queries, all of which are required. |
ConstantScoreQuery.java | Class | A query that wraps a filter and simply returns a constant score equal to the
query boost for every document in the filter. |
ConstantScoreRangeQuery.java | Class | A range query that returns a constant score equal to its boost for
all documents in the range.
It does not have an upper bound on the number of clauses covered in the range.
If an endpoint is null, it is said to be "open".
Either or both endpoints may be open. |
DefaultSimilarity.java | Class | Expert: Default scoring implementation. |
DisjunctionMaxQuery.java | Class | A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum
score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.
This is useful when searching for a word in multiple fields with different boost factors (so that the fields cannot be
combined equivalently into a single search field). |
DisjunctionMaxScorer.java | Class | The Scorer for DisjunctionMaxQuery's. |
DisjunctionSumScorer.java | Class | A Scorer for OR like queries, counterpart of ConjunctionScorer .
This Scorer implements
Scorer.skipTo(int) and uses skipTo() on the given Scorers. |
DuplicateFilter.java | Class | |
DuplicateFilterTest.java | Class | |
ExactPhraseScorer.java | Class | |
Explanation.java | Class | Expert: Describes the score computation for document and query. |
ExtendedFieldCache.java | Interface | |
ExtendedFieldCacheImpl.java | Class | |
FieldCache.java | Interface | Expert: Maintains caches of term values. |
FieldCacheImpl.java | Class | Expert: The default cache implementation, storing all values in memory. |
FieldDoc.java | Class | Expert: A ScoreDoc which also contains information about
how to sort the referenced document. |
FieldDocSortedHitQueue.java | Class | Expert: Collects sorted results from Searchable's and collates them. |
FieldSortedHitQueue.java | Class | Expert: A hit queue for sorting by hits by terms in more than one field. |
Filter.java | Class | Abstract base class providing a mechanism to restrict searches to a subset
of an index. |
FilterClause.java | Class | A Filter that wrapped with an indication of how that filter
is used when composed with another filter. |
FilteredQuery.java | Class | A query that applies a filter to the results of another query. |
FilteredTermEnum.java | Class | Abstract class for enumerating a subset of all terms. |
FilterManager.java | Class | Filter caching singleton. |
FuzzyLikeThisQuery.java | Class | Fuzzifies ALL terms provided as strings and then picks the best n differentiating terms.
In effect this mixes the behaviour of FuzzyQuery and MoreLikeThis but with special consideration
of fuzzy scoring factors.
This generally produces good results for queries where users may provide details in a number of
fields and have no knowledge of boolean query syntax and also want a degree of fuzzy matching and
a fast query.
For each source term the fuzzy variants are held in a BooleanQuery with no coord factor (because
we are not looking for matches on multiple variants in any one doc). |
FuzzyQuery.java | Class | Implements the fuzzy search query. |
FuzzyTermEnum.java | Class | Subclass of FilteredTermEnum for enumerating all terms that are similiar
to the specified filter term.
Term enumerations are always ordered by Term.compareTo(). |
Hit.java | Class | Wrapper used by
HitIterator to provide a lazily loaded hit
from
Hits . |
HitCollector.java | Class | Lower-level search API. |
HitIterator.java | Class | An iterator over
Hits that provides lazy fetching of each document.
Hits.iterator returns an instance of this class. |
HitQueue.java | Class | |
Hits.java | Class | A ranked list of documents, used to hold search results.
Caution: Iterate only over the hits needed. |
IndexSearcher.java | Class | Implements search over a single IndexReader.
Applications usually need only call the inherited
IndexSearcher.search(Query) or
IndexSearcher.search(Query,Filter) methods. |
MatchAllDocsQuery.java | Class | A query that matches all documents. |
MockFilter.java | Class | |
MultiPhraseQuery.java | Class | MultiPhraseQuery is a generalized version of PhraseQuery, with an added
method
MultiPhraseQuery.add(Term[]) . |
MultiSearcher.java | Class | Implements search over a set of Searchables . |
MultiTermQuery.java | Class | A
Query that matches documents containing a subset of terms provided
by a
FilteredTermEnum enumeration.
MultiTermQuery is not designed to be used by itself.
The reason being that it is not intialized with a
FilteredTermEnum enumeration. |
NonMatchingScorer.java | Class | A scorer that matches no document at all. |
ParallelMultiSearcher.java | Class | Implements parallel search over a set of Searchables . |
PhrasePositions.java | Class | Position of a term in a document that takes into account the term offset within the phrase. |
PhraseQuery.java | Class | A Query that matches documents containing a particular sequence of terms. |
PhraseQueue.java | Class | |
PhraseScorer.java | Class | Expert: Scoring functionality for phrase queries.
A document is considered matching if it contains the phrase-query terms
at "valid" positons. |
PrefixFilter.java | Class | |
PrefixQuery.java | Class | A Query that matches documents containing terms with a specified prefix. |
Query.java | Class | The abstract base class for queries. |
QueryFilter.java | Class | Constrains search results to only match those which also match a provided
query. |
QueryTermVector.java | Class | |
QueryUtils.java | Class | |
QueryWrapperFilter.java | Class | Constrains search results to only match those which also match a provided
query. |
RangeFilter.java | Class | A Filter that restricts search results to a range of values in a given
field. |
RangeQuery.java | Class | A Query that matches documents within an exclusive range. |
RemoteCachingWrapperFilter.java | Class | Provides caching of
Filter s themselves on the remote end of an RMI connection. |
RemoteCachingWrapperFilterHelper.java | Class | A unit test helper class to help with RemoteCachingWrapperFilter testing and
assert that it is working correctly. |
RemoteSearchable.java | Class | A remote searchable implementation. |
ReqExclScorer.java | Class | A Scorer for queries with a required subscorer and an excluding (prohibited) subscorer. |
ReqOptSumScorer.java | Class | A Scorer for queries with a required part and an optional part. |
SampleComparable.java | Class | An example Comparable for use with the custom sort tests.
It implements a comparable for "id" sort of values which
consist of an alphanumeric part and a numeric part, such as:
ABC-123, A-1, A-7, A-100, B-99999
Such values cannot be sorted as strings, since A-100 needs
to come after A-7.
It could be argued that the "ids" should be rewritten as
A-0001, A-0100, etc. |
ScoreDoc.java | Class | Expert: Returned by low-level search implementations. |
ScoreDocComparator.java | Interface | Expert: Compares two ScoreDoc objects for sorting. |
Scorer.java | Class | Expert: Common scoring functionality for different types of queries. |
Searchable.java | Interface | The interface for search implementations.
Searchable is the abstract network protocol for searching. |
Searcher.java | Class | An abstract base class for search implementations.
Implements the main search methods.
Note that you can only access Hits from a Searcher as long as it is
not yet closed, otherwise an IOException will be thrown. |
Similarity.java | Class | Expert: Scoring API.
Subclasses implement search scoring.
The score of query q for document d correlates to the
cosine-distance or dot-product between document and query vectors in a
Vector Space Model (VSM) of Information Retrieval.
A document whose vector is closer to the query vector in that model is scored higher.
The score is computed as follows:
where
-
tf(t in d)
correlates to the term's frequency,
defined as the number of times term t appears in the currently scored document d.
Documents that have more occurrences of a given term receive a higher score.
The default computation for tf(t in d) in
org.apache.lucene.search.DefaultSimilarity.tf(float) DefaultSimilarity is:
-
idf(t) stands for Inverse Document Frequency.
|
SimilarityDelegator.java | Class | Expert: Delegating scoring implementation. |
SingleDocTestFilter.java | Class | |
SloppyPhraseScorer.java | Class | |
Sort.java | Class | Encapsulates sort criteria for returned hits.
The fields used to determine sort order must be carefully chosen.
Documents must contain a single term in such a field,
and the value of the term should indicate the document's relative position in
a given sort order. |
SortComparator.java | Class | Abstract base class for sorting hits returned by a Query.
This class should only be used if the other SortField
types (SCORE, DOC, STRING, INT, FLOAT) do not provide an
adequate sorting. |
SortComparatorSource.java | Interface | Expert: returns a comparator for sorting ScoreDocs. |
SortField.java | Class | Stores information about how to sort documents by terms in an individual
field. |
SpanFilter.java | Class | Abstract base class providing a mechanism to restrict searches to a subset
of an index and also maintains and returns position information.
This is useful if you want to compare the positions from a SpanQuery with the positions of items in
a filter. |
SpanFilterResult.java | Class | The results of a SpanQueryFilter. |
SpanQueryFilter.java | Class | Constrains search results to only match those which also match a provided
query. |
TermQuery.java | Class | A Query that matches documents containing a term. |
TermScorer.java | Class | Expert: A Scorer for documents matching a Term . |
TermsFilter.java | Class | Constructs a filter for docs matching any of the terms added to this class. |
TestBoolean2.java | Class | Test BooleanQuery2 against BooleanQuery by overriding the standard query parser. |
TestBooleanMinShouldMatch.java | Class | Test that BooleanQuery.setMinimumNumberShouldMatch works. |
TestBooleanOr.java | Class | Created on 2005. |
TestBooleanPrefixQuery.java | Class | |
TestBooleanQuery.java | Class | |
TestBooleanScorer.java | Class | |
TestCachingWrapperFilter.java | Class | |
TestComplexExplanations.java | Class | TestExplanations subclass that builds up super crazy complex queries
on the assumption that if the explanations work out right for them,
they should work for anything. |
TestComplexExplanationsOfNonMatches.java | Class | subclass of TestSimpleExplanations that verifies non matches. |
TestConstantScoreRangeQuery.java | Class | |
TestCustomSearcherSort.java | Class | Unit test for sorting code. |
TestDateFilter.java | Class | DateFilter JUnit tests. |
TestDateSort.java | Class | Test date sorting, i.e. |
TestDisjunctionMaxQuery.java | Class | Test of the DisjunctionMaxQuery. |
TestDocBoost.java | Class | Document boost unit test. |
TestExplanations.java | Class | Tests primative queries (ie: that rewrite to themselves) to
insure they match the expected set of docs, and that the score of each
match is equal to the value of the scores explanation. |
TestExtendedFieldCache.java | Class | |
TestFilteredQuery.java | Class | FilteredQuery JUnit tests. |
TestFuzzyQuery.java | Class | Tests
FuzzyQuery . |
TestMatchAllDocsQuery.java | Class | Tests MatchAllDocsQuery. |
TestMultiPhraseQuery.java | Class | This class tests the MultiPhraseQuery class. |
TestMultiSearcher.java | Class | Tests
MultiSearcher class. |
TestMultiSearcherRanking.java | Class | Tests
MultiSearcher ranking, i.e. |
TestMultiThreadTermVectors.java | Class | |
TestNot.java | Class | Similarity unit test. |
TestParallelMultiSearcher.java | Class | |
TestPhrasePrefixQuery.java | Class | This class tests PhrasePrefixQuery class. |
TestPhraseQuery.java | Class | Tests
PhraseQuery . |
TestPositionIncrement.java | Class | Term position unit test. |
TestPrefixFilter.java | Class | Tests
PrefixFilter class. |
TestPrefixQuery.java | Class | Tests
PrefixQuery class. |
TestQueryTermVector.java | Class | |
TestRangeFilter.java | Class | A basic 'positive' Unit test class for the RangeFilter class.
NOTE: at the moment, this class only tests for 'positive' results,
it does not verify the results to ensure there are no 'false positives',
nor does it adequately test 'negative' results. |
TestRangeQuery.java | Class | |
TestRemoteCachingWrapperFilter.java | Class | Tests that the index is cached on the searcher side of things. |
TestRemoteSearchable.java | Class | |
TestScorerPerf.java | Class | |
TestSearchHitsWithDeletions.java | Class | Test Hits searches with interleaved deletions. |
TestSetNorm.java | Class | Document boost unit test. |
TestSimilarity.java | Class | Similarity unit test. |
TestSimpleExplanations.java | Class | |
TestSimpleExplanationsOfNonMatches.java | Class | subclass of TestSimpleExplanations that verifies non matches. |
TestSort.java | Class | Unit tests for sorting code. |
TestSpanQueryFilter.java | Class | |
TestTermScorer.java | Class | |
TestTermVectors.java | Class | |
TestThreadSafe.java | Class | |
TestWildcard.java | Class | TestWildcard tests the '*' and '?' wildcard characters. |
TopDocCollector.java | Class | A
HitCollector implementation that collects the top-scoring
documents, returning them as a
TopDocs . |
TopDocs.java | Class | Expert: Returned by low-level search implementations. |
TopFieldDocCollector.java | Class | A
HitCollector implementation that collects the top-sorting
documents, returning them as a
TopFieldDocs . |
TopFieldDocs.java | Class | Expert: Returned by low-level sorted search implementations. |
Weight.java | Interface | Expert: Calculate query weights and build query scorers.
The purpose of Weight is to make it so that searching does not modify
a Query, so that a Query instance can be reused. |
WildcardQuery.java | Class | Implements the wildcard search query. |
WildcardTermEnum.java | Class | Subclass of FilteredTermEnum for enumerating all terms that match the
specified wildcard filter term.
Term enumerations are always ordered by Term.compareTo(). |