77import socket
88import time
99from typing import Optional , Union
10- from ..utils .search_engine import SearchEngine
1110
1211import requests
1312import sentry_sdk
1413from requests .packages .urllib3 .exceptions import InsecureRequestWarning
1514
15+ from ..utils .search_engine import SearchEngine
16+
1617requests .packages .urllib3 .disable_warnings (InsecureRequestWarning )
1718
1819
@@ -249,10 +250,11 @@ def query(
249250 query_index : str = None ,
250251 query_field : str = None ,
251252 query_vector : Union [list , dict ] = None ,
252- response_fields : list = None ,
253+ response_fields : Optional [ list ] = None ,
253254 limit : int = 2 ,
254- filter : str = "" ,
255- with_distance : bool = False ,
255+ filter : Optional [str ] = None ,
256+ with_distance : Optional [bool ] = False ,
257+ facets : Optional [list [dict ]] = None ,
256258 ):
257259 if self ._db is None :
258260 raise Exception ("[ERROR] Please use_db() first!" )
@@ -266,6 +268,15 @@ def query(
266268 "filter" : filter ,
267269 "withDistance" : with_distance ,
268270 }
271+ if facets is not None and len (facets ) > 0 :
272+ aggregate_not_existing = 0
273+ for facet in facets :
274+ if "aggregate" not in facet :
275+ aggregate_not_existing += 1
276+ if aggregate_not_existing > 0 :
277+ raise Exception ("[ERROR] key aggregate is a must in facets!" )
278+ else :
279+ req_data ["facets" ] = facets
269280 if query_text is not None :
270281 req_data ["query" ] = query_text
271282 if query_index is not None :
@@ -280,6 +291,7 @@ def query(
280291 status_code = res .status_code
281292 body = res .json ()
282293 res .close ()
294+ del res
283295 return status_code , body
284296
285297 def get (
@@ -291,6 +303,7 @@ def get(
291303 filter : Optional [str ] = None ,
292304 skip : Optional [int ] = None ,
293305 limit : Optional [int ] = None ,
306+ facets : Optional [list [dict ]] = None ,
294307 ):
295308 if self ._db is None :
296309 raise Exception ("[ERROR] Please use_db() first!" )
@@ -302,26 +315,36 @@ def get(
302315 print (
303316 "[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!"
304317 )
305- if primary_keys == None and ids != None :
318+ if primary_keys is None and ids is not None :
306319 primary_keys = ids
307320
308321 req_data = {"table" : table_name }
309322
310- if response_fields != None :
323+ if response_fields is not None :
311324 req_data ["response" ] = response_fields
312325
313- if primary_keys != None :
326+ if primary_keys is not None :
314327 req_data ["primaryKeys" ] = primary_keys
315328
316- if filter != None :
329+ if filter is not None :
317330 req_data ["filter" ] = filter
318331
319- if skip != None :
332+ if skip is not None :
320333 req_data ["skip" ] = filter
321334
322- if limit != None :
335+ if limit is not None :
323336 req_data ["limit" ] = limit
324337
338+ if facets is not None and len (facets ) > 0 :
339+ aggregate_not_existing = 0
340+ for facet in facets :
341+ if "aggregate" not in facet :
342+ aggregate_not_existing += 1
343+ if aggregate_not_existing > 0 :
344+ raise Exception ("[ERROR] key aggregate is a must in facets!" )
345+ else :
346+ req_data ["facets" ] = facets
347+
325348 req_url = "{}/api/{}/data/get" .format (self ._baseurl , self ._db )
326349 res = requests .post (
327350 url = req_url , data = json .dumps (req_data ), headers = self ._header , verify = False
0 commit comments