Search code examples
elasticsearchspring-dataelastic-stackspring-data-elasticsearch

How to Store Distance value to SortValues or Entity


How to Store Distance value to SortValues or Entity using SDE4.0 @Query, and SearchHit

"sort": [
  {
    "_geo_distance" : {
      "codenames.geoLocation" : [
        {
          "lat" : 32.846027,
          "lon" : -96.84987
        }
      ],
      "unit" : "mi",
      "order" : "asc",
    }
  }
]

Solution

  • You have to add a Sort parameter t your repository query, see the documentation for Spring Data Elasticsearch 4 where this is described.

    In your case you'd need:

    Sort sort = Sort.by(
        new GeoDistanceOrder("geoLocation", new GeoPoint(32.846027, -96.84987))
            .withUnit("mi")
            .with(Sort.Direction.ASC)
    );
    

    you can leave out the sort direction, because ASC is the default value