@@ -158,26 +158,6 @@ def convert_nm_to_ellipse_units(distance: float, units: str) -> float:
158
158
# NB. assume "majmin_m" if any others
159
159
return distance * 1852
160
160
161
- def get_field_type (elastic_hosts : str , headers : Optional [str ], params : Dict [str , Any ], field : str , idx : str ) -> str :
162
- user = params .get ("user" )
163
- x_opaque_id = params .get ("x-opaque-id" )
164
- es = Elasticsearch (
165
- elastic_hosts .split ("," ),
166
- verify_certs = False ,
167
- timeout = 900 ,
168
- headers = get_es_headers (headers , user , x_opaque_id ),
169
- )
170
- if idx .find ("*:" ) != - 1 :
171
- idx = idx [idx .find ("*:" )+ 2 :] # when you query for mappings if it is cross cluster you don't get a mapping
172
- mappings = es .indices .get_field_mapping (fields = field , index = idx )
173
- # {'foot_prints': {'mappings': {'foot_print': {'full_name': 'foot_print', 'mapping': {'foot_print': {'type': 'geo_shape'}}}}}}
174
- index = list (mappings .keys ())[0 ] # if index is my_index* it comes back as my_index
175
- field_parts = field .split ("." )
176
- try :
177
- return mappings [index ]['mappings' ][field ]['mapping' ][field_parts [- 1 ]]['type' ] # handles 'geo_center' or a nested object {signal:{geo:{location:{}}}}
178
- except AttributeError :
179
- return mappings [index ]['mappings' ][field ]['mapping' ][field ]['type' ] # handles literal string with periods 'signal.geo.location'
180
-
181
161
def get_search_base (
182
162
elastic_hosts : str ,
183
163
headers : Optional [str ],
@@ -271,21 +251,6 @@ def get_search_base(
271
251
272
252
return base_s
273
253
274
- def handle_range_or_exists_filters (filter_input : Dict [Any , Any ]) -> Dict [str , Any ]:
275
- """
276
- `range` and `exists` filters can appear either directly under
277
- `filter[]` or under `filter[].query` depending on the version
278
- of Kibana, the former being the old way, so they need special
279
- handling for backward compatibility.
280
- """
281
- filter_type = filter_input .get ("meta" ).get ("type" ) # "range" or "exists"
282
-
283
- # Handle old query structure for backward compatibility
284
- if filter_input .get (filter_type ) is not None :
285
- return {filter_type : filter_input .get (filter_type )}
286
-
287
- return filter_input .get ("query" )
288
-
289
254
def build_dsl_filter (filter_inputs ) -> Optional [Dict [str , Any ]]:
290
255
"""
291
256
@@ -309,78 +274,25 @@ def build_dsl_filter(filter_inputs) -> Optional[Dict[str, Any]]:
309
274
f .get ("geo_shape" ) or
310
275
f .get ("geo_distance" )
311
276
)
312
-
313
- # Handle spatial filters
314
- if is_spatial_filter :
315
- if f .get ("geo_polygon" ):
316
- geo_polygon_dict = {"geo_polygon" : f .get ("geo_polygon" )}
317
- if f .get ("meta" ).get ("negate" ):
318
- filter_dict ["must_not" ].append (geo_polygon_dict )
319
- else :
320
- filter_dict ["filter" ].append (geo_polygon_dict )
321
- elif f .get ("geo_bounding_box" ):
322
- geo_bbox_dict = {"geo_bounding_box" : f .get ("geo_bounding_box" )}
323
- if f .get ("meta" ).get ("negate" ):
324
- filter_dict ["must_not" ].append (geo_bbox_dict )
325
- else :
326
- filter_dict ["filter" ].append (geo_bbox_dict )
327
- elif f .get ("geo_shape" ):
328
- geo_bbox_dict = {"geo_shape" : f .get ("geo_shape" )}
329
- if f .get ("meta" ).get ("negate" ):
330
- filter_dict ["must_not" ].append (geo_bbox_dict )
331
- else :
332
- filter_dict ["filter" ].append (geo_bbox_dict )
333
- elif f .get ("geo_distance" ):
334
- geo_bbox_dict = {"geo_distance" : f .get ("geo_distance" )}
335
- if f .get ("meta" ).get ("negate" ):
336
- filter_dict ["must_not" ].append (geo_bbox_dict )
337
- else :
338
- filter_dict ["filter" ].append (geo_bbox_dict )
339
- elif f .get ("query" ):
340
- if f .get ("meta" ).get ("negate" ):
341
- filter_dict ["must_not" ].append (f .get ("query" ))
342
- else :
343
- filter_dict ["filter" ].append (f .get ("query" ))
344
- else :
345
- raise ValueError ("unsupported spatial_filter {}" .format (f )) # pylint: disable=C0209
346
-
347
- # Handle phrase matching
348
- elif f .get ("meta" ).get ("type" ) in ("phrase" , "phrases" , "bool" ):
277
+ if f .get ("query" , None ):
349
278
if f .get ("meta" ).get ("negate" ):
350
279
filter_dict ["must_not" ].append (f .get ("query" ))
351
280
else :
352
281
filter_dict ["filter" ].append (f .get ("query" ))
353
-
354
- elif f .get ("meta" ).get ("type" ) in ("range" , "exists" ):
355
- if f .get ("meta" ).get ("negate" ):
356
- filter_dict ["must_not" ].append (handle_range_or_exists_filters (f ))
357
- else :
358
- filter_dict ["filter" ].append (handle_range_or_exists_filters (f ))
359
-
360
- elif f .get ("meta" , {}).get ("type" ) == "custom" and f .get ("meta" , {}).get ("key" ) is not None :
361
- filter_key = f .get ("meta" , {}).get ("key" )
362
- if f .get ("meta" , {}).get ("negate" ):
363
- if filter_key == "query" :
364
- filt_index = list (f .get (filter_key ))[0 ]
365
- filter_dict ["must_not" ].append ({filt_index : f .get (filter_key ).get (filt_index )})
366
- else :
367
- filter_dict ["must_not" ].append ({filter_key : f .get (filter_key )})
368
- else :
369
- if filter_key == "query" :
370
- filt_index = list (f .get (filter_key ))[0 ]
371
- filter_dict ["must_not" ].append ({filt_index : f .get (filter_key ).get (filt_index )})
372
- else :
373
- filter_dict ["filter" ].append ({filter_key : f .get (filter_key )})
374
-
375
282
else :
376
- # Here we handle filters that don't send a type (this happens when controls send filters)
377
- # example filters[{"meta":{"index":"11503c28-7d88-4f9a-946b-2997a5ea64cf","key":"name"},"query":{"match_phrase":{"name":"word_5"}}}]
378
- if f .get ("meta" , {}).get ("negate" ):
379
- filter_dict ["must_not" ].append (f .get ("query" ))
283
+ if not is_spatial_filter :
284
+ filt_type = f .get ("meta" ).get ("type" )
285
+ if f .get ("meta" ).get ("negate" ):
286
+ filter_dict ["must_not" ].append ({filt_type : f .get (filt_type )})
287
+ else :
288
+ filter_dict ["filter" ].append ({filt_type : f .get (filt_type )})
380
289
else :
381
- filter_dict ["filter" ].append (f .get ("query" ))
382
- # raise ValueError("unsupported filter type {}".format(f.get("meta").get("type"))) # pylint: disable=C0209
383
-
290
+ for geo_type in ["geo_polygon" , "geo_bounding_box" , "geo_shape" , "geo_distance" ]:
291
+ if f .get (geo_type , None ):
292
+ if f .get ("meta" ).get ("negate" ):
293
+ filter_dict ["must_not" ].append ({geo_type : f .get (geo_type )})
294
+ else :
295
+ filter_dict ["filter" ].append ({geo_type : f .get (geo_type )})
384
296
logger .info ("Filter output %s" , filter_dict )
385
297
return filter_dict
386
298
@@ -449,32 +361,6 @@ def parse_duration_interval(interval):
449
361
kwargs [key ] = int (interval [0 :len (interval )- 1 ])
450
362
return relativedelta (** kwargs )
451
363
452
- def convert (response , category_formatter = str ):
453
- """
454
-
455
- :param response:
456
- :return:
457
- """
458
- if hasattr (response .aggregations , "categories" ):
459
- for category in response .aggregations .categories :
460
- for bucket in category .grids :
461
- x , y = lnglat_to_meters (
462
- bucket .centroid .location .lon , bucket .centroid .location .lat
463
- )
464
- yield {
465
- "lon" : bucket .centroid .location .lon ,
466
- "lat" : bucket .centroid .location .lat ,
467
- "x" : x ,
468
- "y" : y ,
469
- "c" : bucket .centroid .count ,
470
- "t" : category_formatter (category .key ),
471
- }
472
- else :
473
- for bucket in response .aggregations .grids :
474
- lon = bucket .centroid .location .lon
475
- lat = bucket .centroid .location .lat
476
- x , y = lnglat_to_meters (lon , lat )
477
- yield {"lon" : lon , "lat" : lat , "x" : x , "y" : y , "c" : bucket .centroid .count }
478
364
479
365
def convert_composite (response , categorical , filter_buckets , histogram_interval , category_type , category_format ):
480
366
if categorical and filter_buckets is False :
@@ -586,20 +472,6 @@ def get_nested_field_from_hit(hit, field_parts: List[str], default=None):
586
472
587
473
raise ValueError ("field must be provided" )
588
474
589
- def chunk_iter (iterable , chunk_size ):
590
- chunks = [None ] * chunk_size
591
- i = - 1
592
- for i , v in enumerate (iterable ):
593
- idx = i % chunk_size
594
- if idx == 0 and i > 0 :
595
- i = - 1
596
- yield (True , chunks )
597
- chunks [idx ] = v
598
-
599
- if i >= 0 :
600
- last_written_idx = i % chunk_size
601
- yield (False , chunks [0 :last_written_idx + 1 ])
602
-
603
475
def bucket_noop (bucket , search ):
604
476
# pylint: disable=unused-argument
605
477
return bucket
0 commit comments