_geo_distance sort always returning Infinity

I have an index:

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

I index some data like:

{
        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:

{
      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 :confused:

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

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.

Hi @dadoonet

Thanks for your response :slight_smile:
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..

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.

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 :slight_smile:

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..