All Products
Search
Document Center

OpenSearch:Search result summary

Last Updated:Oct 24, 2023

In most cases, the content of a document is large in size. When you search for a document, the full content of the document may not be displayed. 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 query terms in the search results. You can configure the display effects of search results in the OpenSearch console. After you configure the display effects, when you call API operations, the system automatically obtains the configurations that you configure for the display effects, and adds them to search queries. This way, you do not need to pass in the summary parameter again. You can also configure the summary parameter in an API operation to specify the search result summary. For more information, see Initiate search requests.

Parameters

  • Field: the fields for which you want to configure a summary.

  • Segment Length: the maximum length of a segment.

  • 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 an HTML tag 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.

00000000000000000000

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.

2

Configure an HTML tag by using SDKs or calling API operations

  • API parameters: You can configure API parameters by specifying the summary parameter.

  • SDKs: The following sample code shows how to configure an HTML tag by calling SDK for Java.

    // Configure the search result summary. The SearchParams object 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");// Specify the length of a segment.
        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:

  • The summary parameter in the SearchParams object of the SDK cannot be used to configure 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 the JSON format with the highlighted content in red: Alibaba Cloud<em>OpenSearch</em>product document.

  • To configure the prefix and suffix of an HTML tag by using SDKs, use the SearchParamsBuilder object.

    // Create a SearchParamsBuilder object. As the utility class of SearchParams, the SearchParamsBuilder object allows you to configure query-related parameters with ease.
          SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
          // Use the SearchParamsBuilder object 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 (;).

    Usage notes

  • Only the query terms can be highlighted in red. You must create an index for the field that is used to highlight the query terms in red. Otherwise, an error may occur.

  • You must 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 a hit query term appears later in a document and the content length before the query term has exceeded the maximum limit of 10,000 bytes, the query term is not highlighted in red.

  • When you specify an HTML tag that is used to highlight query terms in red, the opening angle bracket (<) is escaped into &lt; and the closing angle bracket (>) is escaped into &gt;.

  • 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 undesired 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 undesired 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.

3