When you initiate a query request, you can enable the highlight feature. In this case, the keywords in the results that meet the query conditions are highlighted. Only Text fields support the highlight feature.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to the data table. For more information, see Create a data table and Write data.
A search index is created for the data table. For more information, see Create a search index.
Usage notes
Only Tablestore SDK for Python 6.0.0 or later provides the highlight feature. Before you use the highlight feature, we recommend that you install the latest version of Tablestore SDK for Python. For information about the versions of Tablestore SDK for Python, see Version history of Tablestore SDK for Python.
If you enable the highlight feature in a match query or match phrase query, the keywords in the query results are highlighted by using multiple opening tags (pre_tag) and closing tags (post_tag).
If the tokenization method of a Text field is maximum semantic unit-based tokenization (MaxWord), the highlight feature is not supported when you perform a match phrase query on the Text field.
If the keywords in queries are split, the keywords may not be highlighted as expected.
Parameters
Parameter | Description | |
highlight_encoder | The encoding method of the highlighted text fragments. Valid values:
| |
highlight | The highlight settings of the field. You can enable the highlight feature only for fields that contain keywords specified in SearchQuery objects. | |
highlight_parameters | number_of_fragments | The maximum number of highlighted text fragments that you want to return. We recommend that you set this parameter to 1. |
fragment_size | The length of each text fragment that you want to return. Default value: 100. Important The actual length of the returned text fragment may be different from the value of this parameter. | |
pre_tag | The opening tag used to highlight the query keywords. Examples: | |
post_tag | The closing tag used to highlight the query keywords. Examples: | |
fragments_order | The sorting rule of the highlighted text fragments that you want to return.
|
Example
The following sample code provides an example on how to use the MatchQuery feature to query data that matches hangzhou shanghai
from the Col_Text field and highlight the keywords in the query results. In this example, the Col_Text field is of the Text type.
def match_query_with_highlight(client):
query = MatchQuery('Col_Text', 'hangzhou shanghai')
highlight_parameter = HighlightParameter('Col_Text',None,None,pre_tag='<b>',post_tag='</b>')
highlight_clause = Highlight([highlight_parameter],HighlightEncoder.PLAIN_MODE)
search_response = client.search(
'<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
SearchQuery(query, limit=100, get_total_count=True,highlight=highlight_clause),
ColumnsToGet(return_type=ColumnReturnType.ALL)
)
print('----- Print Highlight Result:')
search_hits = search_response.search_hits
print('search hit count:%d' % len(search_hits))
for search_hit in search_hits:
print('\t score is %.6f' % search_hit.score)
for highlight_field in search_hit.highlight_result.highlight_fields:
print('\t\t highlight:%s:%s' % (highlight_field.field_name, highlight_field.field_fragments))
print('********** End HighlightQuery **********')
References
For information about the highlight feature, see Highlight keywords.
For information about how to use the highlight feature when you query nested fields, see Nested query.