import { i18n } from '@osd/i18n' ; import { BucketAggType , IBucketAggConfig } from './bucket_agg_type' ; import { OSD_FIELD_TYPES } from '../../../../common' ; import { BUCKET_TYPES } from './bucket_agg_types' ; import { GeoBoundingBox } from './lib/geo_point' ; import { BaseAggParams } from '../types' ; const defaultBoundingBox = { top_left : { lat : 1 , lon : 1 } , bottom_right : { lat : 0 , lon : 0 } , } ; const defaultPrecision = 2 ; const geohashGridTitle = i18n . translate ( 'data.search.aggs.buckets.geohashGridTitle' , { defaultMessage : 'Geohash' , } ) ; export interface AggParamsGeoHash extends BaseAggParams { field : string ; autoPrecision ? : boolean ; precision ? : number ; useGeocentroid ? : boolean ; isFilteredByCollar ? : boolean ; boundingBox ? : GeoBoundingBox ; } export const getGeoHashBucketAgg = ( ) => new BucketAggType < IBucketAggConfig > ( { name : BUCKET_TYPES . GEOHASH_GRID , title : geohashGridTitle , makeLabel : ( ) => geohashGridTitle , params : [ { name : 'field' , type : 'field' , filterFieldTypes : OSD_FIELD_TYPES . GEO_POINT , } , { name : 'autoPrecision' , default : true , write : ( ) => { } , } , { name : 'precision' , default : defaultPrecision , write ( aggConfig , output ) { output . params . precision = aggConfig . params . precision ; } , } , { name : 'useGeocentroid' , default : true , write : ( ) => { } , } , { name : 'isFilteredByCollar' , default : true , write : ( ) => { } , } , { name : 'boundingBox' , default : null , write : ( ) => { } , } , ] , getRequestAggs ( agg ) { const aggs = [ ] ; const params = agg . params ; if ( params . isFilteredByCollar && agg . getField ( ) ) { aggs . push ( agg . aggConfigs . createAggConfig ( { type : 'filter' , id : 'filter_agg' , enabled : true , params : { geo_bounding_box : { ignore_unmapped : true , [ agg . getField ( ) . name ] : params . boundingBox || defaultBoundingBox , } , } , } as any , { addToAggConfigs : false } ) ) ; } aggs . push ( agg ) ; if ( params . useGeocentroid ) { aggs . push ( agg . aggConfigs . createAggConfig ( { type : 'geo_centroid' , enabled : true , params : { field : agg . getField ( ) , } , } , { addToAggConfigs : false } ) ) ; } return aggs ; } , } ) ;