Sort on geo_shape using geo_distance filter return 500

Hey there,

my objective is narrowing down nearby addresses within 100 meters and sorting them by the nearest address

My mapping:

mappings: {
    dynamic: "true",
    properties: {
      geometry: { type: "geo_shape" }
    }
}

The geo_shape is always a point

type: "point",
coordinates: [lat, long]

The query:

GET addresses/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "100",
          "geometry": {
            "lat": 51.3606,
            "lon": 7.4785
          }
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "geometry": {
          "lat": 51.3606,
          "lon": 7.4785
        },
        "order": "asc",
        "unit": "m",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]
}

The query works without the sorting

The query produces the following error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "class_cast_exception",
        "reason" : "class org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData cannot be cast to class org.elasticsearch.index.fielddata.IndexGeoPointFieldData (org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData is in unnamed module of loader java.net.FactoryURLClassLoader @28e94c2; org.elasticsearch.index.fielddata.IndexGeoPointFieldData is in unnamed module of loader 'app')"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "addresses",
        "node" : "6ncZ_x83Qw2fnlzYvbcRzQ",
        "reason" : {
          "type" : "class_cast_exception",
          "reason" : "class org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData cannot be cast to class org.elasticsearch.index.fielddata.IndexGeoPointFieldData (org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData is in unnamed module of loader java.net.FactoryURLClassLoader @28e94c2; org.elasticsearch.index.fielddata.IndexGeoPointFieldData is in unnamed module of loader 'app')"
        }
      }
    ],
    "caused_by" : {
      "type" : "class_cast_exception",
      "reason" : "class org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData cannot be cast to class org.elasticsearch.index.fielddata.IndexGeoPointFieldData (org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData is in unnamed module of loader java.net.FactoryURLClassLoader @28e94c2; org.elasticsearch.index.fielddata.IndexGeoPointFieldData is in unnamed module of loader 'app')",
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "class org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData cannot be cast to class org.elasticsearch.index.fielddata.IndexGeoPointFieldData (org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData$LatLonShapeIndexFieldData is in unnamed module of loader java.net.FactoryURLClassLoader @28e94c2; org.elasticsearch.index.fielddata.IndexGeoPointFieldData is in unnamed module of loader 'app')"
      }
    }
  },
  "status" : 500
}

What am I doing wrong?

Hi!!

According to the documentation it doesn't seem possible to apply "sort".
Maybe you need to think about using the "geo_point" type.

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.

2 Likes