Trouble Sorting by Geodistance in Elasticsearch with Nested Geo_location Field

Hello community!
I'm encountering an issue with sorting by geodistance in Elasticsearch and could use some guidance. I have set up an index with a nested geo_location field, and my intention is to sort the search results based on geodistance from a specified point.

However, despite my efforts, the sorting seems to have no effect on the results. And the result is relevent to the searchterm, distance and geopoint specified in the query. I've double-checked my implementation, but I'm still unable to achieve the desired sorting behavior.

If anyone has experience with sorting by geodistance in Elasticsearch, particularly with nested geo_location fields, I would greatly appreciate any insights or suggestions you can offer. Your help would be invaluable in resolving this issue.

Thank you in advance for your assistance!

current mapping: :

{
    "indexemployee": {
        "mappings": {
            "properties": {
                "properties": {
                    "properties": {
                        "profession": {
                            "properties": {
                                "code": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "label": {
                                    "type": "text",
                                    "analyzer": "autocomplete"
                                }
                            }
                        }
                    }
                },
                "otherData": {
                    "properties": {
                        "workplace": {
                            "type": "nested",
                            "properties": {
                                "ent_geo": {
                                    "type": "nested",
                                    "properties": {
                                        "properties": {
                                            "properties": {
                                                "address": {
                                                    "properties": {
                                                        "countryCode": {
                                                            "type": "text",
                                                            "fields": {
                                                                "keyword": {
                                                                    "type": "keyword",
                                                                    "ignore_above": 256
                                                                }
                                                            }
                                                        },
                                                        "countryName": {
                                                            "type": "text",
                                                            "fields": {
                                                                "keyword": {
                                                                    "type": "keyword",
                                                                    "ignore_above": 256
                                                                }
                                                            }
                                                        },
                                                        "geolocation": {
                                                            "type": "geo_point"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

search query :

{
  "sort": [
    {
      "_geo_distance": {
        "otherData.workplace.ent_geo.properties.address.geolocation": {
          "lat": "41.12",
        "lon": "-71.34"
        },
        "order": "desc",
        "unit": "km",
        "mode": "min",
        "nested": {
          "path": "otherData.workplace"
        }
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "properties.profession.label": "Doctor"
          }
        },
        {
          "nested": {
            "path": "otherData.workplace",
            "query": {
              "nested": {
                "path": "otherData.workplace.ent_geo",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "geo_distance": {
                          "distance": "30km",
                          "otherData.workplace.ent_geo.properties.adresse.geolocation": {
                            "lat": "41.12",
                            "lon": "-71.34"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

Hey there!
I was taking a look at your mapping and I am having trouble reading it. There seem to be a lot of {properties: { properties: { properties: { }}}} that I don't understand the sense of. Is that the complete mapping or did you delete some parts that are not relevant to your question?

I will be assuming that there are more fields here that you havent included in your post or that your data really requires this specific mapping.

The only thing I can see is that you included this in the query:

 "otherData.workplace.ent_geo.properties.adresse.geolocation":

Based on the mapping, shouldn't it be

 "otherData.workplace.ent_geo.properties.address.geolocation":

?

1 Like

Thank you for your help,
I apologize for the oversight. You're absolutely right, the correct query should be "otherData.workplace.ent_geo.properties.address.geolocation", as you mentioned.

Regarding the mapping, it appears that it might seem complex due to the presence of the "properties" field. I understand how this could lead to confusion when interpreting the mapping. To enhance readability and emphasize the importance of the geo_point property, I've provided the minimum necessary properties.

To illustrate further, here's an example document that might aid in better comprehension:

{
    "properties": {
        "profession": {
            "code": "86",
            "label": "Technicien de Laboratoire"
        }
    },
    "otherData": {
        "workplace": [
            {
                "ent_geo": {
                    "properties": {
                        "address": {
                            "countryCode": "FR",
                            "countryName": "France",
                            "geolocation": {
                                "lat": "42.7544533927",
                                "lon": "1.69800818291"
                            }
                        }
                    }
                }
            }
        ]
    }
}