Sorting by multiple keys

Managed by | Updated .

Background

It is sometimes desirable to sort the search results by more than one key. For example you may wish to sort by type then alphabetically by title.

Unfortunately Funnelback only supports result sorting on a single key. This article provides a workaround that explains how sorting can be achieved by more than one key.

Process

Because Funnelback only allows sorting on a single key the only way to achieve sorting by more than one key is to pre-generate a sort field within the metadata. This field needs to concatenate the values of the two keys that need to be sorted on with the primary sort field being concatenated to the secondary sort field.

This can be achieved in a number of different ways. Ideally the field should be generated and added to the source content downloaded by Funnelback. If indexing data from a database or CMS it's usually quite straightforward to configure the source system to generate an additional metadata or database field that is a concatenation of the two fields.

If you don't have control over the data source then it is possible to write a filter that extracts the two fields from the source data and generates a third field.

If you wish to sort on more than one key then you'll need to generate a sort field that concatenates the fields you wish to sort on together (in a way that will sort alphabetically) in your metadata and sort on that. If exporting from a DB it's usually quite straightforward to generate an extra sort field in the DB view that you are indexing.

Once the sort field is generated ensure that this field is included in the metamap (type 0 is sufficient) and configure the search to sort on this new field.

A similar philosophy or generating a sort field can be applied if you wish to apply some special rules to you alphabetical search such as ensuring numbers within a string a sorted correctly, or the sort ignores a leading The.

Example: sort by type and title

Consider the following data

TypeTitle
BookGreat Expectations
BookHarry Potter and the Goblet of Fire
BookThe BFG
DVDHarry Potter and the Goblet of Fire
DVDStar Wars

To sort by type then title would require a third field, sortKey to be generated:

TypeTitleSort_Key
BookGreat ExpectationsBookGreat Expectations
BookHarry Potter and the Goblet of FireBookHarry Potter and the Goblet of Fire
BookThe BFGBookThe BFG
DVDHarry Potter and the Goblet of FireDVDHarry Potter and the Goblet of Fire
DVDStar WarsDVDStar Wars

The following would then be added to the metamap.cfg

sortKey,0,Sort_Key

And sorting would then be achieved by adding -sort=metasortKey or &sort=metasortKey.

Was this artcle helpful?

Tags
Type: Keywords:
Features: