From 86ac3ba9f16d1e7bbe55466e5699fd89ec3057a1 Mon Sep 17 00:00:00 2001 From: Trenton Holmes <797416+stumpylog@users.noreply.github.com> Date: Mon, 6 Apr 2026 13:49:23 -0700 Subject: [PATCH] fix: limit global search to 9 IDs and fix more_like_this_ids off-by-one Global search only displays 3 results but was fetching all matching IDs and hydrating them via in_bulk. Now passes limit=9 to search_ids(). more_like_this_ids could return limit-1 results when the original doc appeared in the result set. Now fetches limit+1 and slices after filtering. Co-Authored-By: Claude Opus 4.6 --- src/documents/search/_backend.py | 6 ++++-- src/documents/views.py | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py index 03eff5f21..c6ed11e34 100644 --- a/src/documents/search/_backend.py +++ b/src/documents/search/_backend.py @@ -762,14 +762,16 @@ class TantivyBackend: final_query = self._apply_permission_filter(mlt_query, user) effective_limit = limit if limit is not None else searcher.num_docs - results = searcher.search(final_query, limit=effective_limit) + # Fetch one extra to account for excluding the original document + results = searcher.search(final_query, limit=effective_limit + 1) ids = [] for _score, doc_address in results.hits: result_doc_id = searcher.doc(doc_address).to_dict()["id"][0] if result_doc_id != doc_id: ids.append(result_doc_id) - return ids + + return ids[:limit] if limit is not None else ids def batch_update(self, lock_timeout: float = 30.0) -> WriteBatch: """ diff --git a/src/documents/views.py b/src/documents/views.py index 6c67bd928..1b867bbf4 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -3136,6 +3136,7 @@ class GlobalSearchView(PassUserMixin): query, user=user, search_mode=SearchMode.TEXT, + limit=OBJECT_LIMIT * 3, ) docs_by_id = all_docs.in_bulk(matching_ids) docs = [