Show selected search facets even when there are no results
Describe the problem that you'd like solved
When there are no results we should still show the search facets. That way we can remove the facets easily to see results again. The alternative is just clicking the back button but I think that is less ideal.
Handy facets
https://openlibrary.org/search?q=%22a%22&mode=everything&first_publish_year=2023&publisher_facet=Norton+%26+Company%2C+Incorporated%2C+W.+W.&sort=readinglog
Missing facets :(
https://openlibrary.org/search?q=%22a%22&mode=everything&first_publish_year=2023&publisher_facet=Norton+%26+Company%2C+Incorporated%2C+W.+W.&sort=readinglog&language=dut
Proposal & Constraints
Additional context
Stakeholders
I did a little digging to see if I would be able to do this, but there are some things I'm not sure about. Currently, the facets rely on the search results to get the display names, so if there are no search results then the facets won't appear either.
https://github.com/internetarchive/openlibrary/blob/4043d671afe60105063fdcd12f59ffe36b087cbc/openlibrary/templates/work_search.html#L103-L105
If we rewrite it to rely on the search parameters instead (something like below) we can get the facets to work again, but I don't know how we would get the display names.
$for header, labels in facet_map:
$if header in param:
$for k in param[header]:
$ display = k
@raffchen thanks for starting to look into this! I'm not so sure what the best approach is here. Do you want to try looking into changing the response to include facets even when there are no results?
@cdrini any gut reactions on which approach is worth exploring more?
Removing good first issue label because this is a bit more difficult than originally anticipated. Thanks for the initial investigation @raffchen . Might require some Solr docs digging about returning some info when there are no results.
Here is potentially the thing we might want to do? But more investigation needed: https://solr.apache.org/guide/solr/latest/query-guide/faceting.html#tagging-and-excluding-filters
Had a look at this one on production today, and I think most of it has quietly been fixed since 2024.
Selected facets do now survive a zero-result search. Searching quantum entanglement filtered to person_facet=Napoleon returns nothing, but the filter still renders as a chip with a clear link back to the unfiltered query, so there's no dead end. Language is handled separately — it's in special_handled, surfaced by the language dropper rather than as a chip — and that works too: the same search filtered to Dutch shows the dropper labelled "Dutch" with a "Clear selections" option, no back button needed.
What's still missing is the facet sidebar itself, which is gated on len(search_response.docs) at work_search.html:172 and so disappears entirely when there are no results. That's a different thing from the issue title, though: it's about browsing other values rather than seeing and clearing the ones you picked.
Worth noting the data for it exists now. #13064's /search/facets.json returns full counts once the filter on the field being counted is stripped — ?field=language&q=quantum+entanglement gives ten languages with counts, while adding &language=dut gives back {"language":[]}.
So: is this closeable as done, or is the sidebar the part you still want? Happy to take that on if it would help.
One thing I turned up while looking at this, in case it is the same theme: Advanced Search drops the current query and any selected filters entirely, unlike the other five tabs which carry q forward. Filed separately as #13488.
@pnelson1337 good call, this is definitely fixed enough to call it done!