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.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 2994022-7--db_backend_facet_min_count_0.patch | 7.7 KB | drunken monkey |
| #7 | 2994022-7--db_backend_facet_min_count_0--tests_only.patch | 3.07 KB | drunken monkey |
Comments
Comment #2
watergate commentedAttached a patch that resolves the problem/implements the feature request.
Comment #3
drunken monkeyDoes the
search_api_facets_alwaysoption 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.)
Comment #4
borisson_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.
Comment #5
borisson_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.
Comment #6
watergate commented@drunken monkey, the
search_api_facets_alwaysis requested/added in Display facets when there are no search results. I first tried to solve the problem by setting the already existing optionskip 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).
Comment #7
drunken monkeyHm, yes, so would I.
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.
And is that patch needed for facets to be displayed, or does it just work with this patch, too?
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)
Comment #9
borisson_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.
Comment #10
Anonymous (not verified) commentedTested patch #7, works fine
Comment #12
drunken monkeyAlright, thanks for your feedback!
Committed.
Thanks again, everyone!
Comment #14
lus commentedIs 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:
Comment #15
anybody@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.
Comment #16
anybody@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.