Skip to content

OpenConceptLab/ocl_online#739 | The collection facets list only collection versions the requester can view - #1044

Merged
snyaggarwal merged 4 commits into
masterfrom
search-739-collection-facet
Nov 24, 2026
Merged

snyaggarwal merged 4 commits into
masterfrom
search-739-collection-facet

Conversation

@paynejd

@paynejd paynejd commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Closes OpenConceptLab/ocl_online#739.

What changes

A concept or mapping search's collection facet lists only collections the requester can view, and each count covers only the matches in the versions they can view. The rule is the one the collection filter already uses (Collection.get_viewable_versions_criteria, from #1027), so a collection bucket's count is what that name's filter finds. The collection_version and expansion facets (in org, user, source and collection scopes) follow the same rule. They aren't query-string filters: naming one of their buckets lists the scope as it is. A name the requester can't view in any version is left out. Versions since deleted, which the index keeps until their members are indexed again, drop out too. Staff see no change.

How

  • The faceted search also lists the top 300 collection names matched (_collection_names, max_collection_facet_names). It's removed before the facets are returned.
  • keep_collection_facets_viewable reads, in one DB query, the URLs of those collections' versions the requester can view, and for the expansion facet their expansions' URLs. It counts each collection name, and each collection_version and expansion name the facet lists, in one size-0 Elasticsearch request: a filters aggregation with a terms filter on collection_url or expansion_url per name. Names read as the documents read them.
  • A collection outside the top 300 by match count isn't counted, so its name can be missing, never wrong. A version or expansion name across more than 300 collections can undercount.
  • get_facets splits in two. get_facets(for_requester=False) gives the facets as anyone who can run the search sees them, and get_requester_facets applies what depends on the requester: updatedBy only for the signed in, and the collection facets above.
  • A facetsOnly answer on a source version's list is still cached for a day, now as get_facets(for_requester=False), under a key ending #for-anyone. Every answer, cached or not, goes through get_requester_facets, so a cached entry never carries one requester's view to another, and losing access takes effect at once. Entries cached before this change sit under the old key and aren't read. The repository's cache clearing matches by prefix, so it still clears them.
  • CustomESFacetedSearch takes extra_facets.

Cost

For requesters who aren't staff, a facets request costs one more terms aggregation (the 300 names, in the same request), one DB query, and one size-0 Elasticsearch request when any collection facet has buckets. A cached facetsOnly hit saves the main aggregation only: the DB query and the size-0 request still run on every hit. Staff skip the DB query and the extra request.

Tests

QueryCollectionAccessTest (ES-tagged), for signed out, an outsider, a member and staff, concepts and mappings:

  • global search's collection facet: a private collection, a public one with a private version, and a public one with a public version. Each count equals what that name's collection filter lists;
  • the same name under another owner's private collection counts only for those who can view it;
  • a deleted collection's stale entries drop out for everyone but staff; a version whose only expansion was deleted still counts while its members' documents name it;
  • source-scope collection_version and expansion facets leave out a private version's name, and a version name with a space keeps the index's form for everyone. Neither facet is a query-string filter;
  • the cached facetsOnly answer on a source version: a member's view doesn't reach an outsider or a signed-out requester, nor outlast the member's access.

ConceptListCacheTest / MappingListCacheTest: the facetsOnly cache key ends #for-anyone, and an answer cached under the old key isn't read.

Local runs on 0290592: pylint -j0 core/ clean; ES-tagged suite 701 OK; non-ES suite 3,647, one failure in core.capacity's renewer-thread timing test, which passes alone (80 OK) and is a known flake under load. QueryCollectionAccessTest on b73149a: 13 OK.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49

…ction versions the requester can view

A concept or mapping search's collection, collection_version and expansion facets named every collection version and
expansion its matches belong to, as the index lists them: versions the requester can't view and versions since
deleted too, with their member counts. For a requester who isn't staff, each name now counts only the matches in the
versions they can view, by the rule the collection filter uses (Collection.get_viewable_versions_criteria), and a name
with none is left out. A bucket's count is what its collection filter finds for them.

The facet request also asks for the collection version URLs (and, outside global search, expansion URLs) among the
matches, up to max_viewable_collection_urls; one query keeps the ones the requester can view. A name with one URL
takes that URL's count; names with several get exact counts from one more size-0 request (a filters aggregation).
Past the URL cap a name can undercount, but a version the requester can't view never shows. Staff are
unchanged.

A facetsOnly answer on a source version's list is cached for a day; its cache key now names the requester, so one
requester's facets never reach another.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
@paynejd
paynejd requested a review from snyaggarwal November 24, 2026 04:09
…ctions matched, and a cached facets answer is filtered for each requester

Review pass 1 on oclapi2#1044:

