Generally, the length of content in a document is large. However, 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 to be displayed and the content to be 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 the document that contain the search queries 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 an API operation, the system automatically obtains the settings of the display effects, and adds them into the query clause. This way, you do not need to pass in the summary parameter again. You can also use the summary parameter in an API operation to configure the search result summary. For more information, see Initiate search requests.
Parameters
Field: the field for which you want to configure the search result summary.
Segment Length: the maximum length of the search result summary.
HTML Tag for Highlight: the name of the HTML tag that is used to highlight search queries in red.
Segment Connector: the connector that is used to connect segments.
Number of Segments: the number of segments that are required in the document.
Configure the search result summary in the OpenSearch console
1. Log on to the OpenSearch console. In the left-side navigation pane, click OpenSearch Industry Algorithm Edition and select OpenSearch High-performance Search Edition, and then choose Feature Extensions > Search Result Display. On the Search Result Display page, select an application for which you want to configure the search result summary from the drop-down list, and configure the search result summary in the Search Result Summary section.
2. The following figure shows an example of the search result summary configuration in which HTML Tag for Highlight is set to em for the title field.
Configure the search result summary by using the API or SDK
API: Use the summary parameter to configure the search result summary. For more information, see Initiate search requests.
SDK: The following sample code shows how to configure the search result summary by using the SDK for Java.
// Configure 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 the search result summary. The fields must be of the text type so that the fields can be analyzed. summ.setSummary_len("50");// Specify the maximum length of the search result summary. summ.setSummary_element("em"); // Specify the name of the HTML tag that is used to highlight search queries in red. summ.setSummary_ellipsis("...");// Specify the connector that is used to connect segments. summ.setSummary_snippet("1");// Specify the number of segments. // Add the summary parameter.
Note:
You can use the summary parameter in the SearchParams class of the SDK only to specify the name of the HTML tag that is used to highlight search queries in red. By default, the system encloses the tag name with angel brackets (< >) and places the tag at the beginning and end of the search query to be highlighted. For example, if you set the HTML tag name to em for a field, the search result for the field with the search query highlighted in red is returned in JSON format: Alibaba Cloud<em>OpenSearch</em>product document.
To specify an opening HTML tag and a closing HTML tag, use the SearchParamsBuilder object.
// Create a SearchParamsBuilder object. As the utility class of the SearchParams class, SearchParamsBuilder allows you to set parameters easily. SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams); // Use the SearchParamsBuilder object to add the 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 (;).
Usage notes
Only search queries can be highlighted in red. If you specify an HTML tag for a field, you must create an index on the field. Otherwise, search queries may not be properly highlighted in red in the search results.
You cannot specify an HTML tag for a field separately. You can 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 terms of search queries, the terms in the fields are displayed and highlighted in red in the search results.
The maximum length of the search result summary is 10,000 bytes. The hit terms that appear after 10,000 bytes are not highlighted in red.
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.
If two or more different analysis indexes are set in the same query clause of a search request, the specified content may not be highlighted in red or inappropriate content is highlighted in red.
If a segment that contains a search query 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 set the custom clause kvpairs=filter_punc:0 to skip the filtering.