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
| Attribute | Type | Description |
|---|---|---|
| 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_TYPES | dict = {"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
| Name | Type | Description |
|---|---|---|
| value | Any | The search term or expression to be converted into a tsquery. |
| output_field | [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) = None | The field type for the output of the expression. |
| config | Any = None | The search configuration (dictionary) to use for the query. |
| invert | boolean = False | Whether to invert the search query logic. |
| search_type | string = "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
| Name | Type | Description |
|---|---|---|
| value | `string | Expression` |
| output_field | [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) | The model field used to represent the output of this expression. |
| config | `string | Expression` |
| invert | boolean | Whether to negate the search query using the logical NOT operator. |
| search_type | string | The 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
| Name | Type | Description |
|---|---|---|
| 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. |
| connection | BaseDatabaseConnection | The current database connection used to handle backend-specific SQL formatting. |
| function | string | An optional override for the SQL function name used in the expression. |
| template | string | An optional override for the SQL template string used to render the expression. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing the formatted SQL string and the list of parameters for the database cursor. |