OpenSearch: [BUG] NullPointerException in _geo_distance sort

Describe the bug

all shards failed, root_cause: [{null_pointer_exception Cannot invoke "String.endsWith(String)" because "field" is null 

This occurs on Opensearch 3.0 with a geo_point field while sorting by distance.

"location_point": {
    "type": "geo_point"
}

Changing unit from “km” to “m” fixed it on some requests but failed again when changing sort order from “asc” to “desc”

Related component

Search

To Reproduce

  1. Have a search index with geo_point field
  2. Sort by _geo_distance
{
  "query": { "match_all": {} },
  "sort": [ { "_geo_distance": { "location_point": [9.227400, 49.189800], "order": "desc", "unit": "km", "distance_type": "arc", "mode": "min", "ignore_unmapped": true } } ],
  "size": 10
}
  1. Get an Exception all shards failed, root_cause: [{null_pointer_exception Cannot invoke "String.endsWith(String)" because "field" is null 

Expected behavior

Sorting works like in Opensearch 2.18

Additional Details

Plugins Basic docker image:
Image with Bug: opensearchproject/opensearch:latest Working image: opensearchproject/opensearch:2.18.0

Screenshots If applicable, add screenshots to help explain your problem.

Host/Environment (please complete the following information):

  • OS: [e.g. iOS]
  • Version [e.g. 22]

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 19 (12 by maintainers)

Most upvoted comments

@owaiskazi19, I tried to reproduce in the latest. I think the original poster mentioned that 2.18 is fine but not the latest.

@arsonik Thank you so much for the detailed steps! I was able to reproduce the error. Now, I can try to understand what is wrong.

So if you start os with this command

docker run -p 9200:9200 --rm -e node.name=geonames-node -e discovery.type=single-node -e OPENSEARCH_INITIAL_ADMIN_PASSWORD=xxx -e OPENSEARCH_JAVA_OPTS="-Xms1g -Xmx1g" opensearchproject/opensearch:3

http get / output

{
  "name" : "geonames-node",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "he5lb8_6S8WZ2uiLFWjMJg",
  "version" : {
    "distribution" : "opensearch",
    "number" : "3.1.0",
    "build_type" : "tar",
    "build_hash" : "8ff7c6ee924a49f0f59f80a6e1c73073c8904214",
    "build_date" : "2025-06-21T08:05:50.445588571Z",
    "build_snapshot" : false,
    "lucene_version" : "10.2.1",
    "minimum_wire_compatibility_version" : "2.19.0",
    "minimum_index_compatibility_version" : "2.0.0"
  },
  "tagline" : "The OpenSearch Project: https://opensearch.org/"
}

/geonames/_mapping?pretty

{
  "geonames" : {
    "mappings" : {
      "properties" : {
        "admin1" : {
          "type" : "keyword"
        },
        "admin2" : {
          "type" : "keyword"
        },
        "admin3" : {
          "type" : "keyword"
        },
        "admin4" : {
          "type" : "keyword"
        },
        "alternateNames" : {
          "type" : "search_as_you_type",
          "doc_values" : false,
          "max_shingle_size" : 3,
          "analyzer" : "autocomplete_analyzer",
          "search_analyzer" : "search_analyzer"
        },
        "coordinates" : {
          "type" : "geo_point"
        },
        "countryCode" : {
          "type" : "keyword"
        },
        "elevation" : {
          "type" : "long",
          "index" : false
        },
        "featureClass" : {
          "type" : "keyword"
        },
        "featureCode" : {
          "type" : "keyword"
        },
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          },
          "analyzer" : "autocomplete_analyzer",
          "search_analyzer" : "search_analyzer"
        },
        "population" : {
          "type" : "long"
        },
        "timezone" : {
          "type" : "text",
          "index" : false
        }
      }
    }
  }
}

/geonames/_settings?pretty

{
  "geonames" : {
    "settings" : {
      "index" : {
        "replication" : {
          "type" : "DOCUMENT"
        },
        "refresh_interval" : "-1",
        "number_of_shards" : "1",
        "provided_name" : "geonames",
        "creation_date" : "1751867889296",
        "analysis" : {
          "filter" : {
            "edge_ngram_filter" : {
              "type" : "edge_ngram",
              "min_gram" : "2",
              "max_gram" : "20"
            }
          },
          "analyzer" : {
            "autocomplete_analyzer" : {
              "filter" : [
                "lowercase",
                "edge_ngram_filter"
              ],
              "type" : "custom",
              "tokenizer" : "standard"
            },
            "search_analyzer" : {
              "filter" : [
                "lowercase"
              ],
              "type" : "custom",
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "0",
        "uuid" : "kR0qz9TKT7OdhVpMIud7-g",
        "version" : {
          "created" : "137227827"
        }
      }
    }
  }
}

few documents /geonames/_search?q=paris&pretty

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 995,
      "relation" : "eq"
    },
    "max_score" : 5.4265165,
    "hits" : [
      {
        "_index" : "geonames",
        "_id" : "6618620",
        "_score" : 5.4265165,
        "_source" : {
          "id" : 6618620,
          "name" : "Paris 14",
          "alternateNames" : [
            "Paris 14",
            "Paris 14e",
            "Paris 14eme",
            "Paris 14ème",
            "Paris XIV",
            "XIVe"
          ],
          "coordinates" : {
            "lat" : 48.8331,
            "lon" : 2.3264
          },
          "featureClass" : "A",
          "featureCode" : "ADM5",
          "countryCode" : "FR",
          "admin1" : "11",
          "admin2" : "75",
          "admin3" : "751",
          "admin4" : "75056",
          "population" : 137105,
          "elevation" : 0,
          "timezone" : "Europe/Paris"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "6618621",
        "_score" : 5.4265165,
        "_source" : {
          "id" : 6618621,
          "name" : "Paris 15",
          "alternateNames" : [
            "Paris 15",
            "Paris 15e",
            "Paris 15eme",
            "Paris 15ème",
            "Paris XV",
            "XVe"
          ],
          "coordinates" : {
            "lat" : 48.8412,
            "lon" : 2.3003
          },
          "featureClass" : "A",
          "featureCode" : "ADM5",
          "countryCode" : "FR",
          "admin1" : "11",
          "admin2" : "75",
          "admin3" : "751",
          "admin4" : "75056",
          "population" : 233484,
          "elevation" : 0,
          "timezone" : "Europe/Paris"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "5170013",
        "_score" : 5.419116,
        "_source" : {
          "id" : 5170013,
          "name" : "Saint Paris",
          "alternateNames" : [
            "New Paris",
            "Paris",
            "Saint Paris",
            "St. Paris"
          ],
          "coordinates" : {
            "lat" : 40.12839,
            "lon" : -83.95966
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "US",
          "admin1" : "OH",
          "admin2" : "021",
          "admin3" : "39228",
          "population" : 2089,
          "elevation" : 368,
          "timezone" : "America/New_York"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "5166094",
        "_score" : 5.359525,
        "_source" : {
          "id" : 5166094,
          "name" : "Paris",
          "alternateNames" : [
            "Paris",
            "Paris Center",
            "Parisville"
          ],
          "coordinates" : {
            "lat" : 41.16645,
            "lon" : -81.05121
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "US",
          "admin1" : "OH",
          "admin2" : "133",
          "admin3" : "59822",
          "population" : 0,
          "elevation" : 298,
          "timezone" : "America/New_York"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "2968815",
        "_score" : 5.313365,
        "_source" : {
          "id" : 2968815,
          "name" : "Paris",
          "alternateNames" : [
            "Departement de Paris",
            "Département de Paris",
            "Parigi",
            "Paris",
            "Paris Department"
          ],
          "coordinates" : {
            "lat" : 48.8534,
            "lon" : 2.3486
          },
          "featureClass" : "A",
          "featureCode" : "ADM2",
          "countryCode" : "FR",
          "admin1" : "11",
          "admin2" : "75",
          "population" : 2165423,
          "elevation" : 0,
          "timezone" : "Europe/Paris"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "5130526",
        "_score" : 5.177707,
        "_source" : {
          "id" : 5130526,
          "name" : "Parish",
          "alternateNames" : [
            "Parish",
            "Parishville"
          ],
          "coordinates" : {
            "lat" : 43.40618,
            "lon" : -76.12576
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "US",
          "admin1" : "NY",
          "admin2" : "075",
          "admin3" : "56352",
          "population" : 438,
          "elevation" : 151,
          "timezone" : "America/New_York"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "4220849",
        "_score" : 5.134913,
        "_source" : {
          "id" : 4220849,
          "name" : "Saint Philip's Parish",
          "alternateNames" : [
            "Saint Philip Parish",
            "Saint Philip's Parish",
            "St. Philip's Parish"
          ],
          "coordinates" : {
            "lat" : 34.12872,
            "lon" : -82.93375
          },
          "featureClass" : "A",
          "featureCode" : "ADMD",
          "countryCode" : "US",
          "admin1" : "GA",
          "admin2" : "105",
          "population" : 0,
          "elevation" : 164,
          "timezone" : "America/New_York"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "2525222",
        "_score" : 5.112889,
        "_source" : {
          "id" : 2525222,
          "name" : "Casa Parisi",
          "alternateNames" : [
            "Casa Parisi",
            "Parisi"
          ],
          "coordinates" : {
            "lat" : 37.45,
            "lon" : 14.7
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "IT",
          "admin1" : "15",
          "admin2" : "CT",
          "admin3" : "087037",
          "population" : 0,
          "elevation" : 0,
          "timezone" : "Europe/Rome"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "4519642",
        "_score" : 5.112889,
        "_source" : {
          "id" : 4519642,
          "name" : "New Paris",
          "alternateNames" : [
            "New Paris",
            "Paris"
          ],
          "coordinates" : {
            "lat" : 39.85699,
            "lon" : -84.79329
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "US",
          "admin1" : "OH",
          "admin2" : "135",
          "admin3" : "38780",
          "population" : 1580,
          "elevation" : 316,
          "timezone" : "America/New_York"
        }
      },
      {
        "_index" : "geonames",
        "_id" : "3000074",
        "_score" : 5.112889,
        "_source" : {
          "id" : 3000074,
          "name" : "Les Paris",
          "alternateNames" : [
            "Les Paris",
            "Paris"
          ],
          "coordinates" : {
            "lat" : 45.63972,
            "lon" : 5.73742
          },
          "featureClass" : "P",
          "featureCode" : "PPL",
          "countryCode" : "FR",
          "admin1" : "84",
          "admin2" : "73",
          "admin3" : "732",
          "admin4" : "73147",
          "population" : 0,
          "elevation" : 0,
          "timezone" : "Europe/Paris"
        }
      }
    ]
  }
}

and the query sorting by distance

POST https://localhost:9200/geonames/_search
Content-Type: Application/json
Authorization: Basic YWRtaW46eHh4

{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "coordinates"
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "coordinates": {"lat": 40.7128, "lon": -74.0060},
        "ignore_unmapped":true,
        "order": "asc",
        "unit": "km"
      }
    }
  ],
  "size": 1
}

you get

{
  "error": {
    "root_cause": [
      {
        "type": "null_pointer_exception",
        "reason": "Cannot invoke \"String.endsWith(String)\" because \"field\" is null"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "geonames",
        "node": "djFGwotgR8yCu1ZbFVan2w",
        "reason": {
          "type": "null_pointer_exception",
          "reason": "Cannot invoke \"String.endsWith(String)\" because \"field\" is null"
        }
      }
    ],
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": "Cannot invoke \"String.endsWith(String)\" because \"field\" is null",
      "caused_by": {
        "type": "null_pointer_exception",
        "reason": "Cannot invoke \"String.endsWith(String)\" because \"field\" is null"
      }
    }
  },
  "status": 500
}

I have tried 3.0.0 and 3.1.0 Same error

With a simple docker run single node I’ll send u the complete command tomorrow !

@janschwarz I will try to see if anyone could reproduce it. This is a pretty strange issue.

@ajleong623 Can you please take an initial look on why it is failing?