Class Similarity
Similarity defines the components of Lucene scoring.
Expert: Scoring API.
This is a low-level API, you should only extend this API if you want to implement an information retrieval model. If you are instead looking for a convenient way to alter Lucene's scoring, consider extending a higher-level implementation such as TFIDFSimilarity, which implements the vector space model with this API, or just tweaking the default implementation: DefaultSimilarity.
Similarity determines how Lucene weights terms, and Lucene interacts with this class at both index-time and query-time.
At indexing time, the indexer calls ComputeNorm(FieldInvertState), allowing the Similarity implementation to set a per-document value for the field that will be later accessible via GetNormValues(String). Lucene makes no assumption about what is in this norm, but it is most useful for encoding length normalization information.
Implementations should carefully consider how the normalization is encoded: while Lucene's classical TFIDFSimilarity encodes a combination of index-time boost and length normalization information with SmallSingle into a single byte, this might not be suitable for all purposes.
Many formulas require the use of average document length, which can be computed via a combination of SumTotalTermFreq and MaxDoc or DocCount, depending upon whether the average should reflect field sparsity.
Additional scoring factors can be stored in named NumericDocValuesFields and accessed at query-time with GetNumericDocValues(String).
Finally, using index-time boosts (either via folding into the normalization byte or via DocValues), is an inefficient way to boost the scores of different fields if the boost will be the same for every document, instead the Similarity can simply take a constant boost parameter C, and PerFieldSimilarityWrapper can return different instances with different boosts depending upon field name.
At query-time, Queries interact with the Similarity via these steps:
- The ComputeWeight(Single, CollectionStatistics, TermStatistics[]) method is called a single time, allowing the implementation to compute any statistics (such as IDF, average document length, etc) across the entire collection. The TermStatistics and CollectionStatistics passed in already contain all of the raw statistics involved, so a Similarity can freely use any combination of statistics without causing any additional I/O. Lucene makes no assumption about what is stored in the returned Similarity.SimWeight object.
- The query normalization process occurs a single time: GetValueForNormalization() is called for each query leaf node, QueryNorm(Single) is called for the top-level query, and finally Normalize(Single, Single) passes down the normalization value and any top-level boosts (e.g. from enclosing BooleanQuerys).
- For each segment in the index, the Query creates a GetSimScorer(Similarity.SimWeight, AtomicReaderContext) The GetScore() method is called for each matching document.
When Explain(Query, Int32) is called, queries consult the Similarity's DocScorer for an explanation of how it computed its score. The query passes in a the document id and an explanation of how the frequency was computed.
@lucene.experimental
Inheritance
Assembly: DistributedLucene.Net.dll
Syntax
public abstract class Similarity : object
Constructors
Name | Description |
---|---|
Similarity() | Sole constructor. (For invocation by subclass constructors, typically implicit.) |
Methods
Name | Description |
---|---|
ComputeNorm(FieldInvertState) | Computes the normalization value for a field, given the accumulated state of term processing for this field (see FieldInvertState). Matches in longer fields are less precise, so implementations of this
method usually set smaller values when is small.
@lucene.experimental |
ComputeWeight(Single, CollectionStatistics, TermStatistics[]) | Compute any collection-level weight (e.g. IDF, average document length, etc) needed for scoring a query. |
Coord(Int32, Int32) | Hook to integrate coordinate-level matching.
By default this is disabled (returns |
GetSimScorer(Similarity.SimWeight, AtomicReaderContext) | Creates a new Similarity.SimScorer to score matching documents from a segment of the inverted index. |
QueryNorm(Single) | Computes the normalization value for a query given the sum of the normalized weights GetValueForNormalization() of each of the query terms. this value is passed back to the weight (Normalize(Single, Single) of each query term, to provide a hook to attempt to make scores from different queries comparable.
By default this is disabled (returns |