From 27d1b03c3c6f3fd53bc979ef2e1ba835ed8a12c6 Mon Sep 17 00:00:00 2001 From: Tris Forster Date: Thu, 14 May 2026 11:02:55 +1000 Subject: [PATCH] Added fuzzy matching --- app/library/indexer/whoosh.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/app/library/indexer/whoosh.py b/app/library/indexer/whoosh.py index fad6cd7..dde065f 100644 --- a/app/library/indexer/whoosh.py +++ b/app/library/indexer/whoosh.py @@ -3,7 +3,7 @@ from whoosh.analysis import StemmingAnalyzer, CharsetFilter from whoosh.support.charset import accent_map from whoosh.fields import Schema, TEXT, KEYWORD, NUMERIC from whoosh.qparser import QueryParser -from whoosh.query import Term, NullQuery +from whoosh.query import Term, NullQuery, Prefix, FuzzyTerm from typing import Any @@ -61,20 +61,27 @@ def search( pagesize: int = 20, ) -> tuple[list[dict], dict[str, Any]]: meta = {} + query = query.lower() qp = QueryParser("text", schema=schema) - q = qp.parse(query.lower()) - meta["query"] = str(q) + q = qp.parse(query) - terms = NullQuery + limit = NullQuery for c in collections: - terms = terms | Term("collection", c) - q = q & terms + limit |= Term("collection", c) + print(limit) hits = [] ix = get_index() with ix.searcher() as searcher: - results = searcher.search_page(q, page, pagesize) + results = searcher.search_page(q & limit, page, pagesize) + + # if no results, do a prefix search + if results.results.is_empty(): + qp.termclass = Prefix + q = qp.parse(query) + results = searcher.search_page(q & limit, page, pagesize) + for result in results: hits.append( dict( @@ -85,7 +92,7 @@ def search( collection_id=int(result["collection"]), ) ) - + meta["query"] = str(q & limit) meta["total"] = len(results) return hits, meta