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.
-
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.
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) |
|
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 |
|
highlightEncoder (optional) |
HighlightEncoder |
The encoding method for original text in highlighted fragments. Valid values:
|
|
fieldHighlightParams[].numberOfFragments (optional) |
Integer |
The maximum number of highlighted fragments to return for a field. We recommend that you set this parameter to |
|
fieldHighlightParams[].fragmentSize (optional) |
Integer |
The target length of each fragment. The default value is |
|
fieldHighlightParams[].preTag (optional) |
String |
The pre-tag for a matched term. The default value is |
|
fieldHighlightParams[].postTag (optional) |
String |
The post-tag for a matched term. The default value is |
|
fieldHighlightParams[].highlightFragmentOrder (optional) |
HighlightFragmentOrder |
The order in which multiple highlighted fragments are returned. |
Response
Query response
The search method returns a SearchResponse object. The following fields are relevant to summary and highlighting.
|
Name |
Type |
Description |
|
searchHits |
|
The query hits, which can be obtained by calling |
|
isAllSuccess |
boolean |
Indicates whether all index partitions were queried. You can obtain the value by calling |
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 |
|
highlightResultItem |
HighlightResultItem |
The summary and highlighting results of all fields in the row. You can obtain the value by calling |
Highlighting result
response.searchHits[].highlightResultItem is of the HighlightResultItem type and contains the following field.
|
Name |
Type |
Description |
|
highlightFields |
|
The highlighting results for fields in the row. You can obtain the map by calling |
Field fragments
response.searchHits[].highlightResultItem.highlightFields[fieldName] is of the HighlightField type and contains the following field.
|
Name |
Type |
Description |
|
fragments |
|
The highlighted fragments for the field. You can obtain the list by calling |