Sorting Search Results with the Sort Widget

The Sort widget gives Users configurable control over the order of returned results: no code necessary.

Add it to a page and begin sorting results.

By default, results are sorted by the relevance score returned by the search engine. Users can choose from one of the out-of-the-box alternative sorting strategies, or one configured by a search administrator.

Out of the box, order results in these ways as an alternative to relevance sorting:

  • alphabetically by Title
  • by the Modified date (oldest first)
  • by the Create date (newest first)
  • by the Create date (oldest first)
  • alphabetically by the User that created each matching asset

If the out-of-the-box alternatives aren’t enough, an administrator can create additional sort options from the widget’s configuration.

It’s also possible to delete unwanted sort options from the widget.

Adding and Configuring the Sort Widget

To get started with the Sort widget,

  1. Open the Add menu (Add) for the page and expand the Widgets section.

  2. From the Search section, drag a Sort widget onto the page.

Figure 1: Users can re-order search results with the Sort widget.

Figure 1: Users can re-order search results with the Sort widget.

Configuring the Sort Widget

Three things can be done from the Configuration screen:

  • Editing existing Sort options
  • Deleting options
  • Adding options

Figure 2: From the Sort widgets configuration, add, edit, or remove Sort options.

Figure 2: From the Sort widget's configuration, add, edit, or remove Sort options.

To access the widget configuration screen, open the widget Options menu (Options) and click Configuration.

Each Sort option has two fields: Label and Field.

Label
Set the displayed label for the type of sort being configured.
Field
The fieldName of the indexed field to sort. Most of the time this is a keyword field. Other acceptable options are date and any numeric datatype. There’s even a way for persistent search administrators to coerce text fields into behaving with the Sort widget. Keep reading for details.

Finding Sortable Fields

To find the fields available for use in the Sort widget, Users with the proper permissions can navigate to Control PanelConfigurationSearch. From there, open the Field Mappings tab and browse the mappings for each index. Scroll to the properties section of the mapping, and find any keyword field, date field, or a field with any numeric datatype. The type field is instructive:

"type" : "keyword"

"type" : "date"

"type" : "long"

What if you really need to sort by a text field? You can do it by adding a new version of the field to the index, with the type keyword. Don’t worry; you don’t need to code anything to do this. From the field mappings screen mentioned above, look at the firstName field in the index called liferay-[companyID]. In fact, look at the next entry as well:

"firstName" : {
    "type" : "text",
    "store" : true
},
"firstName_sortable" : {
    "type" : "keyword",
    "store" : true
},

There’s a corresponding field with the suffix _sortable, and of the correct type for sorting (keyword). How did that get there? Via the portal property

index.sortable.text.fields=firstName,jobTitle,lastName,name,screenName,title

All the text fields listed here have a fieldName_sortable counterpart created automatically in the index. To add more, copy this value into a portal-ext.properties file into your Liferay Home folder, add any new field names you need to sort by, and restart the server.

Adding New Sort Options

To sort by the new field, use the plus symbol below any option’s Field configuration make sure to use the fieldName_sortable version of the field in the widget configuration.

To add a new sort option that’s already of the proper datatype, use the plus symbol below any option’s Field configuration and fill in the fields. The order of options here in the configuration screen matches the order Users see in the select list while configuring the widget for their search.

Editing and Deleting Sort Options

To edit an existing option, edit the text in its configuration section.

To delete an existing option, use the minus symbol below its Field configuration.

Controlling the Sort Order

To control the order for the sort option, add a plus or minus symbol after the fieldName. Look how it’s done for the existing sort options labeled Created and Created (oldest first) to understand how it works:

Label: Created Field: createDate-

The - sign following the field name indicates that the order is descending. Choosing to sort with this brings search results created most recently to the top of the list.

Label: Created (oldest first) Field: createDate+

The + sign following the field name indicates that the order is ascending. Choosing to sort with this brings the oldest (by creation date) results to the top of the list.

« Filtering Search Results with the Custom Filter WidgetSearch Results Behavior »
Was this article helpful?
0 out of 0 found this helpful