openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Book Page: Other Editions Table → Solr

Open mekarpeles opened this issue 3 years ago • 10 comments

Describe the problem that you'd like solved

Today, the "Other Editions" table is one of the most expensive parts to load of our Book Page, which is one of our more important pages; see: #6195. There are two main reasons for this:

  1. It requires we fetch lots of records from infogami
  2. It requires we fetch Bulk Book Availability info for these items

The table has other issues. Most patrons likely only want readable books in the list. But to determine this (without solr) we need to fetch more than we need from infogami. This results in issues like #7233

Proposal & Constraints

This issue proposes we:

  1. switch the Other Editions table to use Solr Editions instead of infogami
  2. update the search function so it also uses Solr (solving #7233)
  3. Get a further win by pairing this issue with #7450 (to remove the Bulk Availability check)

Stakeholders

@cdrini @mekarpeles @jimchamp

mekarpeles avatar Jan 26 '23 06:01 mekarpeles

Switching the editions page to solr may not be trivial.

Proposed path forward: /search/editions UI + API

After discussion (cc @cdrini), we'd like to propose a concrete implementation path for this issue. Rather than rewiring the existing editions table in place as a first step, we think the best approach is:

Build a new /search/editions page and /search/editions.json API endpoint powered by Solr, closely mirroring our existing search UI.

Why this approach?

Creating /search/editions.json is effectively a prerequisite for replacing the editions table anyway — we need a clean API layer over Solr edition results before we can swap out the infogami-backed table. Building it as a standalone search page first gives us:

  • A working API that the editions table can later be rewired to consume
  • A place to prototype edition management features (merge, delete) that we've been wanting
  • An incremental, non-destructive path — we build alongside the existing table rather than modifying it, reducing risk
  • A community-friendly work breakdown — the API and UI are cleanly separable across contributors

This directly addresses the core problems in this issue:

  1. Expensive infogami fetches → Solr replaces infogami as the data source
  2. Bulk Availability overhead → pairs with #7450 (Book Availability in Solr) to eliminate this entirely
  3. The 1000-edition cap (#7233) → Solr handles pagination natively, so "View all editions" works properly

Scope

Phase 1: API — /search/editions.json (FastAPI)

  • New FastAPI endpoint at /search/editions.json
  • Queries Solr's edition index with standard search parameters (q, sort, page, limit, facets, etc.)
  • Returns edition results in a JSON format consistent with our existing /search.json patterns
  • This is the easier lift and a good candidate for community and/or AI-assisted contribution

Phase 2: UI — /search/editions (web.py) using plugins/worksearch

  • New web.py route at /search/editions
  • UI closely mirrors the existing /search interface (search bar, faceted sidebar, paginated results)
  • Results display edition-specific metadata (publisher, publish date, ISBN, format, availability, etc.)
  • More complex — requires careful design work to match existing search UX patterns

Phase 3: Rewire the editions table

  • Once /search/editions.json is stable and proven, swap the "Other Editions" table on the Book Page to consume it instead of fetching from infogami
  • This is the payoff that solves the original performance problem in #6195
  • Will need to similarly support selection of editions (for future merge capability to merge editions #2114

Future: Edition management

  • The /search/editions page becomes a natural surface for prototyping bulk edition operations (merge, delete)

mekarpeles avatar Mar 31 '26 00:03 mekarpeles

@mekarpeles,I would love to work on the phase 1 FastAPI endpoint for /search/editions.json following the existing /search.json patterns. Is this open for community contributors?

bhardwajparth51 avatar Mar 31 '26 04:03 bhardwajparth51

Thank you @bhardwajparth51 , this is already being tackled in PR #12663

cdrini avatar Jul 06 '26 17:07 cdrini

Hi, I would love to work on this issue! Will submit my approach soon.

RsbhThakur avatar Aug 11 '26 17:08 RsbhThakur

Hi @RsbhThakur, thank you. This is a big task.

There's a handful of existing work that may be really helpful. For instance:

  • #12663

In this PR, we create a new search endpoint for editions (and a UI for testing it). The /search/editions.json could be very useful as a backend to power the editions table.

By default the editions table should likely only show books that are e.g. ebook_access:[borrowable TO *] (i.e. one page of results from some useful query)

mekarpeles avatar Aug 12 '26 13:08 mekarpeles

Thanks @mekarpeles! Excited to take this on.

Now that Phase 1 (GET /search/editions.json in FastAPI) and Phase 2 (GET /search/editions UI) have been established in #12663, I'm proposing the following concrete implementation plan for Phase 3: rewiring the Book Page "Other Editions" table to consume Solr.


Key Goals & Proposed Approach

1. Replace Infogami DB Fetches with Solr Edition Queries

  • Currently, get_sorted_editions() in models.py fetches raw Infogami edition objects via web.ctx.site.get_many(...). For works with hundreds or thousands of editions, this causes significant initial page load latency and heavy DB overhead.
  • Fix: Update the edition table provider (editions_datatable.html / editions.html / helper) to query Solr edition documents (type:edition scoped by work_key) via the Solr edition service / /search/editions.json pattern introduced in #12663.

2. Native Solr Pagination & Fixing the 1000-Edition Cap (#7233)

  • Solr handles pagination natively via start and limit. Rewiring the table to Solr's pagination will eliminate the 1000-edition truncation cap (#7233), allowing users to seamlessly paginate through all editions of works with large publication histories (e.g. classic literature).

3. Unblocking Work-Wide Availability & Language Boosting (#13179 / PR #13289)

  • In #13179 (PR #13289), we updated get_best_edition to boost edition selection by user language match and ebook availability tier. However, because candidate editions were previously sampled/capped via Infogami DB queries, borrowable or same-language editions outside the initial candidate sample could be missed.
  • Fix: Moving edition queries to Solr allows us to pass ebook_access and language boosting directly as query parameters upfront into the Solr request. Solr evaluates the entire edition pool for a work instantly without candidate sampling limitations.

4. (Optional / Needs feedback)

  • Interactive Table Sorting: Add simple controls above the editions datatable:
    • Sort Dropdown: Sort editions by Publication Date (Newest/Oldest), Popularity, or Relevance.
  • Async Datatable Pagination: Update table pagination to fetch subsequent pages (page 2, page 3) asynchronously via fetch('/search/editions.json?work_key=...&page=2') for instant navigation without full page reloads.
  • Foundation for Edition Management Actions: Lay the initial UI structure (e.g., edition selection checkboxes) to prepare the datatable for future bulk librarian workflows like Edition Merge (#2114), edition deletion, or adding editions to personal lists.

Implementation Steps

  1. Backend Integration: Add a Solr-backed edition fetcher helper for works that converts Solr edition docs into the template-compatible payload needed by editions_datatable.html.
  2. Template Rewiring: Replace direct get_sorted_editions() calls in editions_datatable.html / editions.html with the Solr-backed edition provider.
  3. Query-Time Boosting: Connect user language (web.ctx.lang) and ebook_access boosting into the Solr edition query.
  4. Testing: Add tests in test_search.py and work page tests ensuring fast pagination, correct field rendering, and graceful fallback if Solr data is partial.

Looking forward to maintainers' thoughts on this approach! If this looks good, I'll dive in and open the draft PR.

RsbhThakur avatar Aug 14 '26 19:08 RsbhThakur

Also, I did some deep dive and cross referencing realted open issues

This approach on #7451 paired up with the my approach in #13179 will address and solve 4 interconnected open issues:


Issues that Will be closed

  1. #7233 — View all editions maxes out at 1000

    • Reason: Solr handles pagination natively via start and limit. Moving away from Infogami DB queries removes the artificial 1,000-record cap, allowing readers to view and paginate through all 3,500+ editions of classic works.
  2. #7452 — Books Page: Get Best Edition → Solr

    • Reason: Currently sitting in State: Blocked waiting for #7451. Shifting edition selection and fetching on work pages from Infogami DB to Solr in this issue along with update of get_best_edition in #13179 satisfies #7452's exact requirements.
  3. #13179 — Smart "Available Alternate Edition" Fallback on Work Pages

    • Reason: The language match and ebook availability boosting algorithm implemented in PR #13289 was previously limited by candidate sampling (top 10 DB records). Solr allows this boosting logic to evaluate the entire edition index upfront.
  4. #10925 — Editions table filter does not work when ISBN 10 and ISBN 13 aren't equivalent

    • Reason: Replaces legacy client-side jQuery DataTables DOM text matching (which broke when hidden ISBN 10/13 representations differed) with structured Solr index queries over both isbn_10 and isbn_13 fields.

This makes #7451 a multi-issue win! Let me know if you have any suggestions or feedback before i start working along this appraoch.

RsbhThakur avatar Aug 14 '26 19:08 RsbhThakur

Hi, thank you for doing this research. I agree that all of the issues you referenced are interrelated! For this specific issue, rather than to attempt a solution that satisfies all four directions, I'd strongly encourage you to prototype a version of the editions table in isolation that is powered by solr. Sometimes developing the simplest integration path in isolation is the fastest way to understand how difficult the problem is and the real world characteristics of the solution.

I think the majority of the value of this feature will be both in the performance improvements, if any, and also the user experience improvements of the new table and its search capabilities.

We want to make sure that we build something that is actually usable and useful to the community and if we can develop a prototype of the table wired up to solar and let the community try it out on our testing server, this would be a great first milestone.

mekarpeles avatar Aug 20 '26 10:08 mekarpeles

As Per the discussion with @mekarpeles, This issue will be done in the following milestones:

  • Milestone 1: Minimal, isolated prototype view at /works/OL...W/solr_editions. It uses field-restricted Solr queries (rows=20,facet=False) and UI similar to editions_datatable.html, along with timing benchmark logs so we can measure raw Solr query latency on staging with zero risk to live book pages.
  • Milestone 2: Interactive client-side JS, text searchbox, availability filters, and sort dropdowns.
  • Milestone 3: Live book page editions table rewire (editions_datatable.html) + 5-minute Memcached layer to protect Solr under production traffic.
  • Milestone 4: Query-time boosting for language & availability (language:<user_lang>^40,ebook_access:borrowable^8) to handle best-edition selection and handle related issues.

Also I would like to note that Draft PR #13384, has already been done targetting milestone 1, and needs feedback and suggestions from Mek, or other maintainers to continue the work on this and the related issues.

RsbhThakur avatar Sep 08 '26 18:09 RsbhThakur

Now that #13863 has been merged, I will be proceeding to further milestones of this issue.

Here is how the follow-up work maps across the remaining milestones:

  • Milestone 2 (Next Step - Book Page Rewire):
    • Swap out Infogami DB edition fetching (web.ctx.site.get_many) in get_sorted_editions() and editions_datatable.html with this Solr service.
    • Add a 5-minute JSON Memcached layer (cache.memcache_memoize) for default work page views to ensure zero Solr overhead in production.
    • Closes #7451 and #7233.
  • Milestone 3 (Query-Time Boosting for Best Edition Selection):
    • Connect language and availability boosting directly into Solr query parameters (language:<user_lang>^40, ebook_access:borrowable^8).
    • Closes #7452 and #13179.
  • Milestone 4 (Interactive Client Filtering, Sorting, & Solr 10 Syntax):
    • Add interactive searchbox (q), format/ebook availability dropdowns, and Solr 10 sort syntax.
    • Wire progressive AJAX updates via the FastAPI /search/editions.json endpoint.
    • Lay the foundation for Edition Merge (#2114) librarian checkboxes and close #10925.

RsbhThakur avatar Oct 08 '26 20:10 RsbhThakur