openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Clicking carousels title link, e.g. "Trending", on Home Page results in confusing populated query

Open CipherZ3r0 opened this issue 3 months ago • 6 comments

Problem

When navigating to Trending Books on OpenLibrary, the internal query used to generate trending results is exposed in the search input.

Additionally, -subject:"content_warning:cover" appears twice in the generated query. The query can also reappear after clearing the search input and changing the sort/filter (e.g. from Trending to Random or First Published).

This makes the search input confusing for users and may indicate that the internal trending query is being incorrectly persisted or merged into the user's search query.

Image

Reproducing the bug

  1. Go to https://openlibrary.org/
  2. Click Trending Books.
  3. Observe that you are redirected to a search URL similar to:
  • https://openlibrary.org/search?q=trending_score_hourly_sum%3A%5B1+TO+%2A%5D+readinglog_count%3A%5B4+TO+%2A%5D+language%3Aeng+-subject%3A%22content_warning%3Acover%22+-subject%3A%22content_warning%3Acover%22&sort=trending
  1. Observe the search input. It contains:
  • trending_score_hourly_sum:[1 TO *] readinglog_count:[4 TO *] language:eng -subject:"content_warning:cover" -subject:"content_warning:cover"
  1. Clear the query from the search input.
  2. Change the sort/filter from Trending to Random or First Published.
  3. Observe that the following query appears again in the search input:
  • trending_score_hourly_sum:[1 TO *] readinglog_count:[4 TO *] language:eng -subject:"content_warning:cover" -subject:"content_warning:cover" Changing the sort/filter again can cause the same internal query to persist/reappear.
  • Expected behavior:
  • Actual behavior:

Context

  • Browser (Chrome, Safari, Firefox, etc): Brave
  • OS (Windows, Mac, etc): Fedora
  • Logged in (Y/N): Y
  • Environment (prod, dev, local): prod

Breakdown

Implementation Details (for maintainers)

Requirements Checklist

  • [ ]

Related files

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

CipherZ3r0 avatar Aug 29 '26 21:08 CipherZ3r0

Thank you @CipherZ3r0 for submitting this issue!

Contributors, this issue will be ready to work on once:

  • [ ] Triage — maintainers will add Priority: * and Lead: @*
  • [ ] Needs: Staff Decision — should curated/trending carousel links (Trending, Classic Books, Romance, Kids, Thrillers, subject pages) continue to expose their raw internal Solr query syntax as the visible q param/search box value, or should these link to a friendlier presentation? See open question below.
  • [ ] Needs: Breakdown — issue lacks a concrete implementation checklist/related files/stakeholders (template sections left as placeholders)

