Problem/Motivation

This feature request is related to Display facets when there are no search results.

When for a facet the "Minimum count" value is set to 0 (or negative), all facet options are correctly shown when the search returns a result. However, when there are no results also no facets are shown. Setting the "Minimum count" to 0, suggests to me that the facet options should always be shown.

I understand the reasoning that it only makes sense to show facets when you can narrow down a search result, but in my situation visitors can end up in a situation that no results are shown and they aren't able to deselect the active facets (this occurs when using a text search).

Proposed resolution

A solution would be to set the "extra data" when this is indicated by the Facets functionality (in the case "Minimum count" is set to 0), even when there will be no results for the search query.

Comments

Watergate created an issue. See original summary.

watergate’s picture

Assigned: watergate » Unassigned
Status: Active » Needs review
StatusFileSize
new945 bytes

Attached a patch that resolves the problem/implements the feature request.

drunken monkey’s picture

Component: General code » Database backend
StatusFileSize
new4.63 KB

Does the search_api_facets_always option actually exist or did you just invent it?
In the latter case: why?
In any case, it seems this can be much easier be implemented by just moving the facets code out of the enclosing if?

Anyways, if we do that, we’ll decrease performance for queries with empty results sets even in the vast majority of cases where there won’t be any facets. So, I think we should take this opportunity to finally optimize the getFacets() code a bit.

Patch attached, please test/review!
(I completely reverted your changes, so no interdiff.)

borisson_’s picture

+++ b/modules/search_api_db/src/Plugin/search_api/backend/Database.php
@@ -2374,8 +2376,33 @@ protected function getFacets(QueryInterface $query, SelectInterface $db_query) {
+        if (isset($result_count) && $result_count < $facet['min_count']) {

I would prefer to do if ($result_count !== NULL && ...) instead.
Are we sure that this is <, and not =<? I'm not sure I am.

Otherwise the changes here look very good and I think they are an improvement. I would like to run facets tests with this patch enabled first though. Doing that now locally and I'll get back with the results.

borisson_’s picture

The tests are still green, so that's nice. I think we can get this in - depending on the result of my other comments in #4.

watergate’s picture

Issue summary: View changes
Status: Needs review » Needs work

@drunken monkey, the search_api_facets_always is requested/added in Display facets when there are no search results. I first tried to solve the problem by setting the already existing option skip result count, but that didn't work well with View's pagination. Therefor, I added an extra option to get desired behavior without having to alter existing logic.

I've tested the patch of #3, and in my setup I combine facets with free text search. When I enter some keywords in the search field that don't produce any results and than select a facet checkbox, all facet options are lost again (so, I'm also not able to deselect it).

drunken monkey’s picture

Status: Needs work » Needs review
StatusFileSize
new4.2 KB
new3.07 KB
new7.7 KB

I would prefer to do if ($result_count !== NULL &amp;&amp; ...) instead.

Hm, yes, so would I.

Are we sure that this is &lt;, and not =&lt;? I'm not sure I am.

Yes. If there is 1 result and min-count is 2, we can’t possibly have any facets. If min-count is 1, we can.

@drunken monkey, the search_api_facets_always is requested/added in Display facets when there are no search results.

And is that patch needed for facets to be displayed, or does it just work with this patch, too?

I've tested the patch of #3, and in my setup I combine facets with free text search. When I enter some keywords in the search field that don't produce any results and than select a facet checkbox, all facet options are lost again (so, I'm also not able to deselect it).

Can’t reproduce that, and don’t see why that would happen. The minimum count of 0 should be in effect both before and after selecting a facet filter.

But I guess we still need tests for this anyways, so adding those, too. (Before Joris can complain again. ;P)

borisson_’s picture

Awesome, thanks for that! This feature has been requested a couple of times in facets - and I think that this implementation makes more sense than some others I've seen/heard before.

Anonymous’s picture

Tested patch #7, works fine

  • drunken monkey committed c8bbc2e on 8.x-1.x
    Issue #2994022 by drunken monkey, Watergate, borisson_: Fixed facets...
drunken monkey’s picture

Status: Needs review » Fixed

Alright, thanks for your feedback!
Committed.
Thanks again, everyone!

Status: Fixed » Closed (fixed)

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

lus’s picture

Is it just me or it's not working ? Used with Drupal core 9.2.0, Search Api 1.20 and Facets 1.8. Settings of my facet:

  • taxonomy field
  • checkbox widget
  • operator OR
  • minimum count 0
anybody’s picture

@Lus if you were using operator "and" the further discussion in the issue might have been the reason: #2993101: Display facets when there are no search results but in your constellation, I don't have an explanation so far.

anybody’s picture

@drunken monkey, @borisson_, @Watergate, sadly after this has been committed, there was no more progress or documentation over at #2993101: Display facets when there are no search results. Please also help to get this done over there cleanly. That would be great.