Skip to main content

SearchVector

This class represents a searchable vector by converting one or more expressions into a format suitable for full-text search. It supports combining multiple fields, applying specific language configurations, and assigning weights to control the importance of different search components. The class automatically handles type casting and null values to ensure consistent search indexing.

Attributes

AttributeTypeDescription
functionstring = "to_tsvector"The PostgreSQL text search function used to convert the input expressions into a search vector.
arg_joinerstring = "
output_field[SearchVectorField](searchvectorfield.md?sid=django_contrib_postgres_search_searchvectorfield) = SearchVectorField()The database field type used to represent the resulting search vector in the query results.

Constructor

Signature

def SearchVector(
*expressions: any,
config: any = None,
weight: any = None
) - > null

Parameters

NameTypeDescription
*expressionsanyOne or more field names or expressions to be converted into a search vector.
configany = NoneThe search configuration to use for the vector (e.g., a language).
weightany = NoneAn optional weight (A, B, C, or D) to assign to the vector for ranking purposes.

Methods


resolve_expression()

@classmethod
def resolve_expression(
query: django.db.models.sql.query.Query = null,
allow_joins: boolean = true,
reuse: set = null,
summarize: boolean = false,
for_save: boolean = false
) - > [SearchVector](searchvector.md?sid=django_contrib_postgres_search_searchvector)

Prepares the search vector and its associated configuration for use in a database query by resolving field lookups and joins.

Parameters

NameTypeDescription
querydjango.db.models.sql.query.Query = nullThe current query object used to resolve field names and track joins.
allow_joinsboolean = trueDetermines whether the expression resolution is permitted to introduce new table joins into the query.
reuseset = nullA set of reusable joins to optimize query performance by avoiding redundant table connections.
summarizeboolean = falseIndicates if the expression is being resolved as part of an aggregate or summary calculation.
for_saveboolean = falseSpecifies if the resolution is occurring during a model save operation, affecting how certain expressions are evaluated.

Returns

TypeDescription
[SearchVector](searchvector.md?sid=django_contrib_postgres_search_searchvector)A resolved copy of the search vector with all expressions and configurations bound to the current query context.

as_sql()

@classmethod
def as_sql(
compiler: django.db.models.sql.compiler.SQLCompiler,
connection: django.db.backends.base.base.BaseDatabaseWrapper,
function: string = null,
template: string = null
) - > tuple

Generates the SQL fragment for the search vector, handling type casting to text, null coalescing, and optional weight assignments.

Parameters

NameTypeDescription
compilerdjango.db.models.sql.compiler.SQLCompilerThe query compiler instance responsible for translating expressions into database-specific SQL.
connectiondjango.db.backends.base.base.BaseDatabaseWrapperThe active database connection used to determine vendor-specific SQL syntax and escaping.
functionstring = nullAn optional override for the database function name used to generate the search vector.
templatestring = nullAn optional format string used to structure the generated SQL, allowing customization of how functions and arguments are rendered.

Returns

TypeDescription
tupleA tuple containing the SQL string with placeholders and a list of parameters to be interpolated by the database driver.