Context

  • The duplicated -subject:"content_warning:cover" filter traces to an inconsistency between two places that append this "safe mode" filter:
    • openlibrary/macros/RawQueryCarousel.html lines 14-15 (https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/macros/RawQueryCarousel.html#L14-L15) appends the filter to query unconditionally whenever safe_mode is true.
    • openlibrary/plugins/openlibrary/partials.py lines 565-568 (https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/plugins/openlibrary/partials.py#L565-L568) (gather_lazy_carousel_data_async) appends the same filter only if it is not already present (if safe_mode and _SAFE_MODE_FILTER not in query).
    • The lazy-carousel flow triggers both: LazyCarouselPartial.generate_async (partials.py lines 487-513: https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/plugins/openlibrary/partials.py#L487-L513) receives params.query, which was already built with one copy of the filter when the page first rendered (RawQueryCarousel.html: https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/macros/RawQueryCarousel.html -> data-config). It then re-renders RawQueryCarousel with that same (already-filtered) query, which appends the filter a second time — producing the q used to build the carousels "view all" /search?q=... link (RawQueryCarousel.htmls url variable).
    • The Trending Books carousel is configured in openlibrary/templates/home/index.html line 22 (https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/templates/home/index.html#L22): query=trending_score_hourly_sum:[1 TO *] readinglog_count:[4 TO *] with user_lang_only=True (adds language:eng), matching the query reported in the issue almost exactly once the safe-mode filter is (correctly, singly) appended.
  • The raw query showing up in the visible search input is expected given the current design: openlibrary/templates/search/searchbox.html (https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/templates/search/searchbox.html) renders value="$q" directly from whatever q the page was loaded with — there is no separate "friendly label" for curated/internal queries.
  • The "reappears after changing sort" behavior is separate from the duplication bug: openlibrary/plugins/openlibrary/js/sort_options.js (https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/plugins/openlibrary/js/sort_options.js) navigates to a server-built changequery URL (built in openlibrary/plugins/openlibrary/code.py line 936: https://github.com/internetarchive/openlibrary/blob/aced0c079fad187fde16b2ca1b3ae1bcb5a9a864/openlibrary/plugins/openlibrary/code.py#L936) that preserves the existing q from the current pages URL, regardless of unsubmitted edits in the input box. Since clearing the box client-side does not change the URL until the form is submitted, changing sort re-loads the original q (the raw trending query) and it reappears in the box — this is the existing changequery pattern working as designed, not a separate bug in itself, but it compounds the confusion from the raw query being shown at all.
  • No open PR or prior issue was found addressing either symptom (gh issue list/gh pr list search for "trending search query", "content_warning cover", and files touching RawQueryCarousel.html came up empty).

Issue Assessment

Open questions

  • Should curated/carousel-driven searches (Trending, Classic Books, Romance, Kids, Thrillers, and subject-page "browse" links) continue to route through /search?q=, exposing internal Solr syntax in the visible search box — or should these use a mechanism that does not put the raw query into the user-facing search input at all? This affects the scope of the fix: a minimal fix only removes the duplicate filter; a fuller fix addresses the "confusing search input" complaint directly.

Action items

  • [x] What causes the duplicate filter? (Answer: RawQueryCarousel.htmls safe-mode append is not idempotent, unlike the equivalent check already present in gather_lazy_carousel_data_async — see Context above.)
  • [x] Is there a related/duplicate issue or PR? (Answer: none found via issue/PR search.)
  • [ ] Confirm with a maintainer whether the "reappears on sort change" behavior should be addressed as part of this issue (architectural: changequery preserving q) or is out of scope / working as intended.

Suggested breakdown

  • [ ] Make the safe-mode filter append in RawQueryCarousel.html idempotent (check for _SAFE_MODE_FILTER presence before appending), matching the guard already in partials.pys gather_lazy_carousel_data_async — this alone resolves the literal "duplicated" symptom and is narrowly scoped.
  • [ ] Separately decide/design (pending staff decision above) whether curated carousel "view all" links should populate the visible search box with the raw internal query at all.

🔔 Stakeholders @cdrini — listed lead for solr, search, and frontend performance, which covers both the Solr query-building code and the search UI involved here.

Full triage checklist
  • [x] Triage — labels applied: Theme: Search, Module: Solr, Affects: UI, Needs: Staff Decision
  • [x] Problem / opportunity — problem is clear: internal Solr query exposed and duplicated in the search box
  • [x] Scope — well-scoped to the trending/carousel search-query display, though it bundles two related but distinct symptoms
  • [ ] Justification — impact stated qualitatively ("confusing") but no measurable/quantified impact given
  • [ ] Success & testing criteria — no explicit "done" definition provided in the issue
  • [ ] Proposal & breakdown — issues own checklist/related files/stakeholders sections are empty (unfilled template placeholders)
  • [x] If bug: environment & repro steps — clear repro steps, browser/OS/environment provided, screenshot included
  • [ ] Risks, concerns, open questions — not surfaced by the author; added above
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
  • [x] References — screenshot included; permalinks added above

Note: This comment was automatically generated by Richy (https://github.com/internetarchive/openlibrary/blob/master/.github/workflows/issue_enrichment.md), Open Librarys issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Aug 29 '26 21:08 openlibrary-bot

Hi @maintainers! I am a new contributor and would like to work on this issue. Could you please assign it to me?

abhipriya-anshu avatar Aug 30 '26 13:08 abhipriya-anshu

@abhipriya-anshu try providing approach and what's making this behavior occur that will make it easy for maintainers to decide what to do and if they make sense of your approach they will assign you.

CipherZ3r0 avatar Aug 30 '26 14:08 CipherZ3r0

Hi @CipherZ3r0 ,

I investigated the issue and traced the duplicated safe-mode filter through the lazy-carousel flow.

The Trending carousel starts with the query:

trending_score_hourly_sum:[1 TO *] readinglog_count:[4 TO *]

RawQueryCarousel.html appends the safe-mode filter when safe_mode=True. That filtered query is then passed through the lazy-carousel request to LazyCarouselPartial.generate_async().

gather_lazy_carousel_data_async() already avoids adding the filter when it is present, but generate_async() passes the already-filtered params.query back to RawQueryCarousel, where the filter is appended again unconditionally. This is what produces the duplicated -subject:"content_warning\:cover" in the generated /search?q=... URL.

My proposed fix is to make the safe-mode append in RawQueryCarousel.html idempotent, matching the existing check in gather_lazy_carousel_data_async():

if safe_mode and filter not in query:
    append filter

This is a small, localized change and should prevent the duplicate filter both in the lazy-carousel flow and for any other caller that passes an already-filtered query with safe_mode=True.

I would also add regression coverage for an already-filtered query / lazy-carousel rendering to ensure the filter is only present once.

For the raw Solr query appearing in the search box, I found that this is a separate architectural/UX concern: curated carousel queries are currently passed as the normal q parameter, and the search page renders q directly into the search input. Changing that safely would require deciding how curated/internal queries should be represented separately from the user's visible search query. I would therefore leave that behavior unchanged unless you would like it addressed as part of this issue.

Would this approach be appropriate for me to implement? Let me know if anything, and I'd like to get it assigned.

theia13 avatar Sep 01 '26 07:09 theia13

Heyy @theia13 , I looked into your approach and it looks good to me 👍 what you said checks out.

One small thing I'd recommend looking at is centralizing the safe-mode filter application, if possible, so we don't have the filtering logic duplicated between Python and the template. But otherwise the idempotent check makes sense and should fix the duplicate.

I also found that the raw Trending query showing up in the search box is a separate issue caused by the internal query being passed through q, so I'd keep that separate from this particular fix.

Overall looks good to me, wait for confirmation from @cdrini on the preferred scope/approach.

CipherZ3r0 avatar Sep 06 '26 11:09 CipherZ3r0

Moving to @lokesh who is working on homepage browse the stacks alternative that helps resolves this

mekarpeles avatar Oct 05 '26 22:10 mekarpeles