- The facets no longer list the version URLs among the matches, which stopped at 1,000: on a broad search, versions
  the requester can't view could crowd out the ones they can. The search now lists the top 300 collection names
  matched (`_collection_names`), and one query reads the viewable versions' expansions of those collections. A
  filters aggregation counts each collection, version name and expansion over the URLs those expansions give their
  members' documents, so the names read as the index has them (a version name with a space stays encoded).
- A facetsOnly answer on a source version's list is cached again as anyone who can run the search sees it, and each
  answer, cached or not, is then filtered for its requester (get_requester_facets). A collection made private, or a
  member who leaves, no longer leaves its name in a cached answer. The cache key is the shared one again, and the
  updatedBy facet is dropped for signed-out requesters on cached answers too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Codex review, pass 1 (codex-cli 0.169.0, commit 313c498)

Verdict: findings: 2 P1, 3 P2. Two P1s fixed and one P2 fixed in ad283a2; two P2s accepted. Detailed scenarios are on the tracking ticket.

  • F1.1 (P1), fixed: a cached facetsOnly answer could keep a collection's name after the requester lost access to it. The cache now holds the facets as anyone who can run the search sees them, and every answer, cached or not, is filtered for its requester. A test removes a member after warming the cache.
  • F1.2 (P1), fixed: the 1,000-URL list could drop or undercount collections the requester can view on a broad search. There's no URL list now: the search lists the top 300 collection names matched, one query reads the viewable expansions of those collections, and one filters aggregation counts each name exactly. A collection outside the top 300 by match count isn't counted (missing, never wrong), as with any top-N terms facet.
  • F1.3 (P2), accepted: between a collection's delete and its members' reindex, a collection recreated at the same URL can be counted with the old members. The collection filter has the same window. OpenConceptLab/ocl_online#740 (the next PR) reindexes members on delete.
  • F1.4 (P2), accepted: how the index attributes membership for a version whose name matches a route segment. That's existing index behaviour, shared with the collection filter, and outside this change. Raised on the ticket.
  • F1.5 (P2), fixed: a version name with a space was keyed decoded for non-staff and encoded for staff. Names are now read from the expansion URIs exactly as the documents are, and a test covers a version named v 2.

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Test runs on ad283a2 (local compose stack, oclapi2-local image with pylint 4.0.9): pylint -j0 core/ clean; ES-tagged suite 700 tests OK; TEST_ES=off --exclude-tag es --parallel=4 3,647 tests OK. The new facet tests fail against master's views.py, mixins.py and search.py.

…ction filter matches them; shared facet entries get their own cache key

Review pass 2 on oclapi2#1044:

- The collection and collection_version facets count over the URLs of the collection versions the requester can
  view, as the collection filter matches them, rather than over their expansions. A version whose only expansion was
  deleted is still counted while its members' documents still name it. The expansion facet counts over expansion
  URLs. A version or expansion name is counted only if its facet lists it, so the count request stays the size of
  the facets.
- A facetsOnly entry is cached as anyone sees it, under a key ending in `#for-anyone` (FACETS_CACHE_SUFFIX). So an
  answer cached before as one requester's, under the old key, is never read as anyone's, and code from before this
  change never reads the new entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at ad283a2. Two findings inline, plus two notes.

  • Stale description. The PR description still describes the first pass: facets built from a list of up to 1,000 URLs (max_viewable_collection_urls), and a cache key that names the requester. Since ad283a2 the facets work from the top 300 collection names, the cache key is unchanged, and each answer is filtered for its requester. Please update it so the merged PR text matches the code.
  • Cost of a cache hit. A cached facetsOnly answer still costs a DB query on every hit for non-staff. When any collection names are present it also costs a size-0 ES request. So the day-long cache now saves only the main aggregation. That's fine by me, but the "Cost" section should say so.

