# Sorting by geolocation outside of mapping constraints

**URL:** <https://discuss.elastic.co/t/sorting-by-geolocation-outside-of-mapping-constraints/13122>\
**Category:** Elasticsearch\
**Created:** [August 8, 2013, 7:02am UTC](https://discuss.elastic.co/t/sorting-by-geolocation-outside-of-mapping-constraints/13122 "2013-08-08T07:02:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lawrence\_cooke](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Lawrence\_cooke](https://discuss.elastic.co/u/Lawrence_cooke)\
**Post date:** [August 8, 2013, 7:02am UTC](https://discuss.elastic.co/t/sorting-by-geolocation-outside-of-mapping-constraints/13122/1 "2013-08-08T07:02:05Z")

</div>

I have an elasticsearch db of postcode data.

for fast searching I have used both \_type by country and also added routing  
so I can search a subset of data within 1 shard, rather than all shards.

the mapping looks like this:

> <https://gist.github.com/bugcafe/c7d6c6319d06d5ef099f>

The important part of this mapping is "location" which I am setting to  
"geo\_point" for each country mapping

When querying, I am using both \_type and routing to search with

eg.

[http://localhost:9200/us/\_search?routing=us](http://localhost:9200/us/_search?routing=us)

This gives me a much smaller subset of data on which to sort by distance

query looks like this:

> <https://gist.github.com/bugcafe/5496e43708839f249dbb>

Note that in the sort section it is in the format of the location is  
"country.locaton" keeping with the mapping set up earlier.

But what happens if I need to search over the whole DB and not just 1  
country? how do I sort by location when the country is unknown and each doc  
has a \_type = countrycode?

In the sort section:

"sort": [{  
"\_geo\_distance": {  
"au.location": [146.35,  
-41.16667],  
"order": "asc",  
"unit": "km"  
}  
}],

What should the "au.location" look like when I do not know it is "au"?

Is it possible to search the results and sort them by location in this  
case? or is there a way to do it better that would allow the results to be  
sorted by location regardless of knowing the mapping "name"?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:22am UTC](https://discuss.elastic.co/t/sorting-by-geolocation-outside-of-mapping-constraints/13122/2 "2017-07-06T02:22:10Z")

</div>

Closed on July 6, 2017, 2:22am UTC


