Use the Tablestore SDK for Go to return text fragments from a Text field and mark matched terms.
Prerequisites
Before you begin, complete the following preparations:
-
Install the Tablestore Go SDK and initialize a client.
-
When you create the search index, enable EnableHighlighting for the Text field. For more information, see Create a search index.
Description
Summary and highlighting extracts fragments from a matched Text field and marks matched terms with configured tags. The Tablestore SDK for Go supports highlighting for term, terms, prefix, wildcard, range, Boolean, constant score, match, match phrase, and nested queries (by using InnerHits).
If you use
MatchQueryorMatchPhraseQuery, a matched term may be enclosed by multiple pairs of pre-tags and post-tags.If maximum semantic tokenization is used for a
Textfield,MatchPhraseQuerydoes not support summary and highlighting.A fragment boundary may split a matched term. In this case, the term is not highlighted.
highlight := search.NewHighlight().
AddFieldHighlightParameter(
"title",
search.NewHighlightParameter().
SetNumberOfFragments(3).
SetPreTag("<em>").
SetPostTag("</em>"),
)
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchQuery{FieldName: "title", Text: "go search"}).
SetHighlight(highlight)
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
for _, hit := range response.SearchHits {
fmt.Println(hit.HighlightResultItem)
}
Parameters
Highlight configuration
|
Name |
Type |
Description |
|
FieldHighlightParameters (required) |
map[string]*search.HighlightParameter |
A mapping between field names and highlight parameters. A field must have summary and highlighting enabled and be used in a supported query condition. Otherwise, no highlighted fragments are returned for the field. |
|
HighlightEncoder (optional) |
*search.HighlightEncoder |
The encoding method for the original fragment text. Valid values:
|
Field highlight parameters
|
Name |
Type |
Description |
|
NumberOfFragments (optional) |
*int32 |
The maximum number of fragments to return. Recommended value: 1. |
|
FragmentSize (optional) |
*int32 |
The target length of each fragment. Default value: 100. The actual fragment length may differ. |
|
PreTag (optional) |
*string |
The tag inserted before a matched term. Default value: |
|
PostTag (optional) |
*string |
The tag inserted after a matched term. Default value: |
|
HighlightFragmentOrder (optional) |
*search.HighlightFragmentOrder |
The fragment sort order. TextSequence sorts fragments by their positions in the source text and is the default. Score sorts fragments by match score. |
Response
After highlighting is configured, read field fragments from SearchResponse.SearchHits[].HighlightResultItem.HighlightFields. For a nested query, read highlighting results from the hits in SearchInnerHits.