openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Search : DDC query normalization is broken due to dcc typo and latent transform bugs

Open CipherZ3r0 opened this issue 2 months ago • 2 comments

Description

DDC query normalization in WorkSearchScheme.transform_user_query is currently unreachable because the dispatch condition checks for dcc instead of the actual ddc field:

if node.name in ("dcc", "dcc_sort"):
    ddc_transform(node)

dcc does not exist as a valid Open Library/Solr field; the actual fields are ddc and ddc_sort.

As a result, queries such as:

ddc:65.8
ddc:[61 TO 65]

are not normalized before being sent to Solr.

The work index stores normalized DDC values, for example:

65.8 → 065.8
61   → 061
65   → 065

so these queries can return incorrect or no results.

Additional issue

While investigating the unreachable ddc_transform() code, three additional bugs were found that become active once the dcc typo is fixed:

  1. normalize_ddc() returns a list, but the code assigns the list directly to val.value.
  2. Prefix normalization is returned instead of being assigned back to val.value.
  3. Range fallback assigns the Luqum node itself instead of its .value.

Therefore, fixing only the dcc → ddc typo can turn the current silent search failure into transformation errors.

Reproduction

Current behavior:

ddc:65.8       → ddc:65.8
ddc:[61 TO 65] → ddc:[61 TO 65]

Expected:

ddc:65.8       → ddc:065.8
ddc:[61 TO 65] → ddc:[061 TO 065]

Other expected behavior:

ddc:658        → ddc:658
ddc:23.45*     → ddc:023.45*
ddc_sort:65.8  → ddc_sort:065.8

Proposed fix

Fix the dispatch condition and the three latent bugs in ddc_transform() atomically.

Add regression coverage for DDC prefix, range, wildcard, and ddc_sort queries.

Relevant files:

  • openlibrary/plugins/worksearch/schemes/works.py
  • openlibrary/plugins/worksearch/tests/test_worksearch.py
  • openlibrary/utils/ddc.py
  • openlibrary/tests/solr/updater/test_work.py

No existing test currently covers DDC query transformation.

@mekarpeles @cdrini

found by @RayBB

CipherZ3r0 avatar Sep 29 '26 18:09 CipherZ3r0

Thank you @CipherZ3r0 for submitting this issue!

