Creating a Faceted Search View in Drupal

Drupal Version:
7

INTRODUCTION

Faceted Searching is a method that allows a user to apply multiple filters of varying dimensions to a list of items on your website. This is a great method for quickly filtering a long list of information down to narrower results that are relevant to what you’re searching for.

For example, on Amazon.com you may filter your product lists by “department”, “rating” or “format” or on Zappos.com you may filter by “shoe size”, “shoe width” and “shoe color” to narrow the list of shoes. Faceted searches are a common and effective method for simplifying the searching of items on your website. But how do you achieve this in Drupal? In this tutorial we’ll show you how to create faceted searches that you can use on your Article listing page created using a Drupal View. This method can also be used for Drupal Commerce product pages and almost any other type of data you may be displaying on your site.

Drupal Search API Module Exmplae

VIDEO AVAILABLE: Please note that there is a video version of this tutorial available at the end of this page.

MODULE SETUP

Install the following modules:

Enable the following modules:

  • Current Search Blocks
  • Database Search
  • Search APi
  • Search Facets
  • Search Views

Drupal Search API Module Settings

NOTE: The Search API system that we’ll be using does not require Drupal’s core Search module. If you’re not using a site-wide system search you can safely disable this module.

Dependencies:

The above modules have the following required modules, if there are not already then they will also need to be installed.

NOTE: For this tutorial we’re going to use Drupal’s built-in Article content type. However, you can use any Drupal entity that can be displayed in a View to create your search facets. You can also chain referenced entities so, for example, you can show Drupal Commerce "Display Nodes" while pulling information from the product entities attached to them into your search facets.

CONFIGURATION

SERVER SETUP

The first thing we’ll need to do is create the ‘Server’ that will host your search index that we’ll be using.

  1. Navigate to CONFIGURATION->SEARCH AND META DATA->SEARCH API (YOURSITE/admin/config/search/search_api)
  2. Click the “Add Server” link.
  3. Enter a name and description for your server for your reference. For example, “Search API Server”
  4. From the “Service Class” drop-down, select the “Database Server” option.
  5. Click the “Create Server” button.

Drupal Search API Create DB Server

INDEX SETUP

The next step is to create the index that will hold your search index information. This index will reside within the server object created in the previous step.

  1. Navigate back to to CONFIGURATION->SEARCH AND META DATA->SEARCH API (YOURSITE/admin/config/search/search_api)
  2. Click the “Add Index” link.
  3. Give your index a name, for example (“Article Index”)
  4. In the “Item Type” dropdown, select “Node”.
  5. In the “Server” dropdown, select the server you created in the previous step.
  6. If your index is going to be large then do not select “Index items immediately”. Since our site will not be very large we will check this option so that node changes are updated in the index right away instead of having to wait for the next cron run.
  7. Click the “Create Index” button.

Drupal Search API Index Settings

FIELD SETUP

The next step is to select the fields that will be indexed. This is a basic and arbitrary list for this example but should give you an idea of how the field selection works to get you started. First, under the “Add Related Fields” section near the bottom of the page, add the “Author” and “Main Body Text” to our index. This will give us access to ‘related’ fields to use in our index. After adding these two related-fields you’ll see more fields available to add to your index.

For this example, we’re going to select the following fields:

  1. Title – Even though this isn’t going to be a search facet, we may want to use this for text searches.
  2. Author > Name
    • IMPORTANT: Change the ‘type’ from ‘Full Text’ to ‘String’. This will index the all words of this field as one string instead of individual words.
  3. Tags
  4. The main body text » Text – Even though this isn’t going to be a search facet, we may want to use this for text searches.
  5. Click the “Save Changes” button.

Drupal Search API Field Settings

NOTE: You can adjust the ‘boost’ level as need to increase or decrease the importance the index will give to that field.

FILTERS SETUP

Now we need to set up the fields for our index. You should already be on the “Filters” tab for your index.

  1. Click the “Bundle filter” checkbox so we can limit our index to the Article content type.
  2. Click the “Exclude unpublished nodes” checkbox if you do not want your unpublished nodes to be indexed.
  3. At the button of the form you should now see of list of “Bundles” (exposed by checking the “Bundle Filter” option). Select the “Article” bundle and select the option to ‘only [index] those from the selected bundles’
  4. Click the “Save Configuration” button.

NOTE: For this example we’ve created a few fake Article nodes and populated them some titles, body text, taxonomy tags and authors (Users).

Drupal Search API Filter Settings

FACET SETUP

Now click the “Facets” tab in your Search Index and select the following fields to create facets out of:

  1. Tags
  2. Author > Name

Drupal Search API Facet Settings

NOTE: If your Index is set to "Index Items Immediately" then your Entities will be added to your Index as your create or update them. If this setting is not enabled then your Index will be updated on each Cron run. Either way though, if you make changes to your Search Index settings you will need to rebuild your Index after making the changes. You do this by clicking the "Index Now" button on your Index's View tab.

Drupal Search API Index Now

FACET BLOCKS SETUP

For each Facet created (above) there will be a corresponding Drupal Block available. We'll want to configure these block to display on your search page.

  1. Navigate to the Block configuration page (admin/structure/block)
  2. Find your Search API Blocks (there will be one block for each facet you created earlier and the Block names will start with 'Facet API')
  3. Assign the blocks to the region that you want them to display one. This is usually either Right Sidebar, Left Sidebar, First Sidebar or Second Sidebar.
  4. NOTE: You do not need to assign visibility to the Blocks. They will automatically be displayed only when you ware on a Search API Views page

OPTIONAL: Although optional, you'll most likely want to also enable the "Current search: Standard" to display the result count and parameters of the current search. To do so, simply find the block title "Current search: Standard" and assign it a region. This block usually works best above the main content so the "Content" region with a low weight value should work well.

Drupal Search API Block Settings

VIEW SETUP

Now we'll create a View to display our indexed items and search facets.

  1. Add a new View (admin/structure/views/add)
    • IMPORTANT: Be sure to select the search index name that you created earlier. In this example it is called “Article Index”.
  2. You can create your View however you’d like it to display. In this example, we’ve created our View to display using a Field Display type and the fields we’re using are Node Title, Author Name and Node Tags (taxonomy).
  3. Optional – If you’d like to create a search box for your page add the filter called “Search: Fulltext search”, Expose it and select the text fields to search on. In this example we’re going to allow search on the Title and Body fields so select those two fields.
  4. Save your View once it is setup how you’d like it.

Drupal Search API View Setup

The Magic

Now, when you navigate to your new, Search Index View you'll see the Search Facets appear beside your View. These Facets will automatically display the Facet Fields that you configured and will only display the fields that are contained in your current View.They will also display the Facet count (number of each field contained in your View) and, the best part, they can be combined to produce a very specific list of results!

Drupal Search API View Output

CONCLUSION

This was a very basic example to get you started but, with this very powerful system, you can create complex Search Facets to filter lists using ranges, multiple taxonomy terms, field values, referenced Entities, and much more.

There are additional Facet options available through the Facet's Block configuration options, including, controlling how many items to display, the display type (Check boxes instead of links) and much more. Be sure to explore these options.

NOTE: The Search API module has many Extension Modules available to add additional functionality. A popular Extension Module is the Search API Ranges Module which allow creating slider facets for a high/low range. For example, creating a facet between $100 and $500.

Follow us for more Drupal tips and articles

VIDEO VERSION OF THIS TOPIC AVAILABLE