# \_geo\_distance sort always returning Infinity

**URL:** <https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523>\
**Category:** Elasticsearch\
**Created:** [September 13, 2024, 10:42am UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523 "2024-09-13T10:42:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sircameron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sircameron/32/137559_2.png) [@sircameron](https://discuss.elastic.co/u/sircameron)\
**Post date:** [September 13, 2024, 10:42am UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/1 "2024-09-13T10:42:41Z")

</div>

I have an index:

```auto
{
      index: 'discoveries',
      mappings: {
        properties: {
          id: { type: 'integer' },
          description: { type: 'text' },
          location: { type: 'geo_point' },
        },
      },
    }

```

I index some data like:

```auto
{
        refresh: true,
        id: 'some id',
        index: "discoveries",
        document: {
          id: 'some id',
          description: 'description',
          location: {
            type: "Point",
            coordinates: [longitude, latitude],
          },
        },
      }

```

when I run a query like this:

```auto
{
      index: 'discoveries',
      query: {
        match: {
          description: {
            query: 'some text',
          },
        },
      },
      sort: [
        {
          _geo_distance: {
            location: [longitude,latitude],
            order: 'desc',
            unit: 'km',
            mode: 'median',
            distance_type: 'arc',
            ignore_unmapped: true,
          },
        },
      ],
    }

```

This always returns Infinity for the \_geo\_distance sort... always...  
Has anyone else experienced this? is there something obvious I'm missing?

This is running locally with docker, using the javascript lib..

If I set `ignore_unmapped` to false, I get the error `	illegal_argument_exception: failed to find mapper for [location] for geo distance based sort`.  
So this points to location not being mapped correctly, but I map it when creating the index 😕

---

<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:** [September 13, 2024, 11:40am UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/2 "2024-09-13T11:40:37Z")

</div>

Welcome!

Does the sort order look ok when you look at the resultset?

May be you need to add:

```
track_scores: true

```

See [Sort search results | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#_track_scores)

This page also says:

> geo distance sorting does not support configurable missing values: the distance will always be considered equal to `Infinity` when a document does not have values for the field that is used for distance computation.

Which means that the first docs don't have a location.

Could you share a response and also the mapping.

---

<div class="post-metadata">

**Author:** ![sircameron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sircameron/32/137559_2.png) [@sircameron](https://discuss.elastic.co/u/sircameron)\
**Post date:** [September 13, 2024, 1:34pm UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/3 "2024-09-13T13:34:34Z")

</div>

Hi @dadoonet

Thanks for your response 🙂  
The mapping is in my first post..

The sort order is not in order of distance at all, and when I console out the sort value from the hit, it shows Infinity...

I think you were correct that the first few docs didn't have a location set correctly... but I've tried many times to delete and recreate the index with no luck...  
BUT, I've managed to get this working by only ever specifying the location as an array [lon, lat], in both indexing and searching... I think this might be a bug in the JS lib..

---

<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:** [September 13, 2024, 1:49pm UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/4 "2024-09-13T13:49:34Z")

</div>

You are sorting with `order: 'desc'` which means that you start from the most far document. Which explains Infinity here.  
`order: 'asc'` should give accurate results.

---

<div class="post-metadata">

**Author:** ![sircameron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sircameron/32/137559_2.png) [@sircameron](https://discuss.elastic.co/u/sircameron)\
**Post date:** [September 13, 2024, 1:59pm UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/5 "2024-09-13T13:59:10Z")

</div>

That's a typo here... in code I have `asc`..  
While I was struggling with it, I changed all of this many times to test and still got Infinity.... also, even with sorting the wrong direction, I shouldn't be getting Infinity since the planet and coordinates are finite 🙂

---

<div class="post-metadata">

**Author:** ![sircameron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sircameron/32/137559_2.png) [@sircameron](https://discuss.elastic.co/u/sircameron)\
**Post date:** [September 13, 2024, 2:00pm UTC](https://discuss.elastic.co/t/geo-distance-sort-always-returning-infinity/366523/6 "2024-09-13T14:00:31Z")

</div>

But to anyone struggling with this and using the js lib:  
coordinates need to always be defined as an array eg `[lon, lat]` and not using the other formats mentioned in the docs..
