# How to run multiple geo\_distance filter to get result for each filter separately?

**URL:** <https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286>\
**Category:** Elasticsearch\
**Created:** [June 6, 2023, 6:57am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286 "2023-06-06T06:57:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maulik\_trapasiya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maulik_trapasiya/32/121816_2.png) [@maulik\_trapasiya](https://discuss.elastic.co/u/maulik_trapasiya)\
**Post date:** [June 6, 2023, 6:57am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286/1 "2023-06-06T06:57:25Z")

</div>

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:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [June 6, 2023, 7:23am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286/2 "2023-06-06T07:23:52Z")

</div>

Hi.  
Either use the [multi-search api](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html) to issue two searches in one request or use a single request with a [filters aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/8.8/search-aggregations-bucket-filters-aggregation.html) to include the two geo distance filters and use a top\_hits aggregation under that to group the results

---

<div class="post-metadata">

**Author:** ![maulik\_trapasiya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maulik_trapasiya/32/121816_2.png) [@maulik\_trapasiya](https://discuss.elastic.co/u/maulik_trapasiya)\
**Post date:** [June 7, 2023, 3:46am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286/3 "2023-06-07T03:46:52Z")

</div>

@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

```auto
{
  "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?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [June 7, 2023, 7:20am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286/4 "2023-06-07T07:20:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2023, 7:20am UTC](https://discuss.elastic.co/t/how-to-run-multiple-geo-distance-filter-to-get-result-for-each-filter-separately/335286/5 "2023-07-05T07:20:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
