Get Nearest Unique Locations

I need to fetch nearest distinct locations from my index
Here is my mapping

{
	"properties": {
		"id": {
			"type": "keyword"
		},
	     "location_name": {
                  "type": "keyword"
              },
             "post_location": {
                   "type": "geo_point"
             },
	}
}

We are saving so many posts on same location just like
Suppose
"The White House" contains 100 posts
"Washington National Cathedral." contains 50 posts
"Jefferson Memorial" contains 20 posts
etc

I need to get only unique locations rather than their posts. but order of the locations should be according my geo locations.

I am applying following query


POST /posts/post/_search?size=0
{
   "sort": [
              {
                "_geo_distance": {
                  "post_location": [
                    46,
                    24
                  ],
                  "order": "asc",
                  "unit": "km"
                }
              }
            ], 
  "aggs": {
    "top_tags": {
      "terms": {
        "field": "location_name",
        "size": 100
       
      },
      "aggs": {
        "top_sales_hits": {
          "top_hits": {
            "sort": [
              {
                "_geo_distance": {
                  "post_location": [
                    46,
                    24
                  ],
                  "order": "asc",
                  "unit": "km"
                }
              }
            ],
            "_source": false,
            "size": 1
          }
        }
      }
    }
  }
}

Good thing is that it gives my unique locations in bucket
but bad thing is order of the locations is not according to my geo location.

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