Skip to main content

SearchHeadline

This class represents a database function that generates highlighted text fragments from a search query match. It allows for extensive customization of the output, including the ability to define custom highlight tags, fragment lengths, and delimiters. The class automatically handles the integration of search configurations and query expressions to produce formatted text results.

Attributes

AttributeTypeDescription
functionstring = ts_headlineThe name of the database function used to generate the search headline.
templatestring = %(function)s(%(expressions)s%(options)s)The SQL template string used to construct the function call with expressions and options.
output_field[TextField](../../../db/models/fields/textfield.md?sid=django_db_models_fields_textfield) = TextField()The database field type used to represent the resulting highlighted text.

Constructor

Signature

def SearchHeadline(
expression: Any,
query: Any,
config: Any = None,
start_sel: str = None,
stop_sel: str = None,
max_words: int = None,
min_words: int = None,
short_word: int = None,
highlight_all: bool = None,
max_fragments: int = None,
fragment_delimiter: str = None
) - > null

Parameters

NameTypeDescription
expressionAnyThe field or expression containing the text to be highlighted.
queryAnyThe search query to match against the text.
configAny = NoneOptional search configuration for language-specific processing.
start_selstr = NoneThe starting HTML tag or string for highlighting matches.
stop_selstr = NoneThe closing HTML tag or string for highlighting matches.
max_wordsint = NoneThe maximum number of words to include in the headline.
min_wordsint = NoneThe minimum number of words to include in the headline.
short_wordint = NoneWords shorter than this length will be discarded.
highlight_allbool = NoneWhether to highlight the entire document instead of a fragment.
max_fragmentsint = NoneThe maximum number of fragments to display.
fragment_delimiterstr = NoneThe delimiter used to separate text fragments.

Signature

def SearchHeadline(
expression: [Expression](../../../db/models/expressions/expression.md?sid=django_db_models_expressions_expression),
query: SearchQuery | str,
config: str | Expression,
start_sel: str,
stop_sel: str,
max_words: int,
min_words: int,
short_word: int,
highlight_all: bool,
max_fragments: int,
fragment_delimiter: str
) - > null

Parameters

NameTypeDescription
expression[Expression](../../../db/models/expressions/expression.md?sid=django_db_models_expressions_expression)The field or expression containing the text to be highlighted.
query`SearchQuerystr`
config`strExpression`
start_selstrThe HTML tag or string used to open the highlighted portion of a match.
stop_selstrThe HTML tag or string used to close the highlighted portion of a match.
max_wordsintThe maximum number of words to include in the resulting headline snippet.
min_wordsintThe minimum number of words to include in the resulting headline snippet.
short_wordintWords of this length or shorter will be discarded from the headline unless they are search terms.
highlight_allboolIf true, the entire document will be used as the headline instead of a fragment.
max_fragmentsintThe maximum number of fragments to display when multiple matches are found.
fragment_delimiterstrThe string used to separate multiple fragments in the output headline.

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: str,
template: str
) - > tuple

Generates the SQL fragment for the PostgreSQL ts_headline function, including the formatting options string.

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 string.
connectionBaseDatabaseConnectionThe current database connection used to handle vendor-specific SQL formatting.
functionstrAn optional override for the SQL function name, defaulting to ts_headline.
templatestrAn optional override for the SQL template string used to format the function call.

Returns

TypeDescription
tupleA tuple containing the SQL string with placeholders and the list of parameters to be merged by the database driver.