Codex pass 1 (F1.1–F1.5, in the author's comment above) is already covered there, so I haven't repeated it.

Comment thread core/common/views.py
Comment on lines +73 to +74
collection_url_facets = {'collection': 'collection_url', 'collection_version': 'collection_url',
'expansion': 'expansion_url'}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recount makes the collection_version and expansion facets count only versions the requester can view, but their filters still read the raw index. They aren't in collection_facets, so get_faceted_filters turns ?collectionVersion= and ?expansion= into a plain term query, and get_collection_facet_criterion never sees them. In a source scope:

  • An outsider sees v1: 1, clicks it, and gets the members of every collection's v1, private ones included. That's more rows than the count, and it shows which concepts are in a version they can't view.
  • A name left out of the facet still lists its members when typed in. In the new test, ?collectionVersion=hidden… works for an outsider.

So the description's "a bucket's count is what clicking it finds" holds for collection only.

Fix options:

  • In this PR: route these two filters through the same viewable criterion. collection_version=<name> becomes terms collection_url over the viewable versions with that name, and expansion=<name> becomes terms expansion_url over the viewable expansions.
  • Or open a follow-up ticket and narrow the description's claim to collection.

Either way, a check like listed(f'{url}?collectionVersion=v1', self.outsider, 'concepts') next to the facet assertions in test_collection_version_and_expansion_facets_name_only_versions_the_requester_can_view would pin it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I checked this on b73149a before changing anything: collection_version and expansion aren't query-string filters. Neither is in Concept.es_fields or Mapping.es_fields, so get_faceted_fields never picks them up. In a source scope, an outsider's and a member's ?collectionVersion=v1, ?collectionVersion=hidden…, ?expansion=hidden… and ?collectionVersion=nope all list exactly the unfiltered scope, on both the DB path and the search path (with q). So clicking a bucket doesn't narrow anything and can't show a version's members.

I pinned it as you suggested, next to the facet assertions in that test (b73149a): each of those queries lists what the unfiltered scope lists for an outsider. The description now claims "count = what clicking finds" for collection only, and says the other two aren't filters. If they're ever made filterable, they should go through get_collection_facet_criterion like collection.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, my mistake. get_faceted_fields builds the filter list from es_fields, and neither Concept.es_fields nor Mapping.es_fields has collection_version or expansion. So ?collectionVersion= and ?expansion= never reach the query, and nothing leaks. The new assertions in b73149a pin that, and the description now scopes its claim to collection. Resolved.

Comment thread core/common/mixins.py
Comment on lines +238 to +239
if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them
return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entries cached before this deploys (they live for a day) were stored by the old get_facets(), so they have no _collection_names. On a hit, keep_collection_facets_viewable gets collections == [] and finds no URLs. It then sets collection, collection_version and expansion to [] for every requester who isn't staff, until the entry expires or the repo's cache is cleared. So for up to a day after release, every source version's cached facets would show no collections.

_collection_names is always in the new entries (the faceted response has a key for every facet, even an empty one), so an entry without it is from before the change. Treating it as a miss recomputes the entry and caches it in the new shape:

Suggested change
if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them
return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}})
if data and self.only_facets(): # cached as anyone sees them, so as this requester sees them
if '_collection_names' in get(data, 'facets.fields', {}):
return Response({'facets': {'fields': self.get_requester_facets(data['facets']['fields'])}})
data = None # cached before collection facets were recounted

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. 0290592 handles it with a separate key rather than a shape check: shared entries are stored under a key ending #for-anyone (FACETS_CACHE_SUFFIX), so entries from before the change sit under the old key and are never read. Non-staff get a fresh computation on the first hit, which is then cached in the new shape. It also covers a rollback (Codex pass 2, F2.1): old code never reads the new entries, which hold _collection_names. test_repo_version_concepts_facets_ignore_answers_cached_before checks that an answer under the old key isn't read.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #for-anyone key works better than my shape check: it also covers a rollback, and clear_cache still clears it because delete_pattern matches by prefix. Resolved.

…nsion facets aren't query-string filters

