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
| Attribute | Type | Description |
|---|---|---|
| function | string = "to_tsvector" | The PostgreSQL text search function used to convert the input expressions into a search vector. |
| arg_joiner | string = " | |
| 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
| Name | Type | Description |
|---|---|---|
| *expressions | any | One or more field names or expressions to be converted into a search vector. |
| config | any = None | The search configuration to use for the vector (e.g., a language). |
| weight | any = None | An 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
| Name | Type | Description |
|---|---|---|
| query | django.db.models.sql.query.Query = null | The current query object used to resolve field names and track joins. |
| allow_joins | boolean = true | Determines whether the expression resolution is permitted to introduce new table joins into the query. |
| reuse | set = null | A set of reusable joins to optimize query performance by avoiding redundant table connections. |
| summarize | boolean = false | Indicates if the expression is being resolved as part of an aggregate or summary calculation. |
| for_save | boolean = false | Specifies if the resolution is occurring during a model save operation, affecting how certain expressions are evaluated. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| compiler | django.db.models.sql.compiler.SQLCompiler | The query compiler instance responsible for translating expressions into database-specific SQL. |
| connection | django.db.backends.base.base.BaseDatabaseWrapper | The active database connection used to determine vendor-specific SQL syntax and escaping. |
| function | string = null | An optional override for the database function name used to generate the search vector. |
| template | string = null | An optional format string used to structure the generated SQL, allowing customization of how functions and arguments are rendered. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing the SQL string with placeholders and a list of parameters to be interpolated by the database driver. |