cruk-searchkit
Version:
Set of overriden and new searchkit components designed specifically for Cancer Research UK
60 lines (52 loc) • 1.36 kB
JSX
import {
FilterBasedAccessor,
ObjectState,
FilteredQuery,
BoolMust,
CardinalityMetric,
FilterBucket
} from "searchkit"
const isUndefined = require("lodash/isUndefined")
export class CRUKSearchkitLocationAccessor extends FilterBasedAccessor {
constructor(key, options){
super(key, options.id)
this.options = options
this.state = new ObjectState({})
}
buildSharedQuery(query) {
const val = this.state.getValue()
this.options.updateParentState(this.getQueryObject())
if(Object.keys(val).length > 0){
let filter = {
geo_distance: {
distance: this.options.resultRadius || "5km",
location: {}
}
}
filter.geo_distance[this.options.field].lat = parseFloat(val.lat);
filter.geo_distance[this.options.field].lon = parseFloat(val.lng);
let geoFilter = {
bool: {
filter: filter
}
};
query = query.addQuery(geoFilter);
}
return query
}
getDocCount(){
return this.getAggregations([this.key, "doc_count"], 0)
}
buildOwnQuery(query) {
let filters = query.getFilters()
if (!this.state.getValue()){
if (filters) filters = BoolMust([filters, this.filter])
else filters = this.filter
}
return query
.setAggs(FilterBucket(
this.key,
filters
))
}
}