Solr, Drupal 7, and faceted search
It's easy to get started with the search server Apache Solr and the popular CMS platform Drupal, as described in our previous tutorial on setting up Solr 4.2 with Drupal 7. Straight out of the box Solr handles basic text searching, but you can increase its power by adding faceted search – the ability to filter on specific facets or aspects of the data on a site. For example, on a Drupal site you might want to be able to filter your searching by author, date, or tags. Solr's faceted search allows users to combine this type of filtering with text searching to find the information they're after faster. Read on for the lowdown on setting up and configuring basic faceted search using Solr 4.2 and Drupal 7, running on Apache on Linux.
The Facet API module for Solr on Drupal will get you set up with faceted search. It doesn't come by default with the Solr Drupal module; you have to install it independently. You can download it from the website and install it manually into sites/all/modules in your Drupal install, or install it using Drush, a command-line interface to Drupal. Drush is worth installing if you have more than one or two modules on your site, as it makes it quicker to work with them from the command line. To get Facet API running, you also need to install its dependency, the Chaos Tools module. From the command line (as root/sudo) type:
drush dl facetapi
drush dl ctools
Once the modules are installed, go to the Drupal admin console and click the Modules tab (http://localhost/?q=admin/modules). Select Chaos Tools in the Chaos Tools Suite section, and Current Search Blocks and Facet API in the Search section, then click Save Configuration.
To configure the facets you want users to be able to search on, go to the Apachesolr module configuration on Drupal (http://localhost/?q=admin/config/search/apachesolr/settings) and click the Facets link next to localhost. You'll see the list of default facet blocks that you can enable: author, content type, language, post date, tags, and update date. Enable only the ones that you wish to have available to users, because facet indexing requires system resources, and indexing facets that you won't use may adversely affect system performance.
Once you've enabled the facets, if you reload the http://localhost/?q=admin/config/search/apachesolr/settings page, you should see a configuration drop-down link next to each enabled facet:
- Configure display controls how the facet links show up on the search page (as links or links with checkboxes, which can help users keep track of what they're searching on), how they are sorted, and whether you use AND or OR when applying this facet.
- Configure dependencies controls under what circumstances the facet is shown. You can hide facets on pages or articles, or when specific users are logged in.
- Configure filters controls whether certain items are shown, and how many levels are displayed for multilevel facets such as tags.
- Export configuration shows you the code associated with the current configuration so you can save it.
Set these up to meet the needs of your specific site and save the configuration. You can then go to the search page and see how your settings appear. At this point they'll only show up on the search page, so you can filter your search results once they come back. Try them out to make sure everything is working as expected.
Showing facets outside of search pages
What if you want facets and facet search to show on non-search pages? For example, many sites show a tag cloud or dates of articles in a sidebar. These are both examples of faceted search, and you can use Facet API and Solr to set them up for your Drupal site.
To show facets on all pages, go back to the Facets settings admin page (http://localhost/?q=admin/config/search/apachesolr/settings/solr/facets) and click "Show facets on non-search pages." You'll then see a box that allows you to enter the paths of the pages on which you want facets to be displayed. Note, however, that you cannot just use * to display facets on all paths; this leads to a bug where faceted searches will fail with no results. Instead, you need to list specific paths. A good starting point is:
This will cover the default front page (
node), all pages and articles (
node/*), and all user pages (
user/*). You may of course wish to add other paths for your site; just don't add
search/* or you'll run into the same bug!
Once you've enabled the facets, you need to decide where to display them on the page; until you do they won't be visible. Go to the Blocks admin page (http://localhost/?q=admin/structure/block/) and scroll down to the Disabled section, then click Configure for each facet you wish to display, and choose where you wish to display each facet. You could, for example, put all your enabled facet searches in the second sidebar. Save the configuration, then check your site to see the new facets in the place you've chosen to display them.
Other facet-related modules
A lot of sites these days show tags as a cloud rather than as a list, where the font size of each tag increases along with how often it appears. The facetapi_tagcloud Drupal module allows you to display your Solr facets this way. Install it manually or with Drush, and enable it from the Modules admin page. Then go to the Facet configuration page, click on tag (http://localhost/?q=admin/config/search/facetapi/apachesolr%40solr/block/im_field_tags/edit), and choose "Tagcloud links" as the display widget. Note that this module is in beta, so use with care.
Another interesting module, facetapi_multiselect, which gives you a multi-select view for faceted search, is unfortunately not yet available as a recommended release for Drupal 7. You could however download the development release and try that at your own risk.
You might also consider building your own facets if the built-in ones don't meet the needs of your site. Unfortunately the module apachesolr_facetbuilder, which allows you to build facets without writing code, isn't available for Drupal 7 (and doesn't appear as if it's being updated) so you'll have to dig into the code to do this. The built-in facets, though, are good enough to meet the needs of many Drupal sites, so take the time to get them set up!
This work is licensed under a Creative Commons Attribution 3.0 Unported License