Skip to content

500 Internal Server Error on category search with zero matching results #4148

Description

@Itz-Agasta

Describe the bug
Any /search request using the hidden [key=value] category syntax combined with a free-text near-phrase crashes with an uncaught IndexError, returned to the client as a generic 500 Internal Server Error, whenever the category search legitimately returns zero results.

Basically thr ForwardGeocoder.sort_and_cut_results() in src/nominatim_api/search/geocoder.py unconditionally indexes results[0]:

def sort_and_cut_results(self, results: SearchResults) -> SearchResults:
    results.sort(key=lambda r: (r.ranking, 0 if r.bbox is None else -r.bbox.area))
    final = SearchResults()
    min_rank = results[0].rank_search   # IndexError if results is empty
    min_ranking = results[0].ranking
    ...

git blame shows the guard against empty results was removed in commit 87a8c24 ("improve result cutting when a POI comes out with top importance", 2025-06-01), which previously wrapped this block in if results:. Still present and unfixed on current master (2991c61).

This is confirmed independent of any specific category backend (old class/type place_classtype_* tables or the newer ltree-based categories column) -- it reproduces on production, which uses the old mechanism.

To Reproduce

curl -G "https://nominatim.openstreetmap.org/search" \
  --data-urlencode "q=[amenity=xyzxyz] Berlin" \
  --data-urlencode "format=json"

Response: {"title": "500 Internal Server Error"} (HTTP 500). amenity=xyzxyz is not a real OSM tag value, so this query is guaranteed zero category matches everywhere, ruling out a data/import-specific cause.

Software Environment (please complete the following information):

  • Nominatim version: 5.3.99-2 (current master, commit 2991c61)
  • Postgresql version: 17.9 (Debian 17.9-0+deb13u1)
  • Postgis version: 3.5.2
  • OS: Debian (x86_64-pc-linux-gnu

Hardware Configuration (please complete the following information):

  • RAM:
  • number of CPUs:
  • type and size of disks:

Ahhh i cant recall

Postgresql Configuration:

NIL

Nominatim Configuration:

NIL

Additional context

Confirmed on two independent instances:

  • nominatim.openstreetmap.org (production) -- [amenity=xyzxyz] Berlin -> HTTP 500
  • our planet test instance -- same guaranteed-zero-match category, and the originally-noticed [club=scout] Vaduz -> HTTP 500

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions