When you query data, you can set highlighting parameters to return text snippets that contain the search query and highlight the query. The summary and highlighting feature is supported only for Text type fields.
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 data tables and Write data.
A search index is created for the data table. For more information, see Create a search index.
Notes
The query highlighting feature is available in Tablestore Node.js Software Development Kit (SDK) 5.5.0 and later. Ensure that you are using a compatible SDK version. For more information about the release history of the Node.js SDK, see Node.js SDK release history.
When you use the query highlighting feature with a MatchQuery or MatchPhraseQuery, the search query may be highlighted by multiple pre-tags and post-tags.
If a Text field uses maximum semantics tokenization, the query highlighting feature is not supported for MatchPhraseQuery.
A search query might not be highlighted if sharding splits the query within the source text.
Parameters
Parameter | Description | |
highlightEncoder | The encoding method for the original content of the highlighted fragments. Valid values:
| |
fieldHighlightParams | The highlighting parameters for a field. You can set this parameter only for fields that are included in a keyword query within SearchQuery. | |
HighlightParameter | numberOfFragments | The maximum number of highlighted fragments to return. We recommend setting this to 1. |
fragmentSize | The length of each fragment. Default value: 100. Important The length of the returned fragments may not be exactly equal to this value. | |
preTag | The pre-tag for highlighting the search query, such as | |
postTag | The post-tag for highlighting the search query, such as | |
highlightFragmentOrder | The sorting rule for fragments when multiple fragments are returned for a highlighted field.
| |
Example
The following example shows how to use MatchQuery to find data where the Col_Text column matches hangzhou shanghai and how to highlight the search query in the results. The Col_Text column is a Text type.
client.search({
tableName: "<TABLE_NAME>",
indexName: "<SEARCH_INDEX_NAME>",
searchQuery: {
offset: 0,
limit: 10, // To get only the row count without any specific data, set limit to 0. This prevents any rows from being returned.
query: { // Set the query type to MatchQuery.
queryType: TableStore.QueryType.MATCH_QUERY,
query: {
fieldName: "Col_Text", // Set the column to match.
text: "hangzhou shanghai" // Set the value to match.
}
},
highlight:{
highlightEncoder:TableStore.HighlightEncoder.PLAIN_MODE,
highlightParameters:[
{
fieldName:"Col_Text",
preTag: "",
postTag: "",
fragmentsOrder: TableStore.HighlightFragmentOrder.TEXT_SEQUENCE,
fragmentSize: 20,
numberOfFragments: 3,
}
],
},
getTotalCount: true // TotalCount in the results indicates the total number of rows in the table. The default value is false, which means the total count is not returned.
},
columnToGet: { // The columns to return. Valid values: RETURN_SPECIFIED (return custom columns), RETURN_ALL (return all columns), RETURN_ALL_FROM_INDEX (return all columns from the search index), and RETURN_NONE (return no 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, "");
});
/**
* Print the content of searchHits.
* @param searchHits The search hits.
* @param prefix The prefix to add for nested structures to print hierarchical 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 more information about the query highlighting feature, see Summary and highlighting.
For more information about using the query highlighting feature with a nested field, see Nested query.