Table Of Contents
- Search Basics
- The Query Classes
- Scoring: Introduction
- Scoring: Basics
- Changing the Scoring
- Appendix: Search Algorithm
Lucene offers a wide variety of
Query implementations, most of which are in
this package, its subpackages (
or the queries module. These implementations can be combined in a wide
variety of ways to provide complex querying capabilities along with information about where matches took place in the document
collection. The Query Classes section below highlights some of the more important Query classes. For details
on implementing your own Query class, see Custom Queries -- Expert Level below.
Once a Query has been created and submitted to the
IndexSearcher, the scoring
process begins. After some infrastructure setup, control finally passes to the
implementation and its
Scorer instances. See the Algorithm
section for more notes on the process.
Of the various implementations of
is the easiest to understand and the most often used in applications. A
TermQuery matches all the documents that contain the
which is a word that occurs in a certain
TermQuery identifies and scores all
Documents that have a
Field with the specified string in it.
is as simple as:
TermQuery tq = new TermQuery(new Term("fieldName", "term"));In this example, the
Documents that have the
Fieldnamed "fieldName" containing the word "term".
Things start to get interesting when one combines multiple
TermQuery instances into a
BooleanQuery contains multiple
where each clause contains a sub-query (
instance) and an operator (from
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.
BooleanClauseinstances. If too many clauses are added, a
TooManyClausesexception will be thrown during searching. This most often occurs when a
Queryis rewritten into a
TermQueryclauses, for example by
WildcardQuery. The default setting for the maximum number of clauses 1024, but this can be changed via the static method
Another common search is to find documents containing certain phrases. This is handled three different ways:
PhraseQuery— Matches a sequence of
PhraseQueryuses a slop factor to determine how many positions may occur between any two terms in the phrase and still be considered a match. The slop is 0 by default, meaning the phrase must match exactly.
MultiPhraseQuery— A more general form of PhraseQuery that accepts multiple Terms for a position in the phrase. For example, this can be used to perform phrase queries that also incorporate synonyms.
SpanNearQuery— Matches a sequence of other
SpanNearQueryallows for much more complicated phrase queries since it is constructed from other
SpanQueryinstances, instead of only
matches all documents that occur in the
exclusive range of a lower
and an upper
TermsEnum.getComparator(). It is not intended
for numerical ranges; use
For example, one could find all documents
that have terms beginning with the letters a through c.
matches all documents that occur in a numeric range.
For NumericRangeQuery to work, you must index the values
using a one of the numeric fields (
has a different implementation, it is essentially a special case of 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
not start with * and ?, as these are extremely slow.
Some QueryParsers may not allow this by default, but provide a
to remove that protection.
RegexpQuery is even more general than WildcardQuery,
allowing an application to identify all documents with terms that match a regular expression pattern.
matches documents that contain terms similar to the specified term. Similarity is
Levenshtein (edit) distance.
This type of query can be useful when accounting for spelling variations in the collection.
Scoring — Introduction
Lucene scoring is the heart of why we all love Lucene. It is blazingly fast and it hides almost all of the complexity from the user. In a nutshell, it works. At least, that is, until it doesn't work, or doesn't work as one would expect it to work. Then we are left digging into Lucene internals or asking for help on email@example.com to figure out why a document with five of our query terms scores lower than a different document with only one of the query terms.
While this document won't answer your specific scoring issues, it will, hopefully, point you to the places that can help you figure out the what and why of Lucene scoring.
Lucene scoring supports a number of pluggable information retrieval models, including:
Similarity API, and offer extension hooks and parameters for tuning. In general, Lucene first finds the documents that need to be scored based on boolean logic in the Query specification, and then ranks this subset of matching documents via the retrieval model. For some valuable references on VSM and IR in general refer to Lucene Wiki IR references.
The rest of this document will cover Scoring basics and explain how to
Similarity. Next, it will cover
ways you can customize the lucene internals in
Custom Queries -- Expert Level, which gives details on
implementing your own
Query class and related functionality.
Finally, we will finish up with some reference material in the Appendix.
Scoring — Basics
Scoring is very much dependent on the way documents are indexed, so it is important to understand
indexing. (see Lucene overview
before continuing on with this section) Be sure to use the useful
to understand how the score for a certain matching document was
Generally, the Query determines which documents match (a binary decision), while the Similarity determines how to assign scores to the matching documents.
Fields and Documents
In Lucene, the objects we are scoring are
A Document is a collection of
Fields. Each Field has
semantics about how it is created and stored
stored, etc). It is important to note that
Lucene scoring works on Fields and then combines the results to return Documents. This is
important because two Documents with the exact same content, but one having the content in two
Fields and the other in one Field may return different scores for the same query due to length
Lucene allows influencing search results by "boosting" at different times:
- Index-time boost by calling
Field.setBoost()before a document is added to the index.
- Query-time boost by setting a boost on a query clause, calling
Indexing time boosts are pre-processed for storage efficiency and written to storage for a field as follows:
- All boosts of that field (i.e. all boosts under the same field name in that doc) are multiplied.
- The boost is then encoded into a normalization value by the Similarity
object at index-time:
computeNorm(). The actual encoding depends upon the Similarity implementation, but note that most use a lossy encoding (such as multiplying the boost with document length or similar, packed into a single byte!).
- Decoding of any index-time normalization values and integration into the document's score is also performed at search time by the Similarity.
Changing Scoring — Similarity
Similarity is an easy way to
influence scoring, this is done at index-time with
IndexWriterConfig.setSimilarity(Similarity) and at query-time with
IndexSearcher.setSimilarity(Similarity). Be sure to use the same
Similarity at query-time as at index-time (so that norms are
encoded/decoded correctly); Lucene makes no effort to verify this.
You can influence scoring by configuring a different built-in Similarity implementation, or by tweaking its parameters, subclassing it to override behavior. Some implementations also offer a modular API which you can extend by plugging in a different component (e.g. term frequency normalizer).
Finally, you can extend the low level
to implement a new retrieval model, or to use external scoring factors particular to your application. For example,
a custom Similarity can access per-document values via
NumericDocValues and integrate them into the score.
org.apache.lucene.search.similarities package documentation for information
on the built-in available scoring models and extending or changing Similarity.
Custom Queries — Expert Level
Custom queries are 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 matching and scoring in Lucene. Lucene's search 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. This is global (across all segments of the index) and generally will require global statistics (such as docFreq for a given term across all segments).
Scorer— An abstract class containing common functionality for scoring. Provides both scoring and explanation capabilities. This is created per-segment.
The Query Class
In some sense, the
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
createWeight(IndexSearcher searcher)— A
Weightis 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:
BooleanQuery, and other queries that implement
The Weight Interface
interface provides an internal representation of the Query so that it can be reused. Any
dependent state should be stored in the Weight implementation,
not in the Query class. The interface defines five methods that must be implemented:
getQuery()— Pointer to the Query that this Weight represents.
getValueForNormalization()— A weight can return a floating point value to indicate its magnitude for query normalization. Typically a weight such as TermWeight that scores via a
Similaritywill just defer to the Similarity's implementation:
SimWeight#getValueForNormalization(). For example, with
Lucene's classic vector-space formula, this is implemented as the sum of squared weights:
(idf * boost)2
normalize(float norm, float topLevelBoost)— Performs query normalization:
topLevelBoost: A query-boost factor from any wrapping queries that should be multiplied into every document's score. For example, a TermQuery that is wrapped within a BooleanQuery with a boost of
5would receive this value at this time. This allows the TermQuery (the leaf node in this case) to compute this up-front a single time (e.g. by multiplying into the IDF), rather than for every document.
norm: Passes in a a normalization factor which may allow for comparing scores between queries.
Similaritywill just defer to the Similarity's implementation:
scorer(AtomicReaderContext context, boolean scoresDocsInOrder, boolean topScorer, Bits acceptDocs)— Construct a new
Scorerfor 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.
explain(AtomicReaderContext context, int doc)— Provide a means for explaining why a given document was scored the way it was. Typically a weight such as TermWeight that scores via a
Similaritywill make use of the Similarity's implementation:
SimScorer#explain(int doc, Explanation freq).
The Scorer Class
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 (some of them are not
yet abstract, but will be in future versions and should be considered as such now) methods which
must be implemented (some of them inherited from
nextDoc()— Advances to the next document that matches this Query, returning true if and only if there is another document that matches.
docID()— Returns the id of the
Documentthat contains the match.
score()— Return the score of the current document. This value can be determined in any appropriate way for an application. For instance, the
TermScorersimply defers to the configured Similarity:
SimScorer.score(int doc, float freq).
freq()— Returns the number of matches for the current document. This value can be determined in any appropriate way for an application. For instance, the
TermScorersimply defers to the term frequency from the inverted index:
advance()— Skip ahead in the document matches to the document whose id is greater than or equal to the passed in value. In many instances, advance can be implemented more efficiently than simply looping through all the matching documents until the target document is identified.
getChildren()— Returns any child subscorers underneath this scorer. This allows for users to navigate the scorer hierarchy and receive more fine-grained details on the scoring process.
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).
Appendix: Search Algorithm
This section is mostly notes on stepping through the Scoring process and serves as fertilizer for the earlier sections.
Once inside the IndexSearcher, a
is used for the scoring and sorting of the search results.
These important objects are involved in a search:
Weightobject of the Query. The Weight object is an internal representation of the Query that allows the Query to be reused by the IndexSearcher.
- The IndexSearcher that initiated the call.
Filterfor limiting the result set. Note, the Filter may be null.
Sortobject for specifying how to sort the results if the standard score-based sort method is not desired.
Assuming we are not sorting (since sorting doesn't affect the raw Lucene score),
we call one of the search methods of the IndexSearcher, passing in the
Weight object created by
Filter and the number of results we want.
This method returns a
which is an internal collection of search results. The IndexSearcher creates
passes it along with the Weight, Filter to another expert search method (for
more on the
IndexSearcher). The TopScoreDocCollector
PriorityQueue to collect the
top results for the search.
At last, we are actually going to score some documents. The score method takes in the Collector
(most likely the TopScoreDocCollector or TopFieldCollector) and does its business.Of course, here
is where things get involved. The
Scorer that is returned
Weight object depends on what type of Query was
submitted. In most real world applications with multiple query terms, the
Scorer is going to be a
BooleanWeight (see the section on
custom queries for info on changing this).
Assuming a BooleanScorer2, we first initialize the Coordinator, which is used to apply the coord()
factor. We then get a internal Scorer based on the required, optional and prohibited parts of the query.
Using this internal Scorer, the BooleanScorer2 then proceeds into a while loop based on the
Scorer.nextDoc() method. The nextDoc() method advances
to the next document matching the query. This is an abstract method in the Scorer class and is thus
overridden by all derived implementations. If you have a simple OR query your internal Scorer is most
likely a DisjunctionSumScorer, which essentially combines the scorers from the sub scorers of the OR'd terms.
Interface Summary Interface Description BoostAttributeAdd this
MultiTermQuery.getTermsEnum(Terms,AttributeSource)and update the boost on each returned term.
FieldCacheExpert: Maintains caches of term values. FieldCache.ByteParser Deprecated. FieldCache.DoubleParserInterface to parse doubles from document fields. FieldCache.FloatParserInterface to parse floats from document fields. FieldCache.IntParserInterface to parse ints from document fields. FieldCache.LongParserInterface to parse long from document fields. FieldCache.ParserMarker interface as super-interface to all parsers. FieldCache.ShortParser Deprecated. FuzzyTermsEnum.LevenshteinAutomataAttributereuses compiled automata across different segments, because they are independent of the index MaxNonCompetitiveBoostAttributeAdd this
Attributeto a fresh
ReferenceManager.RefreshListenerUse to receive notification when a refresh has finished. SearcherLifetimeManager.Pruner
Class Summary Class Description AutomatonQueryA
Querythat will match terms against a finite-state machine.
BitsFilteredDocIdSetThis implementation supplies a filtered DocIdSet, that excludes all docids which are not in a Bits instance. BooleanClauseA clause in a BooleanQuery. BooleanQueryA Query that matches documents matching boolean combinations of other queries, e.g. BoostAttributeImplImplementation class for
CachingCollectorCaches all docs, and optionally also scores, coming from a search, and is then able to replay them to another collector. CachingWrapperFilterWraps another
Filter's result and caches it.
CollectionStatisticsContains statistics for a collection (field) CollectorExpert: Collectors are primarily meant to be used to gather raw results from a search, and implement sorting or custom result filtering, collation, etc. ComplexExplanationExpert: Describes the score computation for document and query, and can distinguish a match independent of a positive value. ConstantScoreQueryA query that wraps another query or a filter and simply returns a constant score equal to the query boost for every document that matches the filter or query. ControlledRealTimeReopenThread<T>Utility class that runs a thread to manage periodicc reopens of a
ReferenceManager, with methods to wait for a specific index changes to become visible.
DisjunctionMaxQueryA 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. DocIdSetA DocIdSet contains a set of doc ids. DocIdSetIteratorThis abstract class defines methods to iterate over a set of non-decreasing doc ids. DocTermOrdsRangeFilterA range filter built on top of a cached multi-valued term field (in
DocTermOrdsRewriteMethodRewrites MultiTermQueries into a filter, using DocTermOrds for term enumeration. ExplanationExpert: Describes the score computation for document and query. FieldCache.BytesField values as 8-bit signed bytes FieldCache.CacheEntryEXPERT: A unique Identifier/Description for each item in the FieldCache. FieldCache.CreationPlaceholderPlaceholder indicating creation of this cache is currently in-progress. FieldCache.DoublesField values as 64-bit doubles FieldCache.FloatsField values as 32-bit floats FieldCache.IntsField values as 32-bit signed integers FieldCache.LongsField values as 64-bit signed long integers FieldCache.ShortsField values as 16-bit signed shorts FieldCacheDocIdSetBase class for DocIdSet to be used with FieldCache. FieldCacheRangeFilter<T>A range filter built on top of a cached single term field (in
FieldCacheRewriteMethodRewrites MultiTermQueries into a filter, using the FieldCache for term enumeration. FieldCacheTermsFilterA
Filterthat only accepts documents whose single term value in the specified field is contained in the provided set of allowed terms.
FieldComparator<T>Expert: a FieldComparator compares hits so as to determine their sort order when collecting the top results with
FieldComparator.ByteComparator Deprecated. FieldComparator.DocComparatorSorts by ascending docID FieldComparator.DoubleComparatorParses field's values as double (using
FieldCache.getDoubles(org.apache.lucene.index.AtomicReader, java.lang.String, boolean)and sorts by ascending value
FieldComparator.FloatComparatorParses field's values as float (using
FieldCache.getFloats(org.apache.lucene.index.AtomicReader, java.lang.String, boolean)and sorts by ascending value
FieldComparator.IntComparatorParses field's values as int (using
FieldCache.getInts(org.apache.lucene.index.AtomicReader, java.lang.String, boolean)and sorts by ascending value
FieldComparator.LongComparatorParses field's values as long (using
FieldCache.getLongs(org.apache.lucene.index.AtomicReader, java.lang.String, boolean)and sorts by ascending value
FieldComparator.NumericComparator<T extends Number>Base FieldComparator class for numeric types FieldComparator.RelevanceComparatorSorts by descending relevance. FieldComparator.ShortComparator Deprecated. FieldComparator.TermOrdValComparatorSorts by field's natural Term sort order, using ordinals. FieldComparator.TermValComparatorSorts by field's natural Term sort order. FieldComparatorSourceProvides a
FieldComparatorfor custom field sorting.
FieldDocExpert: A ScoreDoc which also contains information about how to sort the referenced document. FieldValueFilterA
Filterthat accepts all documents that have one or more values in a given field.
FieldValueHitQueue<T extends FieldValueHitQueue.Entry>Expert: A hit queue for sorting by hits by terms in more than one field. FieldValueHitQueue.EntryExtension of ScoreDoc to also store the
FilterAbstract base class for restricting which documents may be returned during searching. FilteredDocIdSetAbstract decorator class for a DocIdSet implementation that provides on-demand filtering/validation mechanism on a given DocIdSet. FilteredDocIdSetIteratorAbstract decorator class of a DocIdSetIterator implementation that provides on-demand filter/validation mechanism on an underlying DocIdSetIterator. FilteredQueryA query that applies a filter to the results of another query. FilteredQuery.FilterStrategyAbstract class that defines how the filter (
DocIdSet) applied during document collection.
FilteredQuery.FilterStrategythat conditionally uses a random access filter if the given
DocIdSetsupports random access (returns a non-null value from
FuzzyQueryImplements the fuzzy search query. FuzzyTermsEnumSubclass of TermsEnum for enumerating all terms that are similar to the specified filter term. FuzzyTermsEnum.LevenshteinAutomataAttributeImplStores compiled automata as a list (indexed by edit distance) IndexSearcherImplements search over a single IndexReader. IndexSearcher.LeafSliceA class holding a subset of the
IndexSearchers leaf contexts to be executed within a single thread.
LiveFieldValues<S,T>Tracks live field values across NRT reader reopens. MatchAllDocsQueryA query that matches all documents. MaxNonCompetitiveBoostAttributeImplImplementation class for
MultiCollector MultiPhraseQueryMultiPhraseQuery is a generalized version of PhraseQuery, with an added method
MultiTermQuery MultiTermQuery.ConstantScoreAutoRewriteA rewrite method that tries to pick the best constant-score rewrite method based on term and document counts from the query. MultiTermQuery.RewriteMethodAbstract class that defines how the query is rewritten. MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewriteA rewrite method that first translates each term into
BooleanClause.Occur.SHOULDclause in a BooleanQuery, but the scores are only computed as the boost.
MultiTermQuery.TopTermsScoringBooleanQueryRewriteA rewrite method that first translates each term into
BooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query.
MultiTermQueryWrapperFilter<Q extends MultiTermQuery> NGramPhraseQueryThis is a
PhraseQuerywhich is optimized for n-gram phrase query.
NumericRangeFilter<T extends Number>A
Filterthat only accepts numeric values within a specified range.
NumericRangeQuery<T extends Number>A
Querythat matches numeric values within a specified range.
PhraseQueryA Query that matches documents containing a particular sequence of terms. PositiveScoresOnlyCollector PrefixFilterA Filter that restricts search results to values that have a matching prefix in a given field. PrefixQueryA Query that matches documents containing terms with a specified prefix. PrefixTermsEnumSubclass of FilteredTermEnum for enumerating all terms that match the specified prefix filter term. QueryThe abstract base class for queries. QueryWrapperFilterConstrains search results to only match those which also match a provided query. ReferenceManager<G>Utility class to safely share instances of a certain type across multiple threads, while periodically refreshing them. RegexpQueryA fast regular expression query based on the
Scorerwhich wraps another scorer and caches the score of the current document.
ScoreDocHolds one hit in
ScorerExpert: Common scoring functionality for different types of queries. Scorer.ChildScorerA child Scorer and its relationship to its parent. ScoringRewrite<Q extends Query>Base rewrite method that translates each term into a query, and keeps the scores as computed by the query. SearcherFactoryFactory class used by
SearcherManagerto create new IndexSearchers.
SearcherLifetimeManagerKeeps track of current plus old IndexSearchers, closing the old ones once they have timed out. SearcherLifetimeManager.PruneByAgeSimple pruner that drops any searcher older by more than the specified seconds, than the newest searcher. SearcherManagerUtility class to safely share
IndexSearcherinstances across multiple threads, while periodically reopening.
SortEncapsulates sort criteria for returned hits. SortFieldStores information about how to sort documents by terms in an individual field. TermQueryA Query that matches documents containing a term. TermRangeFilterA Filter that restricts search results to a range of term values in a given field. TermRangeQueryA Query that matches documents within an range of terms. TermRangeTermsEnumSubclass of FilteredTermEnum for enumerating all terms that match the specified range parameters. TermStatisticsContains statistics for a specific term TimeLimitingCollectorThe
TimeLimitingCollectoris used to timeout search requests that take longer than the maximum allowed search time limit.
TimeLimitingCollector.TimerThreadThread used to timeout search requests. TopDocs TopDocsCollector<T extends ScoreDoc>A base class for all collectors that return a
TopFieldCollector TopFieldDocsRepresents hits returned by
TopScoreDocCollector TopTermsRewrite<Q extends Query>Base rewrite method for collecting only the top terms via a priority queue. TotalHitCountCollectorJust counts the total number of hits. WeightExpert: Calculate query weights and build query scorers. WildcardQueryImplements the wildcard search query.
Enum Summary Enum Description BooleanClause.OccurSpecifies how clauses are to occur in matching documents. SortField.TypeSpecifies the type of the terms to be sorted, or special types such as CUSTOM
Exception Summary Exception Description BooleanQuery.TooManyClausesThrown when an attempt is made to add more than
CollectionTerminatedExceptionThrow this exception in
Collector.collect(int)to prematurely terminate collection of the current leaf.
TimeLimitingCollector.TimeExceededExceptionThrown when elapsed search time exceeds allowed search time.