Skip to main content

SearchRank

This class represents a database function that calculates the relevance score of a search vector against a search query. It supports optional weighting, normalization, and cover density ranking by switching between the underlying ts_rank and ts_rank_cd functions. The class ensures that input vectors and queries are properly resolved and cast to the appropriate database types for execution.

Attributes

AttributeTypeDescription
functionstring = ts_rankThe name of the database function used to calculate the search rank, which defaults to 'ts_rank' but changes to 'ts_rank_cd' if cover_density is enabled.
output_field[FloatField](../../../forms/fields/floatfield.md?sid=django_forms_fields_floatfield)The database field type used to represent the calculated rank value, which is a FloatField.

Constructor

Signature

def SearchRank(
vector: SearchVector | Expression,
query: SearchQuery | Expression,
weights: list | Expression = None,
normalization: int | Expression = None,
cover_density: boolean = False
) - > null

Parameters

NameTypeDescription
vector`SearchVectorExpression`
query`SearchQueryExpression`
weights`listExpression` = None
normalization`intExpression` = None
cover_densityboolean = FalseIf True, uses the ts_rank_cd function which considers the proximity of matching lexemes.

Signature

def SearchRank(
vector: SearchVector | str,
query: SearchQuery | str,
weights: list | Value | None = None,
normalization: int | Value | None = None,
cover_density: bool = False
)

Parameters

NameTypeDescription
vector`SearchVectorstr`
query`SearchQuerystr`
weights`listValue
normalization`intValue
cover_densitybool = FalseWhen True, switches the underlying function to 'ts_rank_cd' which accounts for the proximity of matching lexemes.