@@ -73,35 +73,35 @@ async def init_search_index(self):
7373 async with db .scoped_session (self .session_maker ) as session :
7474 await session .execute (CREATE_SEARCH_INDEX )
7575 await session .commit ()
76- except Exception as e : # pragma: no cover
76+ except Exception as e : # pragma: no cover
7777 logger .error (f"Error initializing search index: { e } " )
7878 raise e
79-
79+
8080 def _prepare_search_term (self , term : str , is_prefix : bool = True ) -> str :
8181 """Prepare a search term for FTS5 query.
82-
82+
8383 Args:
8484 term: The search term to prepare
8585 is_prefix: Whether to add prefix search capability (* suffix)
86-
86+
8787 For FTS5:
8888 - Special characters and phrases need to be quoted
8989 - Terms with spaces or special chars need quotes
9090 """
9191 if "*" in term :
9292 return term
93-
93+
9494 # List of special characters that need quoting (excluding *)
9595 special_chars = ["/" , "-" , "." , " " , "(" , ")" , "[" , "]" , '"' , "'" ]
96-
96+
9797 # Check if term contains any special characters
9898 needs_quotes = any (c in term for c in special_chars )
99-
99+
100100 if needs_quotes :
101101 # If the term already contains quotes, escape them and add a wildcard
102102 term = term .replace ('"' , '""' )
103103 term = f'"{ term } "*'
104-
104+
105105 return term
106106
107107 async def search (
@@ -140,14 +140,15 @@ async def search(
140140 # Handle permalink match search, supports *
141141 if permalink_match :
142142 # Clean and prepare permalink for FTS5 GLOB match
143- permalink_text = self ._prepare_search_term (permalink_match .lower ().strip (), is_prefix = False )
143+ permalink_text = self ._prepare_search_term (
144+ permalink_match .lower ().strip (), is_prefix = False
145+ )
144146 params ["permalink" ] = permalink_text
145147 if "*" in permalink_match :
146148 conditions .append ("permalink GLOB :permalink" )
147- else :
149+ else :
148150 conditions .append ("permalink MATCH :permalink" )
149151
150-
151152 # Handle type filter
152153 if types :
153154 type_list = ", " .join (f"'{ t .value } '" for t in types )
@@ -223,7 +224,9 @@ async def search(
223224
224225 logger .debug (f"Found { len (results )} search results" )
225226 for r in results :
226- logger .debug (f"Search result: type:{ r .type } title: { r .title } permalink: { r .permalink } score: { r .score } " )
227+ logger .debug (
228+ f"Search result: type:{ r .type } title: { r .title } permalink: { r .permalink } score: { r .score } "
229+ )
227230
228231 return results
229232
0 commit comments