Skip to main content

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

AttributeTypeDescription
prefixbooleanBoolean flag indicating if the lexeme should be treated as a prefix match in the search query.
invertbooleanBoolean flag that determines whether to negate the lexeme match using a logical NOT operator.
weightstringThe 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

NameTypeDescription
valuestrThe string value of the lexeme.
output_field[SearchQueryField](searchqueryfield.md?sid=django_contrib_postgres_search_searchqueryfield) = NoneThe field to be used for the output.
invertbool = FalseWhether to invert the lexeme search.
prefixbool = FalseWhether to treat the lexeme as a prefix.
weightstr = NoneThe 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

NameTypeDescription
valuestringThe 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.
invertbooleanWhether to negate this lexeme in the search query.
prefixbooleanWhether to treat the lexeme as a prefix for partial matching.
weightstringThe 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

NameTypeDescription
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.
connectionDatabaseConnectionThe current database connection used for backend-specific SQL formatting.

Returns

TypeDescription
tupleA tuple containing the SQL placeholder string and a list of formatted lexeme parameters.