Skip to content

Custom sorting of faceted navigation

Introduction

The category values in faceted navigation are sorted server side based on the facet's configuration which can be configured from the facet UI.

Custom sorting

If none of the built in ways of sorting are suitable, it is possible to implement your own custom sort order. To do this, a three step process is required:

  1. Write a groovy script or compile a custom comparator in java.
  2. Enable the custom comparator on the facet in the hook_post_process.groovy hook script.
  3. Choose when the custom comparator should apply in the faceted navigation user interface.

Implement the comparator

Comparators need to implement a java.util.Comparator. Here is an example of comparator which sorts on label.

package com.foo;

import java.util.Comparator;

import com.funnelback.publicui.search.model.transaction.Facet.CategoryValue;

public class MyComparator implements Comparator<CategoryValue> {

    @Override
    public int compare(CategoryValue o1, CategoryValue o2) {
        // lowercase the label as we don't want the sort to be case sensitive.
        return o1.getLabel().toLowerCase().compareTo(o2.getLabel().toLowerCase());
    }
}

We could place our code in file:

$SEARCH_HOME/conf/<collection>/@groovy/com/foo/MyComparator.groovy

The @groovy/com/foo path may need to be created.

Set the comparator on the facet

We will need to set the comparator on the facet in the hook_post_process.groovy hook script. To do this we will create or modify the file:

$SEARCH_HOME/conf/<collection>/hook_post_process.groovy

to contain:

import com.funnelback.publicui.search.model.transaction.Facet;

for(Facet facet : transaction.getResponse().getFacets()) {
    // Find the facet we want to set out comparator on.
    if("Authors".equals(facet.getName())) {
        facet.setCustomComparator(new com.foo.MyComparator());
    }
}

Update the facet config to use the custom comparator

The facet configuration will need to be updated to include the CUSTOM_COMPARATOR sort order, this can be done with the facet editor UI. The custom sort may be used with other predefined sorting methods for example you can sort by count then by your custom comparator.

See also

top

Funnelback logo
v15.12.0