All Products
Search
Document Center

Tablestore:Highlight

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

  • Tablestore SDK for Node.js V5.5.0 or later supports the highlight feature. Before you use the highlight feature, make sure that Tablestore SDK for Node.js V5.5.0 or later is obtained. For more information about the version history of Tablestore SDK for Node.js, see Version history of Tablestore SDK for Node.js.

  • If you enable the highlight feature in a match query or match phrase query, the query strings in the query results may be highlighted by using multiple opening tags (preTag) and closing tags (postTag).

  • 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 you want to return multiple fragments, the query strings in the fragments may be split. In this case, the query strings may not be highlighted.

Parameters

Parameter

Description

highlightEncoder

The encoding method for the highlighted text fragments. Valid values:

  • PLAIN_MODE: displays the highlighted text fragments without the need for encoding. This is the default value.

  • 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.

fieldHighlightParams

The highlight settings of the field. You can configure this parameter only for fields that contain the query strings specified in SearchQuery objects.

HighlightParameter

numberOfFragments

The maximum number of highlighted text fragments that you want to return. We recommend that you set this parameter to 1.

fragmentSize

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.

preTag

The opening tag that you want to use to highlight the query string. Examples: <em> and <b>. Default value: <em>. You can specify a custom opening tag based on your business requirements. The preTag parameter supports the following character sets: < > " ' /, a-z, A-Z, and 0-9.

postTag

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

highlightFragmentOrder

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

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

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

Examples

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.

client.search({
    tableName: "<TABLE_NAME>",
    indexName: "<SEARCH_INDEX_NAME>",
    searchQuery: {
        offset: 0,
        limit: 10, // To query only the number of rows that meet the query conditions without returning specific data, set the limit parameter to 0. 
        query: { // Set the query type to MatchQuery. 
            queryType: TableStore.QueryType.MATCH_QUERY,
            query: {
                fieldName: "Col_Text", // Specify the field that you want to match. 
                text: "hangzhou shanghai" // Specify the query string that you want to match. 
            }
        },
        highlight:{
            highlightEncoder:TableStore.HighlightEncoder.PLAIN_MODE,
            highlightParameters:[
                {
                    fieldName:"Col_Text",
                    preTag: "<b>",
                    postTag: "</b>",
                    fragmentsOrder: TableStore.HighlightFragmentOrder.TEXT_SEQUENCE,
                    fragmentSize: 20,
                    numberOfFragments: 3,
                }
                
            ],
        },
        getTotalCount: true // Specify whether to return the total number of rows that meet the query conditions. Default value: false. 
    },
    columnToGet: { // Specify the columns that you want to return. You can set the parameter to RETURN_SPECIFIED to return specific columns, RETURN_ALL to return all columns, RETURN_ALL_FROM_INDEX to return all columns in the search index, or RETURN_NONE to return only the primary key columns. 
        returnType: TableStore.ColumnReturnType.RETURN_ALL
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:', JSON.stringify(data.rows, null, 2));
    printSearchHit(data.searchHits, "");
});


/**
 * Display the content that meets the query conditions. 
 * @param searchHits searchHits
 * If the output uses the @param prefix Nested structure, add the prefix to display the hierarchy information. 
 */
function printSearchHit(searchHits, prefix) {
    TableStore.util.arrayEach(searchHits, function (searchHit) {
        if (searchHit.highlightResultItem != null) {
            console.log(prefix + "Highlight: \n");
            var strBuilder = ""
            for  (const [key,val]  of searchHit.highlightResultItem.highlightFields.entries()) {
                strBuilder += key + ":[";
                strBuilder += val.fragments.join(",") + "]\n";
                console.log(strBuilder);
            }
        }
        for  (const [key,val]  of searchHit.searchInnerHits.entries()) {
            console.log(prefix + "Path: " + key + "\n");
            console.log(prefix + "InnerHit: \n");
            printSearchHit(val.subSearchHits, prefix + "    ");
        }
    });
}

References

  • For information about the highlight feature, see Highlight.

  • For information about how to use the highlight feature when you query nested fields, see Nested query.