Naming one of their buckets in the query string lists the scope as it is (review on oclapi2#1044).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Codex review, pass 2 (codex-cli 0.169.0, commit ad283a2)

Verdict: findings: 1 P1, 4 P2. The P1 and two P2s are fixed in 0290592 (and b73149a adds a test); two P2s accepted. From pass 1, Codex reads F1.1 and F1.2 as followed up by F2.1 to F2.4 below; F1.5 resolved; F1.3 and F1.4 accepted as before (F1.4 is now OpenConceptLab/ocl_online#753). Detailed scenarios are on the tracking ticket.

  • F2.1 (P1), fixed: the shared facets entries used the old cache key, so during a rolling deploy or rollback each version of the code could read the other's entries. Shared entries now use their own key (ending #for-anyone). A test shows an answer cached under the old key isn't read.
  • F2.2 (P2), accepted: a shared cached answer assumes everyone who can run the search reads the same documents, which legacy owner names differing only in case can break. That assumption predates this change: master shares the same facetsOnly entry, and the list's first page, across requesters. This change doesn't widen it.
  • F2.3 (P2), accepted: collection_version and expansion counts cover the top 300 collections matched. Past that, a version name can undercount, never overcount. These two facets appear only outside global search.
  • F2.4 (P2), fixed: the recount could build a bucket for every version and expansion name in the candidate collections' histories. It now counts only names the facets list (up to 100 each) and up to 300 collection names.
  • F2.5 (P2), fixed: collection and collection_version now count over the viewable collection versions' URLs, as the collection filter matches them, rather than over their expansions. A version whose only expansion was deleted still counts while its members' documents name it. Tested.

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Thanks, Sunny. Both notes are done in the description: it now describes the top-300 names, the #for-anyone cache key and per-requester filtering. Under Cost it says a cached facetsOnly hit still runs the DB query and the size-0 Elasticsearch request, and saves only the main aggregation. Inline replies are on your two findings: the cache one is fixed in 0290592, and the filter one doesn't reproduce (now pinned by a test in b73149a).

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Codex review, pass 3 (codex-cli 0.169.0, commit b73149a)

Verdict: findings: 2 P2, no P0 or P1. Both accepted; this was the last Codex pass. Codex reads F1.1, F1.2, F1.5, F2.1 and F2.5 as resolved. F1.3, F1.4 (OpenConceptLab/ocl_online#753), F2.2 and F2.3 stay accepted. It reads F2.4's fix as narrowing the version and expansion names (F3.2 below).

  • F3.1 (P2), accepted: on a global search, collections the requester can't view can fill the 300-name window, so a viewable collection with fewer matches goes unlisted. It's a missing name, never a wrong or private one. Ranking by viewable URLs before the cutoff would mean paging every matched version through the recount, which isn't small.
  • F3.2 (P2), accepted: in a scoped search, collection_version and expansion count only the names their raw facet lists (top 100), so private names can push a viewable one out. Again the name is missing, never wrong or private. Lifting it needs batched recounts, which isn't small either.

Simplification review and the adversarial access review follow.

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Simplification review (Claude subagent, commit b73149a)

Verdict: 4 suggested cuts, all low confidence. None applied, so the head stays b73149a and no Codex check of later commits is needed.

  • S1 (shrink), declined: the two facetsOnly returns sit on different paths. The cache hit returns before the search path that every other cached list shares, and the miss returns after caching. Merging them means reshaping that shared flow to save one line.
  • S2 (yagni), declined: get_viewable_collection_facet_urls keeps the database step (which versions and expansions the requester can view, and their names as the documents read them) apart from the Elasticsearch count. Inlining it would make keep_collection_facets_viewable longer and harder to check.
  • S3 (shrink), declined: grouping by facet in one pass comes out at about the same length. The lists are bounded by the facet size, so the extra scan costs nothing that matters.
  • S4 (dup-test), declined: the deleted-versions test covers a different case (rows deleted but still in the index) from the first facet test. Its mappings pass goes through the mapping document and facet class, which the concepts pass doesn't reach.

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Adversarial access pass (Claude subagent, commit b73149a)

Verdict: no way in found. A requester who isn't staff can't learn the name of a collection, version or expansion they can't view, or how many concepts or mappings it holds, through the collection, collection_version or expansion facets, the facetsOnly cache, or the collection filters.

  • Every facet name in an answer is rebuilt from collection versions and expansions the requester can view, and each count is limited to their URLs. The index's raw buckets never reach the response.
  • Both facet paths (facetsOnly and includeFacets) go through get_requester_facets. The cache stores only the requester-independent facets, and every answer, cached or not, is filtered for the current requester.
  • Three P3 notes, all on code this PR doesn't change. None reveals a name or a count. Details are on the tracking ticket.

No change follows from it.

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at b73149a (0290592 and b73149a). Both threads are resolved, and my first finding was wrong: collection_version and expansion were never filters.

get_viewable_collection_facet_urls now reads version URLs and names from Collection.uri, the same URLs the collection filter matches through. A HEAD version's URI has no version segment and reads as HEAD, which matches how documents store a HEAD version's collection_url. So a collection whose expansion was deleted on its own still counts what its filter lists, which the new deleted-expansion assertion covers. Version and expansion names are now limited to what the raw facet lists, so the recount stays the facet's size. The new description is accurate, including the cost of a cached hit.

The two accepted gaps, F3.1 and F3.2, can only leave a name missing. They never show a wrong count or a private name. That's fine by me.

Optional nit: test_repo_version_concepts_facets_ignore_answers_cached_before has no mappings twin, unlike the other cache tests. The code path is shared, so this isn't blocking.

No new findings from me.

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at b73149a. Both earlier threads are resolved; the one remaining nit (a mappings twin for the old-cache-key test) is optional.

@snyaggarwal
snyaggarwal merged commit 9ddb485 into master Nov 24, 2026
3 checks passed
@snyaggarwal
snyaggarwal deleted the search-739-collection-facet branch November 24, 2026 05:33
@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Codex check of commits b73149a..e3dd745 (codex-cli 0.169.0)

Verdict: clean, no findings.

e3dd745 adds only the mappings twin of the old-cache-key test that the review asked for: MappingListCacheTest.test_repo_version_mappings_facets_ignore_answers_cached_before. It passes locally with the rest of MappingListCacheTest and the ES-tagged concepts tests (67 tests OK).

@snyaggarwal: your optional nit is done in e3dd745.

@paynejd

paynejd commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

The merge took b73149a, so the mappings twin test (e3dd745) isn't in master. It's still on the branch if you want it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants