All Products
Search
Document Center

Tablestore:Highlight the query results

Last Updated:Oct 17, 2024

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

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:

  • PLAIN (default): displays the highlighted text fragments without the need for encoding.

  • HTML_MODE: performs HTML encoding on the highlighted text fragments. After HTML encoding is complete, < is converted into &lt;, > into &gt;, " into &quot;, ' into &#x27;, and / into &#x2F;. If you want to display web pages, we recommend that you use the HTML format.

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: <em> and <b>. Default value: <em>. You can specify an opening tag based on your business requirements. The preTag parameter supports the following character sets: < > " ' /, a-z, A-Z, and 0-9.

post_tag

The closing tag used to highlight the query keywords. Examples: </em> and </b>. Default value: <em>. You can specify a closing tag based on your business requirements. The postTag parameter supports the following character sets: < > " ' /, a-z, A-Z, and 0-9.

fragments_order

The sorting rule of the highlighted text fragments that you want to return.

  • TEXT_SEQUENCE (default): The highlighted text fragments are sorted based on the order of their appearance in the original text.

  • SCORE: The highlighted text fragments are sorted based on the score of the hit keywords.

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.