Want to learn more about search in Drupal 7? Then check out our online video course called “Build Powerful Search Pages with Views and Search API in Drupal 7“.
In part one of this series, we used Search API and Views to create a custom search page. The page is fairly basic, however it gives you a great introduction into how Search API works.
In this tutorial, we’ll add faceted search to our custom search page using the Facet API. Luckily for us, Search API and Facet API work brilliantly together so setting up faceted search is pretty easy.
Getting Started
Before we begin, download and enable the Facet API module and then enable the “Search facets” sub-module that ships with Search API. “Search facets” integrates Facet API with Search API.
If you prefer Drush, run the following command:
$ drush dl facetapi $ drush en facetapi search_api_facetapi
Enable Facet
We’ll continue building of the custom search page that we created in part one of this Search API series. If you want to follow along, make sure you have read part one.
In this section, we’ll configure a facet block that will display the Tags vocabulary. This allows users to filter content by clicking on a tag.
Steps
1. Go to Configuration -> Search API and click on the edit link within the “Article index” and select Facets from the drop-down.
2. Check the Tags checkbox and click on Save configuration.
Display Facet
At this point, we have just enabled the facet. The next step is to add the facet to a specific region.
1. Go to Structure -> Blocks and place the “Facet API: Search service: Article index : Tags” into the “Sidebar second” region.
Now if you go to /search-page
, you should see a facet block on the right hand side.
If for some reason the facet block is not appearing, try re-indexing the content.
Current Search Block
The final thing we’ll look at in this tutorial is the “Current Search Blocks” sub-module which ships with Facet API. This is a hidden gem that isn’t advertised on the Facet API project page. The sub-module defines a block that displays information about the current search. It’ll make sense once you see it in action.
1. Enable the “Current Search Blocks” module.
2. Go to Structure -> Blocks and add the “Current search: Standard” block into the Highlighted region.
3. Go to the /search-page
and you should see the block above the content area.
If you want to configure the block or define your own, go to Configuration -> “Current search blocks” (admin/config/search/current_search).
As you can see this is a cool sub-module and I plan to write a detailed tutorial about it in the future.
If you have any questions, please leave a comment.
Thanks, good one, I just bookmark every tutorial about Faceted Search (since they are hard to find)
I have set the values in the same as your tutorial but cant get the facet in the sidebar first. And re-indexed the site again and cant get that. Can you suggest anything?
What do you mean by “cant get the facet in the sidebar first”?
Have you added the facet to the region via the Blocks page and are you getting any PHP errors?
Facets appear contextually depending on what is returned in the results. So it’s a good idea not to have complex search pages, start off simple.
Is there a way always to show the facet’s block content without making search?
I don’t think you can display a facet without making a search. But, after a quick google search I found this issue http://drupal.org/node/1235026
Thank you for a quick reply!
I’ve seen this issue.
I’ve solved my problem by using views. It displays fasets by default.
Ivan, nice tutorial thank you and can you advise on how to get clean URLs from these faceted searches using the facetapi_pretty_paths ? I’ve had no luck so far and the documentation appears to be minimal on the module page. I get %2f% for example in the URL and repeated fragments of words.
https://drupal.org/project/facetapi_pretty_paths
Unfortunately, I have not used the module since its 1.0 release. I’ll write a tutorial on using the module sometime in the future.
Hi …Ivan, can u show how to output the search result in table view? can it be done?
Yes you can display the search results in a table if you created the search page using Views.
Just change the “Display format” in the view to “Table”.
Thanks again for your great articles, do you know how to sort alphabetically taxonomy terms in facet filter block?
No I don’t know how to sort facets alphabetically. But try and adjust the order of the sorts in the “Configure facet display” page and see if that works.
Thanks for “Configure facet display” tip, I think I had tried that but will re-visit with a fresh pair of eyes.
An alternative approach I am considering is to write some hook code to implement Implements hook_facetapi_facet_info_alter() in my own module, based on the example here “Add the ability to sort taxonomy facets by their term weight”
https://drupal.org/node/1361284#comment-5377112
(I’ve also raised a specific issue for my need here which says the same: https://drupal.org/node/2086869 )
This is now in its own module: https://drupal.org/project/facetapi_taxonomy_sort
Re-using the code in https://drupal.org/node/1361284#comment-5377112, I would replace the taxonomy weight sorting logic with alphabetical sort.
Even better would be to make this code generic for any sort, via a PHP interface and have the actual sort algorithm in a separate php class in a separate plugins folder and use dependency injection. This would enable people to write further kinds of sorts, though at the moment I can only think of weight and alphabetical.
Thanks for the update.
Thanks for this post, it’s very useful! Especially it’s about “If for some reason the facet block is not appearing, try re-indexing the content.” tip. Not so many Drupal tutorials and docs explicitly point to possible problems and solutions, most of them just looks like:
1. Do smth and it must works.
2. Do the next step
…
etc.
So, very hard to read the official documentation. Thanks again! 🙂
Got pretty far, but when using the full text filter in the views with the facet block, the blocks disappears. Any idea what the solution can be to fix this?
Not sure what the problem could be.
Are results being returned? Facets will only appear if results are returned from the index.
Check your logs and see if you’re getting any PHP errors.
Hi Ivan, many thanks for this great article. The best tutorial for Search API I found!
Do you know how I can limit the search to only one type of content ?
Thanks again
This can be controlled from the “Workflow” section when you’re configuring the index. But if you do this then the content type won’t be added to the index.
If you want to index all content types but only show a certain few then do this using Views.
Hello Ivan,
thank you very much for that great tutorial! It made my facets finally work – yeah! May I ask you for one more hint?
Do you know a posibility to render a field with links that contain the selected facet-values? To explain that question:
My facets produce URLs like this e.g. (that means this it what I see in the browsers url-bar when cklicking a facet):
…/en/?f[0]=facet1%3A999?f[1]=facet1%3A888
f[0] reflects the first activated facet, facet1 reflects the corresponding indexed field, and 999 the selected value.
My view which nicely shows the drilled-down list of nodes renders for each node these fields: title, nid, url. They all link to the corresponding nodes.
Unfortuantely, those links are “pure” node links (node/1, node/2, node/3 etc.) That means, when clicking them, all the facet activations get lost.
What I need are links like this: domain.com/en/node/2/?f[0]=facet1%3A999?f[1]=facet1%3A888
Thank you again and nice regards
Hi Maybor,
Not sure how to implement this. Best to ask in the Facet API issue queue.