
Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughSet up a hierarchical taxonomy term Facet using Facet API with Search API Solr
I wanted to document this here just because it took me a little while to get all the bits working just right so I could have a hierarchical taxonomy display inside a Facet API search facet, rather than a flat display of only the taxonomy terms directly related to the nodes in the current search.
Basically, I had a search facet on a search page that allowed users to filter search results by a taxonomy term, and I wanted it to show the taxonomy's hierarchy:
To do this, you need to do two main things:
- Make sure your taxonomy field is being indexed with taxonomy hierarchy data intact.
- Set up the Facet API facet for this taxonomy term so it will display the full hierarchy.
Let's first start by making sure the taxonomy information is being indexed (refer to the image below):
- In Search API's configuration, edit the Filters for the search index you're using (e.g.
/admin/config/search/search_api/index/[index]/workflow
).- Make sure the 'Index hierarchy' checkbox is checked.
- In the 'Index hierarchy' Callback settings (which appear after you check the box in step 1), scroll down and make sure you select 'Parent terms' and 'All parent terms' under the Taxonomy type you need to display hierarchically.
- Save the Filters configuration, then reindex all the content on your site (otherwise Solr won't have the updated hierarchy information).
Next, we need to edit the Facet API facet for this taxonomy:
- Go to the taxonomy Facet's configuration page (e.g.
/admin/config/search/facetapi/search_api%40[index]/block/field_release/edit
). - Check the 'Expand hierarchy' checkbox under 'Display settings' (near the top of the form).
- Set 'Treat parent items as individual facet items' to 'No'.
- Set 'Flatten hierarchy' to 'No'.
- Set 'Minimum facet count' to 0 (to show all terms in the taxonomy).
After you've done that (make sure you reindexed your content!), you should have a nice hierarchical facet display.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web
