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.
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:
- The search user sees one page of results with 11 results: the top scoring collapsed result and the 10 remaining unique results.
- The displayed results count is 100.
- The displayed pages count is 5.
You can fix this with a Search Blueprint.
Example: Collapse Results by Localized Title
-
Open Site Menu () → Content & Data → Blogs, 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.
-
Create a blueprint. Open Global Menu () → Applications → Blueprints (Search Experiences).
-
Click New, enter the title Collapse by localized title, and click Create.
-
Open the preview window and search for
employee
. Three results appear. -
Open Configuration and enter this in the Advanced Configuration field:
{ "collapse": { "field": "localized_title_${context.language_id}_sortable.keyword_lowercase" } }
-
In the preview window, search for employee again. A single result appears.
-
If you’re using the blueprint on a search page, save it and apply it to the search page.