Filter content by base language code
Overview
Content language filtering matches on exact language id, so a preference for Español misses resources tagged es-MX. Add query support for filtering on the base language code.
Complexity: Medium Target branch: develop
Context
ContentNodeFilter.languagesinkolibri/core/content/viewsets/contentnode/filters.pyis aCharInFilteroverlang_id, an exact match on theLanguageprimary key.ChannelMetadata.included_languagesalready lists each channel's languages, computed from available nodes.- Topics store no languages, so matching a topic means a correlated descendant subquery, the pattern
filter_contains_quizuses. content_contentnodehas no index pairingtree_idwithlft, so on a 52k-node DB that subquery takes 14.8 s across all 12.8k topics.- With the partial index in the acceptance criteria and descendants bounded by
lft > parent.lft AND lft < parent.rght, the same check takes 4–6 ms, matching #12892'sincluded_languagesM2M without storing a rollup. - Django renders
available=Trueas a bare"available", so an index withavailableas a key column cannot seek past it, and takes 7.9 s when most nodes are unavailable. - Bounding descendants by
rght < parent.rghtinstead oflftdrops the range's upper bound, and takes 7.5 s on the same DB. packages/kolibri/utils/i18n.jstreats some ISO 639-3 content codes as the same language as a base code:ff→ful/fuv,ny→nya,sw→swa,yo→yor.
The Change
- A base code matches every regional variant of that language, and the ISO 639-3 codes listed in Context.
- The backend holds a Python copy of the
i18n.jsalias map, and each copy references the other's file. - A resource matches on its own language, a topic matches when any available descendant does, and a channel matches on
ChannelMetadata.included_languages.
How to Get There
- Call
/api/content/contentnode/,/api/content/contentnode_tree/<id>/and/api/content/channel/for the local Library. - Call
/api/public/v2/contentnode/,/api/public/v2/contentnode_tree/<id>/and/api/public/v2/channel/for peers browsing this device. - Check query plans on a device with a partially imported channel, where most nodes are unavailable.
Out of Scope
- Frontend consumption of the new filter.
- Storing or seeding a user's language preference.
- Ordering results by regional variant.
Acceptance Criteria
General
- [ ] The content node list, content node tree and channel list endpoints, local and public v2, accept a set of base language codes.
- [ ] Filtering on
esreturns resources taggedes,es-ES,es-MXandes-419. - [ ] Filtering on
ff,ny,sworyoalso returns resources tagged with its ISO 639-3 codes from Context. - [ ] Requesting several codes returns the union, with each node appearing once.
- [ ] A topic whose only matching descendants are unavailable is excluded.
- [ ] The tree endpoint returns a channel whose root topic is tagged
enwhen filtering onfr, if the channel holds availablefrresources. - [ ] Resources with no language are excluded when the filter is applied.
- [ ] The existing exact-
lang_idfilter behaves as it does today. - [ ] Callers can determine whether a peer supports the base-code filter.
- [ ] Search result labels list each available base language once under a new key, named by its
le_utilsnative name. - [ ] Content tagged
fuvappears in those labels underff. - [ ] A base code unknown to
le_utilsis named by thelang_nameof a matching language, from aLanguagerow locally or a peer'slanguageslabel, or by the code when none has a name. - [ ] The existing
languageslabels are unchanged, since older Kolibri clients read them from peers. - [ ] Browsing a peer that predates this change returns the new key, built from that peer's
languageslabels.
Performance
- [ ]
content_contentnodehas an index on(tree_id, lang_id, lft)with the conditionavailable, on SQLite and PostgreSQL. - [ ] The topic check's query plan is
SEARCH … USING INDEX <that index> (tree_id=? AND lang_id=? AND lft>? AND lft<?). - [ ] Adding the filter to any of the endpoints above introduces no new
SCAN content_contentnodein its query plan.
Testing
- [ ] Tests cover base-code matching across regional variants and the ISO 639-3 codes.
- [ ] Tests cover a topic matched through an available descendant, and excluded when that descendant is unavailable.
- [ ] A test asserts the SQLite query plan of the topic check.
References
- #12892, superseded by this work.
kolibri/core/content/viewsets/contentnode/filters.py:ContentNodeFilter,filter_contains_quiz.kolibri/core/content/viewsets/channel_metadata.py:ChannelMetadataFilter.kolibri/core/content/viewsets/contentnode/base.py:InternalContentNodeMixin.update_data.packages/kolibri/utils/i18n.js:contentLanguageCodes.docs/backend_architecture/api_patterns.rst
AI usage
Drafted with Claude Code from a verbal breakdown of the work and a review of #12892. Claude benchmarked #12892's M2M against index variants on a local 52k-node database. I chose the partial index, backend alias expansion, the Python alias copy and backend-provided language names, and the scoping decisions are mine. I checked its reading of #12892 against the PR myself.
/assign
Hi! 👋
Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.
We really appreciate your willingness to help. 😊
Are you preparing for Google Summer of Code? See our GSoC guidelines.
Plan: A base_languages filter on the content node list, content node tree and channel list endpoints (local and public v2), a partial index that keeps its topic check an index seek, and a base_languages search label.
- [ ]
base_languagesfilter on node, tree and channel endpoints - [ ] Partial index and query-plan guarantees
- [ ]
base_languagessearch label, local and for older peers - [ ] Self-review the produced code
Assumptions — the issue did not settle these; say so if one is wrong.
- Callers learn peer support from the peer's device info —
application == "kolibri"andkolibri_version0.21 or later — asbase.py:261-273and learnuseDevices.js:48gate peer features, so this PR builds nothing for that criterion; an explicit flag instead adds a field to the public or proxied responses. - Studio does not support
base_languagesand ignores it, like an older Kolibri; if it rejects unknown parameters, the proxy must dropbase_languagesfor peers that fail the check above. - The parameter and the label key are both named
base_languages; another name is a rename across the filter, labels, backfill and tests. - The proxy forwards
base_languagesunchanged to an older peer, which ignores it and returns unfiltered results; rewriting it tolanguageswould 404 trees rooted in another language. - A browser holding a proxied node list from before the upgrade keeps it, without
base_languages, while the peer's ETag is unchanged, as withcontentnode_previously_omitted_fieldstoday; closing it means the proxy's ETag must also vary with this device'sContentCacheKey(remote.py:27,utils/cache.py:88). - SQLite before 3.36 (CI's Python 3.6/3.7 images,
tox.yml:110) plans the topic check as the same four-term seek; if CI shows otherwise, the plan test fails there and the query needs reshaping for that planner, not a skip. - A base code
le_utilslacks, with several named matches, takes the name of the lowest language id; another rule changes only that pick.
Written by rtibblesbot, an LLM-based coding agent.
Decided without asking; a reply here changes it.
usercontentnode'snext_stepsis left as it is: withbase_languages=es, an English completion unlocks no Spanish next step. #15385 carries the fix.- #15385 is blocked by this issue, describes this narrowed history as its starting point, and holds the criterion ("completions in any language unlock them") and the cross-language test.
- Not a regression: on unchanged code
languages=esandkind=narrow history the same way (rannext_steps=truewith each: the Spanish sibling is returned unfiltered, absent under all three). - No caller reaches it:
fetchNextStepshas no call site andhomehydratesends onlyresume. - Lines for fixing here: same filter at a sibling endpoint, and #15385 has no assignee or PR. Outweighed: nobody can meet the gap until #15385 adds the caller, and the fix changes how every inherited filter combines with
next_steps, on an endpoint outside this issue's six. - Kept:
.values("id")atuser.py:94, which stops the 500 onnext_stepswithbase_languagesorcontains_quiz, and its subtests intest_next_steps_sibling.
Written by rtibblesbot, an LLM-based coding agent.