title: Spring Data Elastic Search - Sort Geo Locations by distance
body: <p>Given a Geo location point I am trying to find some sites within 10 Km and sort it by the nearest to the location given.</p>
  
  <p>I managed to return the list of locations within 10km but when I try to sort it I get exceptions:</p>
  
  <p>I am using the following versions of:</p>
  
  <pre><code>&lt;properties&gt;
          &lt;commonscollections&gt;3.2.1&lt;/commonscollections&gt;
          &lt;commonslang&gt;2.6&lt;/commonslang&gt;
          &lt;spring.data.elasticsearch&gt;1.0.0.BUILD-SNAPSHOT&lt;/spring.data.elasticsearch&gt;
          &lt;springversion&gt;3.2.5.RELEASE&lt;/springversion&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p>The java code as follow:</p>
  
  <pre><code>public List&lt;SiteResource&gt; findByGeoLocation(Double longitude, Double latitude, String channelKey, String distance) {
  
          if(StringUtils.isEmpty(distance)){
              distance = defaultRadius;           
          }       
          GeoPoint location = new GeoPoint(latitude, longitude);
          CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("location").within(location, distance+"km"));
  
          criteriaQuery.addIndices(channelKey);
          criteriaQuery.addTypes("site");
  
          criteriaQuery.addSort(new Sort(Sort.Direction.ASC, "location"));
          List&lt;SiteResource&gt; sites = esTemplate.queryForList(criteriaQuery, com.company.domain.site.SiteResource.class);
          return sites;
      }
  </code></pre>
  
  <p>If I remove the line:</p>
  
  <pre><code>criteriaQuery.addSort(new Sort(Sort.Direction.ASC, "location"));
  </code></pre>
  
  <p>then the code will returns all the locations, the problem is with the sorting</p>
  
  <p>The error I am getting from elasticsearch log:</p>
  
  <pre><code>[2014-03-20 13:37:02,720][DEBUG][action.search.type       ] [Smuggler II] [210a9696a28545f2bbeecf88d64fbad8_20140318_153743][4], node[dB9dCIXMRZGmdUExquMWlQ], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@71b978fe] lastShard [true]
  org.elasticsearch.search.SearchParseException: [210a9696a28545f2bbeecf88d64fbad8_20140318_153743][4]: query[ConstantScore(*:*)],from[0],size[-1]: Parse Failure [Failed to parse source [{"from":0,"query":{"match_all":{}},"post_filter":{"geo_distance":{"location":[-2.217753,53.432703],"distance":"10km"}},"sort":[{"location":{"order":"asc"}}]}]]
      at org.elasticsearch.search.SearchService.parseSource(SearchService.java:595)
      at org.elasticsearch.search.SearchService.createContext(SearchService.java:498)
      at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:472)
      at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:178)
      at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:168)
      at org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:85)
      at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:216)
      at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:203)
      at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:186)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:701)
  Caused by: org.elasticsearch.ElasticsearchIllegalArgumentException: can't sort on geo_point field without using specific sorting feature, like geo_distance
      at org.elasticsearch.index.fielddata.plain.AbstractGeoPointIndexFieldData.comparatorSource(AbstractGeoPointIndexFieldData.java:142)
      at org.elasticsearch.search.sort.SortParseElement.addSortField(SortParseElement.java:222)
      at org.elasticsearch.search.sort.SortParseElement.addCompoundSortField(SortParseElement.java:172)
      at org.elasticsearch.search.sort.SortParseElement.parse(SortParseElement.java:80)
      at org.elasticsearch.search.SearchService.parseSource(SearchService.java:583)
      ... 11 more
  [2014-03-20 13:37:02,722][DEBUG][action.search.type       ] [Smuggler II] All shards failed for phase: [dfs]
  </code></pre>
  
  <p>If someone tell me if it is possible to sort the locations by nearest to me I very much appreciate it.</p>
  
  <p>Many Thanks in advance</p>