Skip to content

fix(search): sort a document that has no title in the reader’s language - #792

Merged
ddeboer merged 1 commit into
mainfrom
fix/sort-key-locale-fallback
Aug 31, 2026
Merged

fix(search): sort a document that has no title in the reader’s language#792
ddeboer merged 1 commit into
mainfrom
fix/sort-key-locale-fallback

Conversation

@ddeboer

@ddeboer ddeboer commented Aug 31, 2026

Copy link
Copy Markdown
Member

Sorting a collection by a localized text field returned it unsorted whenever the
documents were not titled in the reader’s language.

A sortable text field wrote a sort key only for the languages a document
actually carried: a work with an untagged title got name_sort_und and nothing
else, one titled in English got name_sort_en and nothing else. A sort, unlike
a search, names a single key – the query compiler picks the active locale’s.
So orderBy: {field: NAME, direction: DESC} from a Dutch client compiled to
name_sort_nl:desc, on which every document without a Dutch title carries the
empty string. They all tie, and Typesense hands them back in relevance order –
which is exactly what an unsorted result looks like.

Each locale’s sort key now falls back to the document’s first value in locales
order, so a sort key is present whenever the field holds any value in a declared
locale. A collection of untagged titles sorts by title in a Dutch request rather
than not at all; a work titled only in English sorts under its English title
next to the Dutch ones. Ordering across languages is approximate by nature, and
a total order over the titles a reader actually sees beats a partial one over
the tagged few.

The search companions deliberately keep no fallback: a query fans out over every
locale key at once, so a document is already found whichever language the reader
asks in, and copying the value would only inflate the index.

Reported downstream at https://codeberg.org/limburg/lol/issues/158, on a
register whose CreativeWork titles are predominantly untagged.

Changes

  • packages/search/src/project.tsapplyText computes the values per
    declared locale once, and fills each locale’s sort key from the first
    non-empty bucket when its own is empty. Search keys are unchanged.
  • packages/search/test/project.test.ts – a document titled only in English
    sorts under that title in every declared locale, while its search keys stay
    per-locale.
  • packages/search-pipeline/test/registry-extraction.integration.test.ts – the
    projected dataset now also carries label_sort_und, the fallback for a
    request that states no language.
  • docs/reference/search.md – the Locales section states the fallback, why a
    sort needs one where a search does not, and that a sort field is absent only
    when the field is.

- fall back, per locale, to the document’s first declared-locale value when it
  carries no value in that language, so every sort key a query can name is
  populated whenever the field holds anything at all
- leave the search companions per-locale: a query fans out over every locale
  key at once, so only the sort – which names a single key – needs the fallback
- document the rule and the reason under Locales in the search reference
@ddeboer
ddeboer merged commit 1e39d9f into main Aug 31, 2026
4 checks passed
@ddeboer
ddeboer deleted the fix/sort-key-locale-fallback branch August 31, 2026 17:45
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.

1 participant