# Get Nearest Unique Locations

**URL:** <https://discuss.elastic.co/t/get-nearest-unique-locations/129008>\
**Category:** Elasticsearch\
**Created:** [April 22, 2018, 2:12pm UTC](https://discuss.elastic.co/t/get-nearest-unique-locations/129008 "2018-04-22T14:12:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)\
**Post date:** [April 22, 2018, 2:12pm UTC](https://discuss.elastic.co/t/get-nearest-unique-locations/129008/1 "2018-04-22T14:12:41Z")

</div>

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

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

```auto

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.**

---

<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:** [May 20, 2018, 2:12pm UTC](https://discuss.elastic.co/t/get-nearest-unique-locations/129008/2 "2018-05-20T14:12:57Z")

</div>

Closed on May 20, 2018, 2:12pm UTC

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