kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Filter content by base language code

Open rtibbles opened this issue 4 months ago • 4 comments

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.languages in kolibri/core/content/viewsets/contentnode/filters.py is a CharInFilter over lang_id, an exact match on the Language primary key.
  • ChannelMetadata.included_languages already 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_quiz uses.
  • content_contentnode has no index pairing tree_id with lft, 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's included_languages M2M without storing a rollup.
  • Django renders available=True as a bare "available", so an index with available as a key column cannot seek past it, and takes 7.9 s when most nodes are unavailable.
  • Bounding descendants by rght < parent.rght instead of lft drops the range's upper bound, and takes 7.5 s on the same DB.
  • packages/kolibri/utils/i18n.js treats 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.js alias 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 es returns resources tagged es, es-ES, es-MX and es-419.
  • [ ] Filtering on ff, ny, sw or yo also 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 en when filtering on fr, if the channel holds available fr resources.
  • [ ] Resources with no language are excluded when the filter is applied.
  • [ ] The existing exact-lang_id filter 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_utils native name.
  • [ ] Content tagged fuv appears in those labels under ff.
  • [ ] A base code unknown to le_utils is named by the lang_name of a matching language, from a Language row locally or a peer's languages label, or by the code when none has a name.
  • [ ] The existing languages labels 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 languages labels.

Performance

  • [ ] content_contentnode has an index on (tree_id, lang_id, lft) with the condition available, 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_contentnode in 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.

rtibbles avatar Jul 21 '26 00:07 rtibbles

/assign

Ashish948 avatar Oct 12 '26 15:10 Ashish948

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.

learning-equality-bot avatar Oct 12 '26 15:10 learning-equality-bot

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_languages filter on node, tree and channel endpoints
  • [ ] Partial index and query-plan guarantees
  • [ ] base_languages search 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" and kolibri_version 0.21 or later — as base.py:261-273 and learn useDevices.js:48 gate 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_languages and ignores it, like an older Kolibri; if it rejects unknown parameters, the proxy must drop base_languages for 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_languages unchanged to an older peer, which ignores it and returns unfiltered results; rewriting it to languages would 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 with contentnode_previously_omitted_fields today; closing it means the proxy's ETag must also vary with this device's ContentCacheKey (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_utils lacks, 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.

rtibblesbot avatar Nov 25 '26 23:11 rtibblesbot

Decided without asking; a reply here changes it.

  • usercontentnode's next_steps is left as it is: with base_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=es and kind= narrow history the same way (ran next_steps=true with each: the Spanish sibling is returned unfiltered, absent under all three).
    • No caller reaches it: fetchNextSteps has no call site and homehydrate sends only resume.
    • 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") at user.py:94, which stops the 500 on next_steps with base_languages or contains_quiz, and its subtests in test_next_steps_sibling.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 27 '26 23:11 rtibblesbot