Context (Richy's technical contribution)

Verified directly against the current codebase (6c255c6):

  • The typo is confirmed at works.py#L293-L296 — the sibling lcc/lcc_sort dispatch on the line directly above it is spelled correctly, which is likely how the ddc → dcc typo went unnoticed.
  • All three latent bugs in ddc_transform check out against openlibrary/utils/ddc.py:
    • L827-828: normalize_ddc() is typed -> list[str] (ddc.py#L49); val.value = normed assigns the whole list, not normed[0].
    • L824-825: this branch returns instead of assigning val.value — and since ddc_transform's return value is never used by the caller (ddc_transform(node) at L296 discards it), the wildcard/prefix case is a complete no-op even after the dcc→ddc fix.
    • L822-823: val.low.value = normed_range[0] or val.low — the fallback assigns the Luqum SearchFieldValue node itself (val.low), not its .value string, when normed_range[0] is falsy.
  • lcc_transform (immediately above, works.py#L780-L810) handles the equivalent range/prefix cases correctly (if normed_range: val.low.value, val.high.value = normed_range) and is a reasonable pattern to mirror for the range-fallback fix.
  • Test coverage confirmed as described: openlibrary/utils/tests/test_ddc.py thoroughly unit-tests normalize_ddc/normalize_ddc_prefix/normalize_ddc_range in isolation, but openlibrary/plugins/worksearch/tests/test_worksearch.py has no tests exercising transform_user_query, ddc_transform, lcc_transform, or isbn_transform at all — so integration-level regression coverage would be new, not just an addition to an existing suite.
  • No duplicate or related open issue/PR found (searched "ddc", and files touching works.py); this repo checkout is shallow (depth 1), so git blame/git log -S could not be used to identify who introduced the typo or when.

Issue Assessment

Action items

  • [x] Is there prior related work or a duplicate? (Answer: none found via issue/PR search on "ddc" or on files touching works.py.)
  • [ ] Confirm whether the fix should also address the ddc_transform wildcard branch's dead-return independently of the dict-key typo fix, since fixing only the typo (as flagged in the issue) will start executing all three latent bugs at once — a reviewer/lead call on whether this should land as one atomic PR or be sequenced.

Suggested breakdown

  • [ ] Fix "dcc"/"dcc_sort" → "ddc"/"ddc_sort" in the dispatch condition (works.py#L295)
  • [ ] Fix ddc_transform's three assignment bugs (list vs. [0], return vs. assignment, range fallback using the node vs. .value)
  • [ ] Add regression tests to test_worksearch.py for ddc:65.8, ddc:[61 TO 65], ddc:658, ddc:23.45*, ddc_sort:65.8 per the reproduction cases in this issue
  • [ ] Could be a Good First Issue — the bug locations, root causes, and expected input/output pairs are already fully specified in this issue; main risk is unfamiliarity with the luqum query-tree API used by ddc_transform/lcc_transform

🔔 Stakeholders

@cdrini — solr/search lead; owns this area per the stakeholder reference, and Theme: Search/Module: Solr apply directly here.

Full triage checklist
  • [x] Triage — correct labels applied or removed
  • [x] Problem / opportunity — problem is clear, audience identified, actionable
  • [x] Scope — issue is well-scoped; does not try to do too many things
  • [x] Justification — reasoning and impact stated; sufficient for leads to prioritize
  • [x] Success & testing criteria — defines what "done" looks like, verifiable by a contributor
  • [x] Proposal & breakdown — concrete plan present; checklist actionable for a contributor
  • [ ] If bug: environment & repro steps — no browser/version info given, but this is a backend/query-parsing bug where that's not applicable; query-level repro is present
  • [x] Risks, concerns, open questions — identified and surfaced
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
  • [x] References — code line references present; no screenshots/designs needed for this bug

[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Sep 29 '26 18:09 openlibrary-bot

I'd like to pick this up, if a lead is happy to assign it.

I've read through the code and can confirm all four parts, plus one thing that explains why it went unnoticed.

The typo. works.py:295 reads if node.name in ("dcc", "dcc_sort"), directly under works.py:293's correct ("lcc", "lcc_sort") — a copy-paste slip. Since no field is ever named dcc, ddc_transform() has never run: it's been dead code, and ddc: queries have gone to Solr unnormalized the whole time.

The three latent bugs, each of which only bites once the dispatch is fixed:

  1. normalize_ddc() is annotated -> list[str] (utils/ddc.py:49), but works.py:828 does val.value = normed, putting the list into the node value.
  2. The prefix branch (works.py:825) returns normalize_ddc_prefix(...) + "*" instead of assigning it. The caller at line 296 discards the return value, so the normalization is thrown away.
  3. The range fallback (works.py:822-823) does val.low.value = normed_range[0] or val.low — on the fallback path that assigns the luqum Word node rather than val.low.value.

Why it stayed hidden: schemes/tests/test_works.py has LCC query cases (lines 80-97) but no DDC ones, so nothing exercised this path. That's also the natural place for the regression tests — the file is a dict of named (input_query, expected_normalized_query) pairs, so DDC cases drop straight in alongside the LCC ones.

Approach, assuming that all sounds right:

  • Fix the dispatch to ("ddc", "ddc_sort").
  • Make ddc_transform() mutate val.value consistently, the way lcc_transform() directly above it already does, rather than returning in one branch and assigning in others.
  • Add DDC cases to test_works.py covering each branch: a plain value (ddc:65.8 → 065.8), a prefix (ddc:65*), a range (ddc:[61 TO 65]), and a range with an open end to exercise the fallback.
  • Each test should fail on master before the fix, so they pin the behaviour rather than the patch.

One thing I'd rather ask than assume, @cdrini: normalize_ddc() returns a list. Taking [0] is the obvious minimal fix, but is that right, or should a value that normalizes to several DDCs become an OR across them? Happy to go either way — I just don't want to silently pick one.

I'll hold off opening anything until this is triaged and assigned.

iamtanmaybaranwal avatar Oct 01 '26 11:10 iamtanmaybaranwal