Drupal 7: Faceted Searching of Content

David G - DrupalRecently a fellow developer asked me about making a Drupal search form behave similar to a Faceted Search interface. After sitting down with the developer and going over how to attempt to alter their search form to show the desired behavior; I essentially came to the conclusion that what they wanted was not the default views filtering for a page, but rather a faceted search approach to searching content. In this blog post I’ll describe their problem and at least 1 possible solution.

The Problem: Filtering into News Content (or any content)

The client would like to be able to drill down through News items by category, author or related persons’ name, or Year the content was created. Below is an example screenshot of a live site with a representative filter for News postings:

Sample News listing with a basic search filter.

Sample News listing with a basic search filter.

In the above image this is a Drupal Views implementation of a News listing with 3 exposed filters. The client in question was also using a Contextual Filter in drupal to allow linking of specific news listings per URL, and if the news category was found in the URL then the page title was to change from News to whatever the category contextual filter was set to. Accomplishing this Views title override was a nice bit of contextual filter code in the website:

/* This code goes in Contextual Filter: Default Value from PHP */
#$view = views_get_current_view();
// By default show International News.
$key = 'itn';
$title = 'News';
if (isset ($view->exposed_input['news_type'])) {
   $key = $view->exposed_input['news_type'];
   $field_name = 'field_news_type';
   $my_field = field_info_field($field_name);
   $allowed_values = list_allowed_values($my_field);
   $title = $allowed_values[$key];
}
$argument->validated_title = $title;
return($key);

But, I digress a little. The problem we were facing was that with Contextual Filters and the Views filters. The View filtering when submitted doesn’t really behave the way we want. If we supply a contextual filter and 1 additional filter such as Year the contents of the search results assume that the Name field is a specific default value. If we apply no contextual filter and a Year a default contextual filter is applied. The ultimate problem is that Views filtering doesnt provide the built-upon filtering mechanism we get from Faceted searching. When a Views filter form is submitted all fields are given a value. So it’s hardly incremental.

There’s a small chance our View, or usage of contextual filters and a default values was mucked up. But i’ve seen this before on other sites where a client describes of the 10 Views Search filters on the form they want successive filters to widdle down the search results and I find no combination of ORs or ANDs of search filters achieve the desired results.

So whats the solution? Enter Faceted Searching …

The Solution: Drupal Faceted API based Searching

So after a heck of alot of confusion I realized eventually what the client wants is a form of Faceted Searching. We see this type of searching alot in websites such as Ebay.com or Amazon.com.

Example of searching Amazon.com for shampoo term.

Example of searching Amazon.com for shampoo term.

Example of refining my search within a particular facet of the current search results at Amazon.com.

Example of refining my search within a particular facet of the current search results at Amazon.com.

So as we can see above if I search for shampoo at Amazon.com I get some results, on the left sidebar of the page are additional categories I can search within to refine my search further within the current search scope. In this case I searched for Baby and Child Care Products under the Health and Personal Care category.

Similar functionality is available in Drupal using the Facet API module for Drupal 7 (and there is currently a Beta for Drupal 8). From the Facet API project page:

The Facet API module allows site builders to easily create and manage faceted search interfaces. In addition to the UI components that come out of the box, themers and module developers can build their own widgets that can optionally be contributed back to Drupal.org. Facet API works with the core Search, Search API, and Apache Solr Search Integration modules (including Acquia Search) meaning that code and configuration can be reused as-is with the most popular search solutions available to Drupal. Refer to the documentation for more details.

So, this module works in conjunction with existing search engine solutions available in Drupal. To clarify, when you use Faceting searching you (may) need to install a more complete full-text search engine onto drupal as the default core search mechanism is somewhat limited. The good thing is that through using the Search API module you can use your existing SQL database to store custom search indexes and assuming you have a small site this should be ok. If you have a huge site with lots of content you will want to offload this searching and indexing outside of Drupal to a dedicated search engine such as Apache Solr.

So a small recipe I found and installed into a drupal instance to test out a majority of this functionality can be installed by you in a test environment by running this Drush makefile:

; This file was auto-generated by drush make
; via the command: drush make-generate in Drupal_Root.
core = 7.x
api = 2
projects[drupal][version] = "7.41"

; Modules
projects[ctools][version] = "1.9"
projects[facetapi][version] = "1.5"
projects[devel][version] = "1.5"
projects[entity][version] = "1.6"
projects[facetapi_pretty_paths][version] = "1.4"
projects[module_filter][version] = "2.0"
projects[pathauto][version] = "1.3"
projects[search_api][version] = "1.16"
projects[search_api_autocomplete][version] = "1.4"
projects[search_api_db][version] = "1.5"
projects[token][version] = "1.6"
projects[views][version] = "3.13"

After installing and enabling alllll the above modules (woof! I know right). We have the ability to create custom Search Engines, which maintain custom search indexes for any content types in Drupal. Per search index we can define the Facets to be available in that search index. If we create a View from our Search Engine, when we visit the View page URL the page will automatically include the Drupal blocks for faceted searching.

Here are some screenshots for configuring:

  1. A custom content type called Courses with a title, description, quarter, major and instructor fields.
  2. A search Engine for the content type called Courses.
  3. A search Index for the engine in (2) with defined fields for searching, with configured widgets for faceted searching.
  4. A Drupal view to show the search page and facets.

Whew!

Here is a screenshot of an example Course content type:

Example course content type with fields we want to show.

Example course content type with fields we want to show.

Then we head over to Admin -> Configure -> Search API and create a search engine and a search index:

Example of Search Engine creation and configuration for Courses.

Example of Search Engine creation and configuration for Courses.

Example search index creation and configuration for the Courses engine.

Example search index creation and configuration for the Courses engine.

After creating these resource services we can configure what fields the Index contains, and how the facet widgets should appear on a page:

Example field configuration for our search index.

Example field configuration for our search index.

Example facets available to our search index.

Example facets available to our search index.

By check marking the Facets to enable for our configured fields the Search API module creates block(s) we can place on View pages to show our facets. So we create a view with a path of /courses and assure the blocks are visible on that page. Then we can try out our View page.

Example Views configuration for a Course search page (this searches the Search Engine, not the course node type).

Example Views configuration for a Course search page (this searches the Search Engine, not the course node type).

Default Course page with no filtering.

Default Course page with no filtering.

Example course search page with 1 level of filtering.

Example course search page with 1 level of filtering.

Example course search page with 2 levels of searching within the search context.

Example course search page with 2 levels of searching within the search context.

This is only scratching the surface of configuring faceted searching on a site. You can do autocomplete searching for fields within the index. I am not 100% sure a hybrid approach of autocomplete searching and faceting searching is doable without great effort in Drupal. But, if you find an elegant solution to searching in Drupal please let me know 😉

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

 

Tags: , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *