Drupal and Search API: Unleash the power of aggregated fields

How to leverage Aggregated fields to solve common (yet not trivial) search requirements with Search API

Photo of Salvador Molina Moreno
Tue, 2015-12-01 10:45By salva

Search API is a powerful beast for Drupal 7. With its integration with views, Facet API and other search-related modules, you can build pretty much any kind of search page (and if something's not done already in contrib, Search API and Facet API offer clean ways to extend them). Having said that, it's also true that is a complex and big module (mostly because it's normally used alongside other modules that add plenty of features) that takes some time to learn, before you can take advantage of all its potential.

The problem

We've recently had a project where we were asked to allow users to sort search results by different criteria, one of them being alphabetical order. This feature, quite simple in principle, requires a little bit of extra steps to configure, and can easily catch out people with little experience using Search API. The reason is simple: Using Search API sorts module, is quite easy to allow for A-Z sorting on the title. You just index the title as "String" type, and enable the Alphabetical sorting in the "Sorts" tab.

The problem with that approach, then, is that you can't really search contents by title, because to do so, you need to index it as "Fulltext". And if you index it as "Fulltext", the A-Z sorting won't work. At this point, many would be already thinking about writing custom code to solve the problem. However, Search API has a very powerful feature that makes this task very easy to complete, without having to write a line of code.

Aggregated fields to the rescue

This one was quite new to me until recently. When configuring a Search API search index, one of the things you can control, is what (and how) fields are indexed. With aggregated fields, you can combine some of the fields that are going to be indexed into another new field (that doesn't need to exist elsewhere on your site). You can do this from the "Filters" tab of the index.

search api filters

That way, you can index each field as desired, but also make combinations of different fields, and store them as different data types, so that you can make use of them in Sort options, Facet plugins, etc... The next step, is to scroll down a little bit, and you'll find an "Aggregated fields" vertical tab, under "Callback settings". From there, you can add as many aggregated fields as you want. See below the configuration used to allow for A-Z sorting.

search api aggregated fields

As you can see, I simply select the "Title" of the node, and store it as "Fulltext". After saving an aggregated field, you can go back to the "Fields" tab and check that it appears there. As with any other field, you can select whether to index it or not, change the data type chosen, and assign any "Boost" options. That'd be fairly important in this particular scenario, as we'd probably want to assign the "Title" field a boost higher than the one used for other fields, so that contents with a matching title are more relevant in the search results. The next picture shows a fragment of the "Fields" tab for a Search API index after adding the aggregated field from the previous step.

search api fields

Not so difficult, right? Now we can index the title twice: once to allow for A-Z sorting, and an additional one to actually deliver a search page that works as any user would expect it. Go and have a play with Aggregated fields, you'll probably find they're very useful and can help in plenty of cases!