# API: Need exact classes/code that will sort by geoDistance

**URL:** https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074
**Category:** Elasticsearch
**Created:** [December 11, 2017, 11:12am UTC](https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074 "2017-12-11T11:12:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)
#### Post date: [December 11, 2017, 11:12am UTC](https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074/1 "2017-12-11T11:12:53Z")

</div>

My query is:

> {  
> "query": {  
> //Some query here  
> },  
> "sort": [{  
> "\_geo\_distance": {  
> "location": {  
> "lat": 49.8693846,  
> "lon": -99.9405093  
> },  
> "order": "asc",  
> "unit": "m"  
> }  
> }]  
> }

**I need the code/hints which will let me form this sort at "query" level, programatically in Java.**  
Please quote any classes that i can use.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 11, 2017, 2:27pm UTC](https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074/2 "2017-12-11T14:27:57Z")

</div>

On another project, I wrote something like:

```auto
SearchRequest request = new SearchRequest(banoIndexName)
 .source(new SearchSourceBuilder()
 .sort(new GeoDistanceSortBuilder("location", lat, lon).sortMode(SortMode.AVG))
 );

```

HTH

---

<div class="post-metadata">

### Author: ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)
#### Post date: [December 12, 2017, 6:33am UTC](https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074/3 "2017-12-12T06:33:25Z")

</div>

Thanks !

---

<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: [January 9, 2018, 6:33am UTC](https://discuss.elastic.co/t/api-need-exact-classes-code-that-will-sort-by-geodistance/111074/4 "2018-01-09T06:33:46Z")

</div>

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