oo

Collapsing Search Results

Liferay DXP 2023.Q4+

You can collapse search results that have identical keyword field values. For example, an internal blog titled “Employee of the Month” produces multiple, identically titled search results. To show only the best matching result, use the collapse configuration. See Elasticsearch’s Collapse Search Results for details on how the feature works.

There are multiple blogs about the employee of the month.

By default, collapsing results causes inaccurate result counts and pagination, because the total hits in the search response does not account for collapsing. For example, imagine a search query that returns 100 results over 5 pages, where 90 results have identical titles. Collapsing these results by the title field results in this situation on Liferay’s search page:

  1. The search user sees one page of results with 11 results: the top scoring collapsed result and the 10 remaining unique results.
  2. The displayed results count is 100.
  3. The displayed pages count is 5.

Result counts and pagination don't work well with collapsed results.

You can fix this with a Search Blueprint.

Example: Collapse Results by Localized Title

  1. Open Site Menu (Site Menu) → Content & DataBlogs, and create three blogs with these field values:

    Blog 1, Title:

    Employee of the month
    

    Blog 1, Content:

    Meet Rex Randle, employee of the month for March.
    

    Blog 2, Title:

    Employee of the Month
    

    Blog 2, Content:

    Meet Olivia O'Neal, employee of the month for April.
    

    Blog 3, Title:

    Employee Of The Month
    

    Blog 3, Content:

    Meet Preston Palmer, employee of the month for May.
    
  2. Create a blueprint. Open Global Menu (Global Menu) → ApplicationsBlueprints (Search Experiences).

  3. Click New, enter the title Collapse by localized title, and click Create.

  4. Open the preview window and search for employee. Three results appear.

  5. Open Configuration and enter this in the Advanced Configuration field:

    {
       "collapse": {
          "field": "localized_title_${context.language_id}_sortable.keyword_lowercase"
       }
    }
    
  6. In the preview window, search for employee again. A single result appears.

  7. If you’re using the blueprint on a search page, save it and apply it to the search page.

The results are collapsed, but the count remains the same.

Capability: