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: Default
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 Compute
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 Small
Many formulas require the use of average document length, which can be computed via a
combination of Sum
Additional scoring factors can be stored in named
Numeric
Finally, using index-time boosts (either via folding into the normalization byte or
via Doc
At query-time, Queries interact with the Similarity via these steps:
- The Compute
Weight(Single, Collection 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, Term Statistics[]) Statistics 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.Sim object.Weight - The query normalization process occurs a single time: Get
Value is called for each query leaf node, QueryFor Normalization() Norm(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 BooleanQuery s). - For each segment in the index, the Query creates a Get
Sim The GetScore() method is called for each matching document.Scorer(Similarity. Sim Weight, Atomic Reader Context)
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.
Note
This API is experimental and might change in incompatible ways in the next release.
Inheritance
Inherited Members
Namespace: Lucene.Net.Search.Similarities
Assembly: Lucene.Net.dll
Syntax
public abstract class Similarity
Constructors
| Improve this Doc View SourceSimilarity()
Sole constructor. (For invocation by subclass constructors, typically implicit.)
Declaration
protected Similarity()
Methods
| Improve this Doc View SourceComputeNorm(FieldInvertState)
Computes the normalization value for a field, given the accumulated
state of term processing for this field (see Field
Matches in longer fields are less precise, so implementations of this
method usually set smaller values when state.Length
is large,
and larger values when
state.Length
is small.
Note
This API is experimental and might change in incompatible ways in the next release.
Declaration
public abstract long ComputeNorm(FieldInvertState state)
Parameters
Type | Name | Description |
---|---|---|
Field |
state | current processing state for this field |
Returns
Type | Description |
---|---|
System. |
computed norm value |
ComputeWeight(Single, CollectionStatistics, TermStatistics[])
Compute any collection-level weight (e.g. IDF, average document length, etc) needed for scoring a query.
Declaration
public abstract Similarity.SimWeight ComputeWeight(float queryBoost, CollectionStatistics collectionStats, params TermStatistics[] termStats)
Parameters
Type | Name | Description |
---|---|---|
System. |
queryBoost | the query-time boost. |
Collection |
collectionStats | collection-level statistics, such as the number of tokens in the collection. |
Term |
termStats | term-level statistics, such as the document frequency of a term across the collection. |
Returns
Type | Description |
---|---|
Similarity. |
Similarity. |
Coord(Int32, Int32)
Hook to integrate coordinate-level matching.
By default this is disabled (returns 1
), as with
most modern models this will only skew performance, but some
implementations such as TFIDFSimilarity override this.
Declaration
public virtual float Coord(int overlap, int maxOverlap)
Parameters
Type | Name | Description |
---|---|---|
System. |
overlap | the number of query terms matched in the document |
System. |
maxOverlap | the total number of terms in the query |
Returns
Type | Description |
---|---|
System. |
a score factor based on term overlap with the query |
GetSimScorer(Similarity.SimWeight, AtomicReaderContext)
Creates a new Similarity.
Declaration
public abstract Similarity.SimScorer GetSimScorer(Similarity.SimWeight weight, AtomicReaderContext context)
Parameters
Type | Name | Description |
---|---|---|
Similarity. |
weight | collection information from Compute |
Atomic |
context | segment of the inverted index to be scored. |
Returns
Type | Description |
---|---|
Similarity. |
Sloppy Similarity. |
Exceptions
Type | Condition |
---|---|
System. |
if there is a low-level I/O error |
QueryNorm(Single)
Computes the normalization value for a query given the sum of the
normalized weights Get
By default this is disabled (returns 1
), but some
implementations such as TFIDFSimilarity override this.
Declaration
public virtual float QueryNorm(float valueForNormalization)
Parameters
Type | Name | Description |
---|---|---|
System. |
valueForNormalization | the sum of the term normalization values |
Returns
Type | Description |
---|---|
System. |
a normalization factor for query weights |