From 4f84282ef3f47017451aea9cb30c7e08b2191fb1 Mon Sep 17 00:00:00 2001 From: Trenton Holmes <797416+stumpylog@users.noreply.github.com> Date: Thu, 2 Apr 2026 18:38:10 -0700 Subject: [PATCH] fix: create notes SnippetGenerator once per search, not per hit --- src/documents/search/_backend.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py index a1bff8a9f..a15a9370b 100644 --- a/src/documents/search/_backend.py +++ b/src/documents/search/_backend.py @@ -518,6 +518,7 @@ class TantivyBackend: # Build result hits with highlights hits: list[SearchHit] = [] snippet_generator = None + notes_snippet_generator = None for rank, (doc_address, score) in enumerate(page_hits, start=offset + 1): # Get the actual document from the searcher using the doc address @@ -544,13 +545,16 @@ class TantivyBackend: # Try notes highlights if "notes" in doc_dict: - notes_generator = tantivy.SnippetGenerator.create( - searcher, - final_query, - self._schema, - "notes", + if notes_snippet_generator is None: + notes_snippet_generator = tantivy.SnippetGenerator.create( + searcher, + final_query, + self._schema, + "notes", + ) + notes_snippet = notes_snippet_generator.snippet_from_doc( + actual_doc, ) - notes_snippet = notes_generator.snippet_from_doc(actual_doc) if notes_snippet: highlights["notes"] = str(notes_snippet)