Search : DDC query normalization is broken due to dcc typo and latent transform bugs
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:
normalize_ddc()returns a list, but the code assigns the list directly toval.value.- Prefix normalization is returned instead of being assigned back to
val.value. - 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.pyopenlibrary/plugins/worksearch/tests/test_worksearch.pyopenlibrary/utils/ddc.pyopenlibrary/tests/solr/updater/test_work.py
No existing test currently covers DDC query transformation.
@mekarpeles @cdrini
found by @RayBB
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 siblinglcc/lcc_sortdispatch on the line directly above it is spelled correctly, which is likely how theddc→dcctypo went unnoticed. - All three latent bugs in
ddc_transformcheck out againstopenlibrary/utils/ddc.py:- L827-828:
normalize_ddc()is typed-> list[str](ddc.py#L49);val.value = normedassigns the whole list, notnormed[0]. - L824-825: this branch
returns instead of assigningval.value— and sinceddc_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 thedcc→ddcfix. - L822-823:
val.low.value = normed_range[0] or val.low— the fallback assigns the LuqumSearchFieldValuenode itself (val.low), not its.valuestring, whennormed_range[0]is falsy.
- L827-828:
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.pythoroughly unit-testsnormalize_ddc/normalize_ddc_prefix/normalize_ddc_rangein isolation, butopenlibrary/plugins/worksearch/tests/test_worksearch.pyhas no tests exercisingtransform_user_query,ddc_transform,lcc_transform, orisbn_transformat 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), sogit blame/git log -Scould 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_transformwildcard branch's dead-returnindependently 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],returnvs. assignment, range fallback using the node vs..value) - [ ] Add regression tests to
test_worksearch.pyforddc:65.8,ddc:[61 TO 65],ddc:658,ddc:23.45*,ddc_sort:65.8per 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
luqumquery-tree API used byddc_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.
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:
normalize_ddc()is annotated-> list[str](utils/ddc.py:49), butworks.py:828doesval.value = normed, putting the list into the node value.- The prefix branch (
works.py:825)returnsnormalize_ddc_prefix(...) + "*"instead of assigning it. The caller at line 296 discards the return value, so the normalization is thrown away. - The range fallback (
works.py:822-823) doesval.low.value = normed_range[0] or val.low— on the fallback path that assigns the luqumWordnode rather thanval.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()mutateval.valueconsistently, the waylcc_transform()directly above it already does, rather than returning in one branch and assigning in others. - Add DDC cases to
test_works.pycovering 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.