From 8107b7d209974184eeac1c4324c26a3711c33de0 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Mon, 30 Mar 2026 14:13:10 -0700 Subject: [PATCH] Fix: break autocomplete frequency ties alphabetically for stable output Equal-frequency words were non-deterministically ordered; sort key is now (-count, word) so ties resolve alphabetically. Co-Authored-By: Claude Sonnet 4.6 --- src/documents/search/_backend.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py index e3722a017a..9969f9cfbf 100644 --- a/src/documents/search/_backend.py +++ b/src/documents/search/_backend.py @@ -538,11 +538,11 @@ class TantivyBackend: if "autocomplete_word" in doc_dict: word_counts.update(doc_dict["autocomplete_word"]) - # Filter to prefix matches, then sort by document frequency descending - # so the most-used matching word comes first. + # Filter to prefix matches, sort by document frequency descending; + # break ties alphabetically for stable, deterministic output. matches = sorted( (w for w in word_counts if w.startswith(normalized_term)), - key=lambda w: -word_counts[w], + key=lambda w: (-word_counts[w], w), ) return matches[:limit]