Compute evidence-type and revision facets from the authorized result set without leaking restricted document counts.
Faceted retrieval: trustworthy counts under permission filters
A facet describes the candidate set
A facet groups search matches by a field such as document type, plant, revision or language. It is a navigation control, not a relevance score. If a query returns maintenance notes and incident reports, a type facet tells the reader how many eligible documents of each kind remain. Define whether counts apply before or after a selected facet: both are useful, but silently mixing them makes the interface inconsistent. For a retrieval answer, the same metadata can steer query routing toward a source type without replacing the relevance judgment.
Authorize before counting
Count only documents the requester may see at this moment. A hidden document must not appear in a facet count even if its text never appears in results; a count of one can reveal that a confidential investigation exists. Apply tenant, document-status and access filters before aggregation, then compute buckets from the filtered candidate set. If permissions change, invalidate cached counts with the search result. Document lifecycle includes deletion and revocation, both of which change counts.
Keep a stable counting unit
One source manual may produce 70 chunks, while a short incident note produces one. Counting passages would suggest 70 manuals unless the interface clearly labels the unit as passages. For a document-type facet, deduplicate by source document ID and revision policy first; for an answer-span facet, passage counts may be appropriate. Decide whether two revisions are separate documents or one current logical document. The decision should match the result list and citations. Chunk identity provides the parent IDs required for correct grouping.
Choose fields and cardinality deliberately
Use exact metadata values for facets; analyzed body text is a poor source of category keys. A plant name may contain punctuation that a full-text analyzer splits, and a free-form document title can create nearly one bucket per record. Cap visible buckets and expose a separate search within a high-cardinality facet when needed. Distributed top buckets can have approximate counts if shards truncate their local candidates. When exact counts drive a legal or operational decision, use a compatible exact aggregation path and report its extra cost. Shard failures must make counts incomplete rather than silently authoritative.
Test facets alongside ranking
Create judged searches with multiple document types, one restricted record, duplicate chunks and an old revision. Verify that results, counts and selected filters all share the same authorized snapshot. A user selecting a facet should see a result set that equals the stated count under the same query, subject to pagination and any explicit approximate-count label. Do not compute counts from only the top-K ranked hits unless the UI says top-K: that measures the preview window, not all eligible matches. Stable pagination keeps the same filter and snapshot across pages.
Worked contract
from collections import Counter
from dataclasses import dataclass
@dataclass(frozen=True)
class EvidenceRecord:
document_id: str
passage_id: str
tenant: str
kind: str
current: bool
def document_facets(records: list[EvidenceRecord], tenant: str,
matching_passages: set[str]) -> dict[str, int]:
visible_documents = {}
for record in records:
if record.tenant != tenant or not record.current:
continue
if record.passage_id not in matching_passages:
continue
visible_documents[record.document_id] = record.kind
return dict(Counter(visible_documents.values()))
records = [
EvidenceRecord("manual-p47", "m47-a", "north", "manual", True),
EvidenceRecord("manual-p47", "m47-b", "north", "manual", True),
EvidenceRecord("incident-83", "i83-a", "north", "incident", True),
EvidenceRecord("private-12", "p12-a", "south", "incident", True),
EvidenceRecord("retired-4", "r4-a", "north", "manual", False),
]
counts = document_facets(records, "north", {r.passage_id for r in records})
assert counts == {"manual": 1, "incident": 1}Performance and operating cost
For P matched passage records, this demonstration takes O(P) time and O(D) space for D distinct visible documents. A production index can aggregate metadata without loading every passage, but exact distinct-document counts and high-cardinality fields can consume memory and network work across shards. Count only after authorization. The example's tenant check is a teaching stand-in; a real service must also enforce per-document access and current revocation state before the bucket stage.
Common Mistakes
- Counting chunks while labeling the number as documents.
- Computing facet buckets before access filtering.
- Treating counts from the top-K preview as all query matches.
- Showing exact-looking counts after a shard timed out or returned truncated buckets.
Validation drill
Index 89 passages from 23 documents with two revisions and four restricted records. Compare facet counts with an independently authorized document list, then remove a shard and confirm the UI marks counts incomplete.
