All Products
Search
Document Center

Tablestore:Summary and highlighting

Last Updated:Jul 27, 2026

Use summary and highlighting with the Tablestore SDK for Java to return fragments from Text fields that contain matched terms and mark the terms with configured tags.

Prerequisites

Install the Tablestore SDK for Java and initialize a client.

Feature description

Summary and highlighting extracts text fragments around terms matched by a query and encloses the matched terms in pre-tags and post-tags. You can use the fragments to display the context of query matches. This feature supports only Text fields.

Before you use summary and highlighting, set enableHighlighting to true for the field when you create a search index. In a query, use SearchQuery.highlight to specify the fields whose fragments you want to return and the fragment settings. A field must be used in a query condition that supports summary and highlighting. Otherwise, no highlighted fragments are returned for the field.

The following query types can return summary and highlighting results: TermQuery, TermsQuery, MatchQuery, MatchPhraseQuery, PrefixQuery, WildcardQuery, RangeQuery, BoolQuery, ConstScoreQuery, and NestedQuery.

For a BoolQuery or ConstScoreQuery, you can configure summary and highlighting for fields used by child queries of the preceding seven query types. For a NestedQuery, configure InnerHits.highlight to return summary and highlighting results for matched child rows. For more information, see Nested query.

Note
  • If you use MatchQuery or MatchPhraseQuery, a matched term may be enclosed by multiple pairs of pre-tags and post-tags.

  • If maximum semantic tokenization is used for a Text field, MatchPhraseQuery does not support summary and highlighting.

  • A fragment boundary may split a matched term. In this case, the term is not highlighted.

The following example uses a match query to retrieve rows in which the description field contains the tablestore term. The example encloses matched terms in the returned fragments with <b> and </b>. The description field is a Text field for which summary and highlighting is enabled.

String tableName = "example_table";
String indexName = "example_index";

MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("description");
matchQuery.setText("tablestore");

HighlightParameter highlightParameter = new HighlightParameter();
highlightParameter.setPreTag("<b>");
highlightParameter.setPostTag("</b>");

Highlight highlight = new Highlight();
highlight.addFieldHighlightParam("description", highlightParameter);

SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchQuery);
searchQuery.setHighlight(highlight);
searchQuery.setLimit(10);

SearchRequest request = new SearchRequest(tableName, indexName, searchQuery);
SearchResponse response = client.search(request);
for (SearchHit hit : response.getSearchHits()) {
    HighlightResultItem resultItem = hit.getHighlightResultItem();
    if (resultItem == null) {
        continue;
    }
    HighlightField field = resultItem.getHighlightFieldByName("description");
    if (field != null) {
        System.out.println(field.getFragments());
    }
}

Parameters

request.searchQuery.highlight is of the Highlight type. The values in fieldHighlightParams are HighlightParameter objects that configure the fragment behavior for individual fields.

The following table uses full parameter paths to describe the two configuration levels: Highlight and HighlightParameter.

Name

Type

Description

fieldHighlightParams (required)

Map<String, HighlightParameter>

The field names and fragment settings. A key is the name of a field whose summary and highlighting results you want to return. Its value is the HighlightParameter object for the field. The field must have summary and highlighting enabled and must be used in a supported query condition.

highlightEncoder (optional)

HighlightEncoder

The encoding method for original text in highlighted fragments. Valid values:

  • PLAIN (default): Does not encode original text.

  • HTML: HTML-escapes original text. Use this value if you display results on a web page. The characters <, >, ", ', and / are escaped as &lt;, &gt;, &quot;, &#x27;, and &#x2F;, respectively.

fieldHighlightParams[].numberOfFragments (optional)

Integer

The maximum number of highlighted fragments to return for a field. We recommend that you set this parameter to 1.

fieldHighlightParams[].fragmentSize (optional)

Integer

The target length of each fragment. The default value is 100. The actual fragment length may differ from this value.

fieldHighlightParams[].preTag (optional)

String

The pre-tag for a matched term. The default value is <em>. You can specify a custom tag such as <b>. You must configure preTag and postTag together. The supported characters are < > " ' /, a-z, A-Z, and 0-9.

fieldHighlightParams[].postTag (optional)

String

The post-tag for a matched term. The default value is </em>. You can specify a custom tag such as </b>. You must configure preTag and postTag together. The supported characters are the same as those for preTag.

fieldHighlightParams[].highlightFragmentOrder (optional)

HighlightFragmentOrder

The order in which multiple highlighted fragments are returned. TEXT_SEQUENCE (default) sorts fragments by their positions in the original text. SCORE sorts fragments by the relevance scores of matched terms.

Response

Query response

The search method returns a SearchResponse object. The following fields are relevant to summary and highlighting.

Name

Type

Description

searchHits

List<SearchHit>

The query hits, which can be obtained by calling getSearchHits(). Each element contains row data and summary and highlighting results.

isAllSuccess

boolean

Indicates whether all index partitions were queried. You can obtain the value by calling isAllSuccess(). If the value is false, partial results are returned.

Query hit

response.searchHits[] is of the SearchHit type. The following fields are relevant to summary and highlighting.

Name

Type

Description

row

Row

The row data. You can obtain the value by calling getRow().

highlightResultItem

HighlightResultItem

The summary and highlighting results of all fields in the row. You can obtain the value by calling getHighlightResultItem(). The value is null if the row has no highlighting results.

Highlighting result

response.searchHits[].highlightResultItem is of the HighlightResultItem type and contains the following field.

Name

Type

Description

highlightFields

Map<String, HighlightField>

The highlighting results for fields in the row. You can obtain the map by calling getHighlightFields(). You can also call getHighlightFieldByName(fieldName) to obtain the result for a specific field. The method returns null if the field has no highlighting result.

Field fragments

response.searchHits[].highlightResultItem.highlightFields[fieldName] is of the HighlightField type and contains the following field.

Name

Type

Description

fragments

List<String>

The highlighted fragments for the field. You can obtain the list by calling getFragments(). Matched terms in the fragments are enclosed by the configured pre-tag and post-tag.