17
17
from models .dataset import Dataset
18
18
19
19
logger = logging .getLogger (__name__ )
20
- logging .basicConfig (level = logging .INFO ,
21
- format = "%(asctime)s - %(levelname)s - %(message)s" )
20
+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
22
21
logging .getLogger ("lindorm" ).setLevel (logging .WARN )
23
22
24
23
ROUTING_FIELD = "routing_field"
@@ -135,8 +134,7 @@ def delete_by_ids(self, ids: list[str]) -> None:
135
134
self ._client .delete (index = self ._collection_name , id = id , params = params )
136
135
self .refresh ()
137
136
else :
138
- logger .warning (
139
- f"DELETE BY ID: ID { id } does not exist in the index." )
137
+ logger .warning (f"DELETE BY ID: ID { id } does not exist in the index." )
140
138
141
139
def delete (self ) -> None :
142
140
if self ._using_ugc :
@@ -147,8 +145,7 @@ def delete(self) -> None:
147
145
self .refresh ()
148
146
else :
149
147
if self ._client .indices .exists (index = self ._collection_name ):
150
- self ._client .indices .delete (
151
- index = self ._collection_name , params = {"timeout" : 60 })
148
+ self ._client .indices .delete (index = self ._collection_name , params = {"timeout" : 60 })
152
149
logger .info ("Delete index success" )
153
150
else :
154
151
logger .warning (f"Index '{ self ._collection_name } ' does not exist. No deletion performed." )
@@ -171,14 +168,13 @@ def search_by_vector(self, query_vector: list[float], **kwargs: Any) -> list[Doc
171
168
raise ValueError ("All elements in query_vector should be floats" )
172
169
173
170
top_k = kwargs .get ("top_k" , 10 )
174
- query = default_vector_search_query (
175
- query_vector = query_vector , k = top_k , ** kwargs )
171
+ query = default_vector_search_query (query_vector = query_vector , k = top_k , ** kwargs )
176
172
try :
177
173
params = {}
178
174
if self ._using_ugc :
179
175
params ["routing" ] = self ._routing
180
176
response = self ._client .search (index = self ._collection_name , body = query , params = params )
181
- except Exception as e :
177
+ except Exception :
182
178
logger .exception (f"Error executing vector search, query: { query } " )
183
179
raise
184
180
@@ -224,8 +220,7 @@ def search_by_full_text(self, query: str, **kwargs: Any) -> list[Document]:
224
220
routing = routing ,
225
221
routing_field = self ._routing_field ,
226
222
)
227
- response = self ._client .search (
228
- index = self ._collection_name , body = full_text_query )
223
+ response = self ._client .search (index = self ._collection_name , body = full_text_query )
229
224
docs = []
230
225
for hit in response ["hits" ]["hits" ]:
231
226
docs .append (
@@ -243,8 +238,7 @@ def create_collection(self, dimension: int, **kwargs):
243
238
with redis_client .lock (lock_name , timeout = 20 ):
244
239
collection_exist_cache_key = f"vector_indexing_{ self ._collection_name } "
245
240
if redis_client .get (collection_exist_cache_key ):
246
- logger .info (
247
- f"Collection { self ._collection_name } already exists." )
241
+ logger .info (f"Collection { self ._collection_name } already exists." )
248
242
return
249
243
if self ._client .indices .exists (index = self ._collection_name ):
250
244
logger .info (f"{ self ._collection_name .lower ()} already exists." )
@@ -264,13 +258,10 @@ def create_collection(self, dimension: int, **kwargs):
264
258
hnsw_ef_construction = kwargs .pop ("hnsw_ef_construction" , 500 )
265
259
ivfpq_m = kwargs .pop ("ivfpq_m" , dimension )
266
260
nlist = kwargs .pop ("nlist" , 1000 )
267
- centroids_use_hnsw = kwargs .pop (
268
- "centroids_use_hnsw" , True if nlist >= 5000 else False )
261
+ centroids_use_hnsw = kwargs .pop ("centroids_use_hnsw" , True if nlist >= 5000 else False )
269
262
centroids_hnsw_m = kwargs .pop ("centroids_hnsw_m" , 24 )
270
- centroids_hnsw_ef_construct = kwargs .pop (
271
- "centroids_hnsw_ef_construct" , 500 )
272
- centroids_hnsw_ef_search = kwargs .pop (
273
- "centroids_hnsw_ef_search" , 100 )
263
+ centroids_hnsw_ef_construct = kwargs .pop ("centroids_hnsw_ef_construct" , 500 )
264
+ centroids_hnsw_ef_search = kwargs .pop ("centroids_hnsw_ef_search" , 100 )
274
265
mapping = default_text_mapping (
275
266
dimension ,
276
267
method_name ,
@@ -290,8 +281,7 @@ def create_collection(self, dimension: int, **kwargs):
290
281
using_ugc = self ._using_ugc ,
291
282
** kwargs ,
292
283
)
293
- self ._client .indices .create (
294
- index = self ._collection_name .lower (), body = mapping )
284
+ self ._client .indices .create (index = self ._collection_name .lower (), body = mapping )
295
285
redis_client .set (collection_exist_cache_key , 1 , ex = 3600 )
296
286
# logger.info(f"create index success: {self._collection_name}")
297
287
@@ -396,8 +386,7 @@ def default_text_search_query(
396
386
# build complex search_query when either of must/must_not/should/filter is specified
397
387
if must :
398
388
if not isinstance (must , list ):
399
- raise RuntimeError (
400
- f"unexpected [must] clause with { type (filters )} " )
389
+ raise RuntimeError (f"unexpected [must] clause with { type (filters )} " )
401
390
if query_clause not in must :
402
391
must .append (query_clause )
403
392
else :
@@ -407,22 +396,19 @@ def default_text_search_query(
407
396
408
397
if must_not :
409
398
if not isinstance (must_not , list ):
410
- raise RuntimeError (
411
- f"unexpected [must_not] clause with { type (filters )} " )
399
+ raise RuntimeError (f"unexpected [must_not] clause with { type (filters )} " )
412
400
boolean_query ["must_not" ] = must_not
413
401
414
402
if should :
415
403
if not isinstance (should , list ):
416
- raise RuntimeError (
417
- f"unexpected [should] clause with { type (filters )} " )
404
+ raise RuntimeError (f"unexpected [should] clause with { type (filters )} " )
418
405
boolean_query ["should" ] = should
419
406
if minimum_should_match != 0 :
420
407
boolean_query ["minimum_should_match" ] = minimum_should_match
421
408
422
409
if filters :
423
410
if not isinstance (filters , list ):
424
- raise RuntimeError (
425
- f"unexpected [filter] clause with { type (filters )} " )
411
+ raise RuntimeError (f"unexpected [filter] clause with { type (filters )} " )
426
412
boolean_query ["filter" ] = filters
427
413
428
414
search_query = {"size" : k , "query" : {"bool" : boolean_query }}
0 commit comments