Lexeme
This class represents a single search term or lexeme within a full-text search query. It supports advanced search features such as prefix matching, weight assignments, and logical inversion. The class handles the validation of search values and weights while providing the necessary logic to compile the lexeme into a formatted SQL string.
Attributes
| Attribute | Type | Description |
|---|---|---|
| prefix | boolean | Boolean flag indicating if the lexeme should be treated as a prefix match in the search query. |
| invert | boolean | Boolean flag that determines whether to negate the lexeme match using a logical NOT operator. |
| weight | string | The importance ranking assigned to the lexeme, which must be one of 'A', 'B', 'C', or 'D'. |
Constructor
Signature
def Lexeme(
value: str,
output_field: [SearchQueryField](searchqueryfield.md?sid=django_contrib_postgres_search_searchqueryfield) = None,
invert: bool = False,
prefix: bool = False,
weight: str = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| value | str | The string value of the lexeme. |
| output_field | [SearchQueryField](searchqueryfield.md?sid=django_contrib_postgres_search_searchqueryfield) = None | The field to be used for the output. |
| invert | bool = False | Whether to invert the lexeme search. |
| prefix | bool = False | Whether to treat the lexeme as a prefix. |
| weight | str = None | The weight of the lexeme (A, B, C, or D). |
Signature
def Lexeme(
value: string,
output_field: [Field](../../../forms/fields/field.md?sid=django_forms_fields_field),
invert: boolean,
prefix: boolean,
weight: string
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The search term string; must be non-empty. |
| output_field | [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) | The model field used to resolve the output type of this expression. |
| invert | boolean | Whether to negate this lexeme in the search query. |
| prefix | boolean | Whether to treat the lexeme as a prefix for partial matching. |
| weight | string | The importance weight assigned to the lexeme, must be one of 'A', 'B', 'C', or 'D'. |
Methods
as_sql()
@classmethod
def as_sql(
compiler: [SQLCompiler](../../../db/models/sql/compiler/sqlcompiler.md?sid=django_db_models_sql_compiler_sqlcompiler),
connection: DatabaseConnection
) - > tuple
Generates the SQL fragment and parameters for the lexeme, including prefix, weight, and inversion modifiers.
Parameters
| Name | Type | Description |
|---|---|---|
| compiler | [SQLCompiler](../../../db/models/sql/compiler/sqlcompiler.md?sid=django_db_models_sql_compiler_sqlcompiler) | The query compiler instance used to generate the SQL statement. |
| connection | DatabaseConnection | The current database connection used for backend-specific SQL formatting. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing the SQL placeholder string and a list of formatted lexeme parameters. |