22
22
except ImportError :
23
23
pass
24
24
25
- from indigo import Indigo # type: ignore
25
+ from indigo import Indigo , IndigoObject # type: ignore
26
26
27
27
from bingo_elastic .model .record import (
28
28
IndigoRecord ,
@@ -105,6 +105,8 @@ def get_client(
105
105
"sub_fingerprint" : {"type" : "keyword" , "similarity" : "boolean" },
106
106
"sub_fingerprint_len" : {"type" : "integer" },
107
107
"cmf" : {"type" : "binary" },
108
+ "hash" : {"type" : "unsigned_long" },
109
+ "has_error" : {"type" : "integer" },
108
110
}
109
111
}
110
112
}
@@ -151,13 +153,16 @@ def prepare(
151
153
152
154
153
155
def response_to_records (
154
- res , index_name , postprocess_actions
156
+ res : dict ,
157
+ index_name : str ,
158
+ postprocess_actions : PostprocessType = None ,
159
+ indigo_session : Indigo = None ,
160
+ options : str = "" ,
155
161
) -> Generator [IndigoRecord , None , None ]:
156
- indigo_session = Indigo ()
157
162
for el_response in res .get ("hits" , {}).get ("hits" , []):
158
163
record = get_record_by_index (el_response , index_name )
159
- for action_fn in postprocess_actions :
160
- record = action_fn (record , indigo_session ) # type: ignore
164
+ for action_fn in postprocess_actions : # type: ignore
165
+ record = action_fn (record , indigo_session , options ) # type: ignore
161
166
if not record :
162
167
continue
163
168
yield record
@@ -218,27 +223,25 @@ async def index_records(self, records: Generator, chunk_size: int = 500):
218
223
219
224
async def filter (
220
225
self ,
221
- similarity : Union [BaseMatch ] = None ,
222
- exact : IndigoRecord = None ,
223
- substructure : IndigoRecord = None ,
224
- limit = 10 ,
226
+ query_subject : Union [BaseMatch , IndigoObject , IndigoRecord ] = None ,
227
+ indigo_session : Indigo = None ,
228
+ limit : int = 10 ,
229
+ options : str = "" ,
225
230
** kwargs ,
226
231
) -> AsyncGenerator [IndigoRecord , None ]:
227
232
228
233
# actions needed to be called on elastic_search result
229
234
postprocess_actions : PostprocessType = []
230
235
231
236
query = compile_query (
232
- similarity = similarity ,
233
- exact = exact ,
234
- substructure = substructure ,
237
+ query_subject = query_subject ,
235
238
limit = limit ,
236
239
postprocess_actions = postprocess_actions ,
237
240
** kwargs ,
238
241
)
239
242
res = await self .el_client .search (index = self .index_name , body = query )
240
243
for record in response_to_records (
241
- res , self .index_name , postprocess_actions
244
+ res , self .index_name , postprocess_actions , indigo_session , options
242
245
):
243
246
yield record
244
247
@@ -313,40 +316,33 @@ def delete_all_records(self):
313
316
314
317
def filter (
315
318
self ,
316
- similarity : Union [BaseMatch ] = None ,
317
- exact : IndigoRecord = None ,
318
- substructure : IndigoRecord = None ,
319
- limit = 10 ,
319
+ query_subject : Union [BaseMatch , IndigoObject , IndigoRecord ] = None ,
320
+ indigo_session : Indigo = None ,
321
+ limit : int = 10 ,
322
+ options : str = "" ,
320
323
** kwargs ,
321
324
) -> Generator [IndigoRecord , None , None ]:
322
325
323
326
# actions needed to be called on elastic_search result
324
327
postprocess_actions : PostprocessType = []
325
-
326
328
query = compile_query (
327
- similarity = similarity ,
328
- exact = exact ,
329
- substructure = substructure ,
329
+ query_subject = query_subject ,
330
330
limit = limit ,
331
331
postprocess_actions = postprocess_actions ,
332
332
** kwargs ,
333
333
)
334
334
res = self .el_client .search (index = self .index_name , body = query )
335
335
yield from response_to_records (
336
- res , self .index_name , postprocess_actions
336
+ res , self .index_name , postprocess_actions , indigo_session , options
337
337
)
338
338
339
339
340
- # pylint: disable=too-many-arguments
341
340
def compile_query (
342
- similarity : BaseMatch = None ,
343
- exact : IndigoRecord = None ,
344
- substructure : IndigoRecord = None ,
341
+ query_subject : Union [BaseMatch , IndigoObject , IndigoRecord ] = None ,
345
342
limit : int = 10 ,
346
343
postprocess_actions : PostprocessType = None ,
347
344
** kwargs ,
348
345
) -> Dict :
349
-
350
346
query = {
351
347
"size" : limit ,
352
348
"_source" : {
@@ -359,17 +355,15 @@ def compile_query(
359
355
],
360
356
},
361
357
}
362
- if similarity and substructure :
363
- raise AttributeError (
364
- "similarity and substructure search is not supported"
365
- )
366
358
367
- if similarity :
368
- similarity .compile (query , postprocess_actions )
369
- elif exact :
370
- query_factory ("exact" , exact ).compile (query , postprocess_actions )
371
- elif substructure :
372
- query_factory ("substructure" , substructure ).compile (
359
+ if isinstance (query_subject , BaseMatch ):
360
+ query_subject .compile (query , postprocess_actions )
361
+ elif isinstance (query_subject , IndigoRecord ):
362
+ query_factory ("exact" , query_subject ).compile (
363
+ query , postprocess_actions
364
+ )
365
+ elif isinstance (query_subject , IndigoObject ):
366
+ query_factory ("substructure" , query_subject ).compile (
373
367
query , postprocess_actions
374
368
)
375
369
0 commit comments