Search facets and filtering are a great way to guide your user to the information they are looking for. With facets, we can create an intuitive way for users to narrow down the items that are visible on the screen. Drupal’s core search capability does not provide a way to implement facets, so we need to look at contributed modules to achieve this. In Drupal 6, we would reach for the Apache Solr Search module. This is a great, high performance solution, but it has it’s drawbacks. Installing Solr on your web server can be tricky, and with most shared hosting accounts not even permitted. Fortunately, with Drupal 7 and a few API modules, we can incorporate these wonderful little facets without the need for a full-blown Solr setup.
As an example, we are going to walk through creating a photo gallery using Views. We will then use the powerful combination of the Search API, Entity API and Facet API to allow users to filter the photos using 2 facet blocks.
Create the content type and test data
First, let’s start by creating our content type, called Photo, with the following fields:
- Title
- Photo – Image
- Location – List (text)
- Type – List (text)
Make sure to populate your Location and Type lists with some values. Next, we need to generate test data using Devel Generate content. I created 50 Photo nodes, but feel free to create a different amount if you prefer.
Download the API modules
Download the Search API, Entity API, Facet API and Database search modules to your modules directory. Now, visit the Modules admin page (/admin/modules) and enable the following modules:
- Search API
- Search facets
- Search views
- Entity API
- Facet API
- Database search
Configure the search server
Let’s go to the Configuration admin page (/admin/config) and click on the Search API link. You should now be on the Search API configuration page:
Click on the Add server link and you will see this form:
Choose a name for your server (I chose MySQL) and select Database service for the Service class field. Click the Create server button, and you will see the following:
Configure the search index
Now, click back to the Search API config page, and click the Add index link. You will see the following:
Use the following values:
- Index name: GalleryIndex
- Item type: Node
- Server: MySQL
- Index items immediately: checked
Now, click the Create index button. We are now faced with a rather large form that will allow us to select which fields we would like to index and make available to Views. We are going to check the following:
- Content type
- Location
- Type
Now, click the Save configuration button.
We now need to select the indexed items that we would like to have facets for. Click on the Facets tab and you will see the indexed items we chose. It is recommended for performance reasons to only choose the facets that are needed, so let’s select Location and Type. Click on the Save configuration button when you are done.
Don’t forget to run Cron!
We must run cron at this point to populate the search index.
Create the view
Let’s go to the Structure » Views page and click on Add new view link. Use the following values:
- View name: Gallery
- Show: GalleryIndex (this corresponds to the name of the search index we created earlier)
- Create a page: checked
- Path: gallery
- Display format: Grid
Now, click the Continue & edit button.
Tweak the view
The View setup is pretty straight ahead. We need to add a filter criteria for the Photo Content type. Notice that it is labeled Indexed Node: Content type. Once we do that, we just need to remove the existing Node ID field and add the Indexed Node: Photo field. Now, save your view.
Place the Facet blocks
Go to the Structure » Blocks page (/admin/structure/block) and you should see the facet blocks for Location and Type in the listing. All we need to do is put them in the region in which we want them to appear. In my case I selected Sidebar Second.
Success!
Now, visit the Gallery page (/gallery) and you should see something similar to this:







April 5th, 2012 at 9:32 am
Much appreciated, been just looking how to do it with taxonomies, but since they are fields in D7 the same applies as what you described.
Life saver article.
April 12th, 2012 at 8:54 pm
This was really helpful. Thanks a million for sharing this. I was really struggling with the Faceted Filter in Views and your post saved me time and efforts. Thanks again for such easy-to-follow and to-the-point post. Thumbs Up!
May 14th, 2012 at 8:58 pm
This is a valuable resource of high quality. Thank you for providing it. I doubt that I could have worked it out for myself (or if I could have it would have taken some long time). I used to use Faceted Search, but it hasn’t been ported to Drupal 7 and, apparently, to would not scale. This is an avenue to different but similar things. Thank you once again.
Martin
July 17th, 2012 at 4:01 am
Database search was exactly what I missed in Search API implementation. Thanks a lot , now I have breakthrough!
July 30th, 2012 at 9:52 am
Thanks for taking the time to put this up! I appreciate it. I looked forever for “checkbox search filter” come to find out, it’s called Faceted Search LOL.
August 14th, 2012 at 10:10 am
Very useful, thank!
August 24th, 2012 at 9:31 am
Hi, has anyone else had trouble with finding the index in the show dropdown box? I’ve created my search index as documented above but it does not display in the “Show” dropdown box when I try to “Add new view” , the only index that shows up there is “Search Index” and then I get the error “An illegal choice has been detected”, however I would like to choose my GalleryIndex, but it doesn’t show up in the dropdown box as documented. I’m curious because others here say they had this work, it could possibly be one of the contributed modules I have enabled that is preventing it from working.
August 24th, 2012 at 11:09 am
I retested and got it to work by re-installing Drupal from a vanilla flavour and applying all my modules to it.
There is a bug somewhere in the other distro I am using, will contact them, I will follow up when I find out what it is.
August 27th, 2012 at 1:27 pm
while I did get this working following the above tutorial, I did notice an issue with the date field. If the default configuration is chosen for date then there’s an SQL error. To get around this I was able to select “integer” from the drop down box when configuring the date field in the index. However, instead of facetting nicely formatted dates, I get integers. While I can find a way around this, I wonder if it’s my configuration causing the problem or if it’s a wierdity of these facet search modules. I also had problems with a development module for a CCK Field for money does not get detected automatically, I imagine a hook somewhere needs to be adjusted so that I can display money on the view for the index.
November 15th, 2012 at 3:28 pm
Joseph Olstad,
I got the same trouble with finding the index in the show dropdown box,
The solution is to enable 1 more module!!!
Search views, it comes with search api module.
good luck.
December 18th, 2012 at 4:26 pm
Mate, thanks a million for this. I’d been trying to figure it out for days – works a dream.
December 21st, 2012 at 1:22 am
This is exactly what I needed. This is a gem thanks heaps! I have configured my site without using ApacheSolr.