Skip to main content

SearchQuery

This class represents a database-agnostic search query that translates user-provided terms into a format suitable for full-text search engines. It supports multiple search types including plain text, phrases, raw queries, and web-style search syntax. The class also provides functionality for inverting search logic and configuring language-specific search settings.

Attributes

AttributeTypeDescription
output_field[SearchQueryField](searchqueryfield.md?sid=django_contrib_postgres_search_searchqueryfield) = SearchQueryField()The database field type used to represent the result of the search query expression.
SEARCH_TYPESdict = {"plain": "plainto_tsquery", "phrase": "phraseto_tsquery", "raw": "to_tsquery", "websearch": "websearch_to_tsquery"}A mapping of user-friendly search mode aliases to their corresponding PostgreSQL full-text search functions.

Constructor

Signature

def SearchQuery(
value: Any,
output_field: [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) = None,
config: Any = None,
invert: boolean = False,
search_type: string = "plain"
) - > null

Parameters

NameTypeDescription
valueAnyThe search term or expression to be converted into a tsquery.
output_field[Field](../../../forms/fields/field.md?sid=django_forms_fields_field) = NoneThe field type for the output of the expression.
configAny = NoneThe search configuration (dictionary) to use for the query.
invertboolean = FalseWhether to invert the search query logic.
search_typestring = "plain"The type of search to perform: 'plain', 'phrase', 'raw', or 'websearch'.

Signature

def SearchQuery(
value: string|Expression,
output_field: [Field](../../../forms/fields/field.md?sid=django_forms_fields_field),
config: string|Expression,
invert: boolean,
search_type: string
)

Parameters

NameTypeDescription
value`stringExpression`
output_field[Field](../../../forms/fields/field.md?sid=django_forms_fields_field)The model field used to represent the output of this expression.
config`stringExpression`
invertbooleanWhether to negate the search query using the logical NOT operator.
search_typestringThe conversion strategy to use; must be one of 'plain', 'phrase', 'raw', or 'websearch'.

Methods


as_sql()

@classmethod
def as_sql(
compiler: [SQLCompiler](../../../db/models/sql/compiler/sqlcompiler.md?sid=django_db_models_sql_compiler_sqlcompiler),
connection: BaseDatabaseConnection,
function: string,
template: string
) - > tuple

Generates the SQL fragment for the search query, wrapping the expression in a negation operator if the instance is inverted.

Parameters

NameTypeDescription
compiler[SQLCompiler](../../../db/models/sql/compiler/sqlcompiler.md?sid=django_db_models_sql_compiler_sqlcompiler)The query compiler instance responsible for generating the SQL statement.
connectionBaseDatabaseConnectionThe current database connection used to handle backend-specific SQL formatting.
functionstringAn optional override for the SQL function name used in the expression.
templatestringAn optional override for the SQL template string used to render the expression.

Returns

TypeDescription
tupleA tuple containing the formatted SQL string and the list of parameters for the database cursor.