I need result documents based on geo_distance query for points A(lat=3,long=101) and B(lat=5,long=102) separately. one result docs I need corresponding to filter A(lat=3,long=101) and other result set I need corresponding to filter B(lat=5,long=102). I am not able to find the correct query for this. I found the query which provides union of results is like this:
{
"query": {
"bool": {
"should": [
{
"geo_distance" : {
"distance" : "1km",
"pin" : {
"lat" : 3,
"lon" : 101
}
}
},
{
"geo_distance" : {
"distance" : "1km",
"pin" : {
"lat" : 5,
"lon" : 102
}
}
}
]
}
}
}
but here my requirement is to get results separately instead of union.
Hi.
Either use the multi-search api to issue two searches in one request or use a single request with a filters aggregation to include the two geo distance filters and use a top_hits aggregation under that to group the results
@Mark_Harwood1 Thanks for providing solution. I am not able figure out how to use top_hits after applying multiple filters. My request query is like
{
"size": 0,
"aggs": {
"messages": {
"filters": {
"filters": {
"g1Filter": {
"geo_distance": {
"distance": "0.1km",
"distance_type": "arc",
"location.latLong": {
"lat": 12.93220,
"lon": 77.6904
}
}
},
"g2Filter": {
"geo_distance": {
"distance": "0.2km",
"distance_type": "arc",
"location.latLong": {
"lat": 12.93220,
"lon": 77.6904
}
}
}
}
}
},
"aggs": {
"top_hits": {
"_source": {
"includes": [
"documentId"
]
},
"size": 10
}
}
}
}
I am not getting documents groupBy g1Filter and g2Filter. please help me in figuring-out how to do this?
Hi Maulik,
I don’t have access to your data so can’t test but if the geo distance clauses work as a query they should also work as a filter. Do these clauses match anything?
As for the top_hits expression - aggregations can be nested so simply place another “aggs” expression inside your “messages” section and add the “top_hits” clause inside that.