diff --git a/src/calibre/db/fts/connect.py b/src/calibre/db/fts/connect.py index af979a9e9b..5512b9b306 100644 --- a/src/calibre/db/fts/connect.py +++ b/src/calibre/db/fts/connect.py @@ -137,31 +137,36 @@ class FTS: def search(self, fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, + return_text=True, ): fts_engine_query = unicode_normalize(fts_engine_query) fts_table = 'books_fts' + ('_stemmed' if use_stemming else '') - text = 'books_text.searchable_text' - if highlight_start is not None and highlight_end is not None: - if snippet_size is not None: - text = f'snippet("{fts_table}", 0, "{highlight_start}", "{highlight_end}", "…", {max(1, min(snippet_size, 64))})' - else: - text = f'highlight("{fts_table}", 0, "{highlight_start}", "{highlight_end}")' - query = 'SELECT {0}.id, {0}.book, {0}.format, {1} FROM {0} ' - query = query.format('books_text', text) + if return_text: + text = 'books_text.searchable_text' + if highlight_start is not None and highlight_end is not None: + if snippet_size is not None: + text = f'snippet("{fts_table}", 0, "{highlight_start}", "{highlight_end}", "…", {max(1, min(snippet_size, 64))})' + else: + text = f'highlight("{fts_table}", 0, "{highlight_start}", "{highlight_end}")' + text = ', ' + text + else: + text = '' + query = 'SELECT {0}.id, {0}.book, {0}.format {1} FROM {0} '.format('books_text', text) query += f' JOIN {fts_table} ON fts_db.books_text.id = {fts_table}.rowid' query += f' WHERE "{fts_table}" MATCH ?' data = [fts_engine_query] query += f' ORDER BY {fts_table}.rank ' conn = self.get_connection() try: - for (rowid, book_id, fmt, text) in conn.execute(query, tuple(data)): + for record in conn.execute(query, tuple(data)): + book_id = record[1] if restrict_to_book_ids is not None and book_id not in restrict_to_book_ids: continue yield { - 'id': rowid, + 'id': record[0], 'book_id': book_id, - 'format': fmt, - 'text': text, + 'format': record[2], + 'text': record[3] if return_text else '', } except apsw.SQLError as e: raise FTSQueryError(fts_engine_query, query, e) from e