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
| Attribute | Type | Description |
|---|---|---|
| function | string = ts_headline | The name of the database function used to generate the search headline. |
| template | string = %(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
| Name | Type | Description |
|---|---|---|
| expression | Any | The field or expression containing the text to be highlighted. |
| query | Any | The search query to match against the text. |
| config | Any = None | Optional search configuration for language-specific processing. |
| start_sel | str = None | The starting HTML tag or string for highlighting matches. |
| stop_sel | str = None | The closing HTML tag or string for highlighting matches. |
| max_words | int = None | The maximum number of words to include in the headline. |
| min_words | int = None | The minimum number of words to include in the headline. |
| short_word | int = None | Words shorter than this length will be discarded. |
| highlight_all | bool = None | Whether to highlight the entire document instead of a fragment. |
| max_fragments | int = None | The maximum number of fragments to display. |
| fragment_delimiter | str = None | The 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
| Name | Type | Description |
|---|---|---|
| expression | [Expression](../../../db/models/expressions/expression.md?sid=django_db_models_expressions_expression) | The field or expression containing the text to be highlighted. |
| query | `SearchQuery | str` |
| config | `str | Expression` |
| start_sel | str | The HTML tag or string used to open the highlighted portion of a match. |
| stop_sel | str | The HTML tag or string used to close the highlighted portion of a match. |
| max_words | int | The maximum number of words to include in the resulting headline snippet. |
| min_words | int | The minimum number of words to include in the resulting headline snippet. |
| short_word | int | Words of this length or shorter will be discarded from the headline unless they are search terms. |
| highlight_all | bool | If true, the entire document will be used as the headline instead of a fragment. |
| max_fragments | int | The maximum number of fragments to display when multiple matches are found. |
| fragment_delimiter | str | The 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
| 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 string. |
| connection | BaseDatabaseConnection | The current database connection used to handle vendor-specific SQL formatting. |
| function | str | An optional override for the SQL function name, defaulting to ts_headline. |
| template | str | An optional override for the SQL template string used to format the function call. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing the SQL string with placeholders and the list of parameters to be merged by the database driver. |