# Problem sorting result by distance on a geoshape

**URL:** https://discuss.elastic.co/t/problem-sorting-result-by-distance-on-a-geoshape/170076
**Category:** Elasticsearch
**Created:** [February 26, 2019, 10:01pm UTC](https://discuss.elastic.co/t/problem-sorting-result-by-distance-on-a-geoshape/170076 "2019-02-26T22:01:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jnick1982](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@jnick1982](https://discuss.elastic.co/u/jnick1982)
#### Post date: [February 26, 2019, 10:01pm UTC](https://discuss.elastic.co/t/problem-sorting-result-by-distance-on-a-geoshape/170076/1 "2019-02-26T22:01:10Z")

</div>

As documented :

Sorting and Retrieving index Shapes

Due to the complex input structure and index representation of shapes, it is not currently possible to sort shapes or retrieve their fields directly. The geo\_shape value is only retrievable through the `_source` field.

field Location is of type geo\_shape  
field Coordinates is of type geo\_point

Although when I add a filter on a geoshapecircle like :

```
.Filter(fi => fi    
    .GeoShapeCircle(c => c
        .Name("geo_query")
        .Boost(3)
        .Field(p => p.Location)
        .Coordinates(query.Lon, query.Lat)
        .Radius($"{query.MeterDistance}m")
        .Relation(GeoShapeRelation.Intersects)
    )
)

```

It looks like the results are return sorted in order of proximity of the Coordinates center defined in the filter, but I didn't find anywhere to confirm or infirm this?

So for now I added a field Coordinates which is a geopoint in the index with the same coordinates as the filed Location which is a geoshape point and I am adding sort on my ES request like below but is it really needed?

```
.Sort(s =>
    s.GeoDistance(sd => sd
        .Ascending()
        .DistanceType(GeoDistanceType.Plane)
        .Unit(DistanceUnit.Meters)
        .Field(f => f.Coordinates)
        .Points(new GeoLocation(query.Lat, query.Lon))
        )
)
```

---

<div class="post-metadata">

### Author: ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)
#### Post date: [February 27, 2019, 6:47am UTC](https://discuss.elastic.co/t/problem-sorting-result-by-distance-on-a-geoshape/170076/2 "2019-02-27T06:47:07Z")

</div>

GeoShapeCircle does not order the result by distance so yes, you need to sort by GeoDistance to make sure results are ordered.

---

<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: [March 27, 2019, 6:47am UTC](https://discuss.elastic.co/t/problem-sorting-result-by-distance-on-a-geoshape/170076/3 "2019-03-27T06:47:09Z")

</div>

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