diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 865b65ffa9..ae421b0080 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -987,9 +987,10 @@ class DB: return self.fts.dirty_book(book_id, *fmts) def fts_search(self, - fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, + fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result ): - yield from self.fts.search(fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text,) + yield from self.fts.search( + fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result) def shutdown_fts(self): if self.fts_enabled: diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 6f665f83b4..243d4c026f 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -633,6 +633,7 @@ class Cache: restrict_to_book_ids=None, return_text=True, result_type=tuple, + process_each_result=None, ): return result_type(self.backend.fts_search( fts_engine_query, @@ -642,6 +643,7 @@ class Cache: snippet_size=snippet_size, return_text=return_text, restrict_to_book_ids=restrict_to_book_ids, + process_each_result=process_each_result, )) # }}} diff --git a/src/calibre/db/fts/connect.py b/src/calibre/db/fts/connect.py index 90faebcc86..9ea3d5c35a 100644 --- a/src/calibre/db/fts/connect.py +++ b/src/calibre/db/fts/connect.py @@ -150,7 +150,7 @@ class FTS: def search(self, fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, - return_text=True, + return_text=True, process_each_result=None ): if restrict_to_book_ids is not None and not restrict_to_book_ids: return @@ -184,12 +184,15 @@ class FTS: query += f'; DROP TABLE temp.{temp_table_name}' try: for record in conn.execute(query, tuple(data)): - ret = yield { + result = { 'id': record[0], 'book_id': record[1], 'format': record[2], 'text': record[3] if return_text else '', } + if process_each_result is not None: + result = process_each_result(result) + ret = yield result if ret is True: break except apsw.SQLError as e: