I come from this issue #1415360: Display inactive facets and searching for a solution to display inactive facets. I don't know if it is a issue for search_api, search_api_db or facetapi. It would be nice if someone can clarify me where to look deeper in this.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

checker’s picture

Title: Display inactive facets » Make avaiable facets with counts of 0
Category: support » feature

OK as I see the problem could be that search_api_db can't handle "Minimum facet count to 0". Detailed information in #1669600: How to display inactive items of the origin query without filters.

Can anyone confirm that this feature is not possible with seach_api_db or how to hack it?
Thank you!

figureone’s picture

Title: Make avaiable facets with counts of 0 » Make available: facets with counts of 0
Issue summary: View changes

I'd also like to see this feature. Apparently the solr backend db supports this.

Example: I have a facet showing content types. I would like for all content types to show in the facet, even if the current search has 0 results for a particular content type (or if a particular content type has no content created yet).

In other words, say I have 4 content types in my site: Basic page, Blog entry, Poll, and Survey. I'd like to see this facet in search results:
Filter by Content type:
* Basic page (12)
* Blog entry (0)
* Poll (2)
* Survey (0)

As it stands, the (0) items are being filtered out, like so:
Filter by Content type:
* Basic page (12)
* Poll (2)

Further, if we narrow down to one of these content types, I'd still like to see the others:
Filter by Content type:
(-) Basic page (12)
* Blog entry (0)
* Poll (2)
* Survey (0)

As it stands, it looks like this:
Filter by Content type:
(-) Basic page (12)

figureone’s picture

Status: Active » Needs review
FileSize
1.45 KB

The attached patch enables this feature. For each facet, it performs another database query to get all possible values. It then iterates through the facet values, and adds in the missing facets, with a count set to 0 (i.e., no results for that facet).

Note: 'Minimum facet count' must be set to 0 in the display options for a particular facet for this patch to have any effect.

Note: The attached patch is rolled against 7.x-1.x-dev, and will fail to apply to 7.x-1.2 (line numbers are different). You can manually apply the patch, though.

Module maintainers, please review. and incorporate into the next release.

rosinegrean’s picture

Hello,

I've tested this patch it solves the problem.

vintorg’s picture

I applied this patch, and it works.

drunken monkey’s picture

@ figureone: Thanks a lot for the patch, seems like a very simple solution! (Please mind the Drupal coding standards, though, especially regarding the spaces in function calls.)
I first thought about doing this with a clever RIGHT OUTER JOIN, but I guess that wouldn't work properly with the COUNT(), and also we are already using left joins for the "missing" facets and Drupal doesn't seem to support full joins for some reasons.
And since the performance impact of having another database query per facet (and also only a very simple one) will only apply to those few who actually want no-result facets to display, I guess I'm fine with this approach.

There were a few slight improvements and style changes I wanted to make, though, so attached is a new patch containing those (and also a test for the new functionality).
Please see if that patch still works for you and I'll commit it. Thanks again!

Further, if we narrow down to one of these content types, I'd still like to see the others:

This is an entirely separate request, for which you just need to set the facet operator to "OR". (Though that still needs #1390586: Add support for OR facets to work.)

figureone’s picture

@drunken monkey, Looks good. The new patch works fine for me.

Please mind the Drupal coding standards, though, especially regarding the spaces in function calls.

I sometimes switch back into WordPress mode. ;)
Thanks for cleaning that up.

I also started looking for a solution involving modifying the original db query with another join, but ran into the same problems. Ultimately I think the current patch is the most elegant.

This is an entirely separate request, for which you just need to set the facet operator to "OR". (Though that still needs #1390586: Add support for OR facets to work.)

My current solution is to override theme_facetapi_link_inactive() as mentioned in #1669600: How to display inactive items of the origin query without filters. It's a workable solution, though I suppose the main complaint is that the facet counts are wrong (they are all set to 0 because of the AND operator). I'll keep an eye on the OR issue you linked to. Thanks!

drunken monkey’s picture

Category: Feature request » Bug report
Status: Needs review » Fixed

OK, great to hear! Committed.
Thanks again for your contribution!

vintorg’s picture

My inactive facets don't display anymore after this patch. Investigating...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

philipz’s picture

This works great. Thanks for the patch!

aniket.mohite88’s picture

Thanks. This helped.

I was facing the same issue. I did download the 1st patch & found that some code was already present in my module, whose current version is 7.x-1.4.

Then Setting the "Minimum facet count" to 0 in the facet display configuration will show all the facets, even the ones that aren't matched in the search results.

I was looking for a way to show all filters irrespective of the results set. Its nice that this code was already in place in this version 7.x-1.4.