Generally, the length of content in a document is long. As a result, the full content of a document may not be displayed in the search results. In this case, OpenSearch provides the search result summary feature for you to specify the content that is to be displayed and highlighted in the search results. To help users understand the main content of a document and determine whether the document meets their expectations, the system displays several segments of content of documents that contain the search query terms in the search results. You can set the display effects of search results in the OpenSearch console. After you set the display effects, when you call API operations, the system automatically obtains the configurations that you set for the display effects, and adds them into search queries. This way, you do not need to pass in the summary parameter again. You can also set the summary parameter in an API operation to specify the search result summary. For more information, see Process search queries.
Field: the fields for which you want to configure a summary.
Segment Length: the length of a summary.
HTML Tag for Highlight: the HTML tag that is used to highlight query terms in red.
Segment Connector: the connector that is used to connect segments.
Number of Segments: the number of segments that are required in a search result summary within the specified length.
Configure and use the HTML tags in the OpenSearch console
1.Log on to the OpenSearch console. In the left-side navigation pane, choose Feature Extensions > Search Result Display. On the Search Result Display page, configure the search result summary.
2.The following figure shows an HTML tag that is configured for a field. HTML Tag for Highlight is set to em for the title field.
Configure HTML tags by using SDKs or calling API operations
API parameters: Specify the summary parameter.
SDKs: The following sample code shows how to set an HTML tag by calling the SDK for Java.
// Set the search result summary. The SearchParams class is used to add the search result summary. Summary summ = new Summary("name"); summ.setSummary_field("name");// Specify the fields for which you want to configure a summary. You must specify the fields of the TEXT type that can be analyzed. summ.setSummary_len("50");// Set the length of a segment. summ.setSummary_element("em"); // Set the HTML tag that is used to highlight the query term in red. summ.setSummary_ellipsis("...");// Set the connector that is used to connect segments. summ.setSummary_snippet("1");// Set the number of segments. // Add the summary parameter.
The summary parameter in the SearchParams class of the SDK cannot be used to set the prefix and suffix of an HTML tag that is used to highlight the query term in red. You must specify a complete HTML tag that consists of a prefix and suffix. By default, the prefix and suffix are <>. For example, set the HTML tag to em for a field. The query result for the field is returned in JSON format with the highlighted content in red: Alibaba Cloud<em>OpenSearch</em>product document.
To set the prefix and suffix of an HTML tag by using SDKs, use the searchParamsBuilder object.
// The utility class of the SearchParams class, which allows you to perform convenient operations. SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams); // Use the SearchParamsBuilder class to add a search result summary. paramsBuilder.addCustomParam("summary", "summary_field:name,summary_ellipsis:...,summary_snipped:1,summary_len:50,summary_element_prefix:<abc>,summary_element_postfix:</abc>"); // Separate multiple values of the summary_field parameter with semicolons (;).
You must separately specify an HTML tag for a field when you configure the search result summary for the field.
If you configure the search result summary for multiple fields and the values of the fields contain the query terms that are specified in the index, the query terms in the fields are displayed and highlighted in red in the search results.
If different analysis rules are configured for a field in an application, the content of the field may not be properly highlighted in red in the search results. For example, if you configure both the general analyzer for Chinese and the single character analyzer for Chinese for a field, only the content that is analyzed based on the single character analyzer for Chinese is highlighted in the search results or inappropriate content is highlighted in red in the search results.
In the same search query, if two or more different analysis indexes are configured, the specified content may not be highlighted in red or inappropriate content is highlighted in red.
If a segment that is to be highlighted in red contains special characters, the special characters are filtered out in the search results. If you want the special characters to be displayed in the search results, you can specify kvpairs=filter_punc:0 to skip the filtering in a CONFIG clause. For more information.