feat(maps): ask Photon beside the index for search-as-you-type suggestions

The suggestion list asked only the TREK index and its OpenStreetMap layer,
because Nominatim's usage policy rules out autocomplete. The layer is a slice
of OpenStreetMap, so many places showed up only once the full search was sent
(#2877). Photon is built for this keystroke: the suggestions now ask it in
parallel with the index and interleave its rows, dropping any the list
already holds, by OSM id or by the search list's same-place rule.

Without Photon (services off, an operator's own Nominatim, a paused or failed
answer) the list reads exactly as before. A failing index no longer throws
away what Photon found, and the Google-only switch skips Photon as it skips
the index.

Refs #2877
This commit is contained in:
Maurice
2026-10-29 13:49:06 +01:00
parent 81b3657528
commit 7d669cb5c3
9 changed files with 310 additions and 33 deletions
+17 -1
View File
@@ -5,7 +5,7 @@
* the decision to make them is MapsService's.
*/
import { getCountryFromCoords } from '../../utils/country-borders';
import { buildOsmDetails, parsePoiCategories, samePlace, toApiLang } from './maps.helpers';
import { buildOsmDetails, interleave, parsePoiCategories, samePlace, toApiLang } from './maps.helpers';
import { POI_RESULT_CAP, type PoiSearchResult } from './providers/osm.client';
import {
INDEX_MAX_CATEGORY_CHARS,
@@ -335,6 +335,22 @@ export function indexSuggestions(found: TrekSearchHit[], lang?: string): MapsAut
);
}
type Suggestion = MapsAutocompleteResult['suggestions'][number];
/**
* The index's suggestions with Photon's interleaved, the way the search list
* merges the index with Nominatim (#2877). A Photon row the list already holds
* is dropped: the same OpenStreetMap element under the layer's copy of its id,
* or the same place by the rule the search list dedupes with.
*/
export function mergeSuggestions(fromIndex: Suggestion[], fromPhoton: Suggestion[], limit = 8): Suggestion[] {
const asPlace = (s: Suggestion) => ({ lat: s.lat, lng: s.lng, name: s.mainText });
const extra = fromPhoton.filter(
(o) => !fromIndex.some((i) => i.placeId === o.placeId || samePlace(asPlace(i), asPlace(o))),
);
return interleave(fromIndex, extra, limit);
}
/**
* The details record of a place picked out of the index, with what
* OpenStreetMap knows about the same building filled in.
+8 -4
View File
@@ -115,11 +115,15 @@ export function mergeSearchResults(
limit = 10,
): Record<string, unknown>[] {
const extra = fromOsm.filter((o) => !fromIndex.some((i) => samePlace(i, o)));
return interleave(fromIndex, extra, limit);
}
const out: Record<string, unknown>[] = [];
for (let i = 0; i < Math.max(fromIndex.length, extra.length) && out.length < limit; i++) {
if (i < fromIndex.length) out.push(fromIndex[i]);
if (i < extra.length && out.length < limit) out.push(extra[i]);
/** One row of each list in turn, the first list leading, up to `limit` rows. */
export function interleave<T>(first: T[], second: T[], limit: number): T[] {
const out: T[] = [];
for (let i = 0; i < Math.max(first.length, second.length) && out.length < limit; i++) {
if (i < first.length) out.push(first[i]);
if (i < second.length && out.length < limit) out.push(second[i]);
}
return out;
}
+31 -20
View File
@@ -11,6 +11,7 @@ import {
indexRequests,
indexSuggestions,
mergeIndexPages,
mergeSuggestions,
} from './maps-index.helpers';
import { NEARBY_DEFAULT_LIMIT, NEARBY_DEFAULT_RADIUS_M, nearbyCacheKey, nearestFirst } from './maps-nearby.helpers';
import { MapsUrlResolver, type ResolvedMapsUrl } from './maps-url.resolver';
@@ -542,29 +543,39 @@ export class MapsService {
}
if (this.trekPlacesEnabled() && !(await this.selector.googleOnly(keyed))) {
try {
const centre = boxCentre;
// Both layers here, unlike the explicit search above. That path asks
// Nominatim in parallel and would get the same OpenStreetMap places
// twice; this one asks nobody else, because Nominatim's usage policy
// names autocomplete as unacceptable use. So the layer is not a second
// opinion here, it is the only place the missing names live.
//
// Measured on the case that surfaced it: "Tokio station" typed from
// Tokyo returned a weigh station in Ritzville and a station in Mexico
// from the index alone, and "Tokio Hauptbahnhof" in second place with
// the layer. The index holds businesses; stations, temples and bridges
// are in OpenStreetMap, and so is every exonym a traveller types.
const found = await trekPlacesSearch(input, {
lat: centre?.lat,
lng: centre?.lng,
// Both layers here, unlike the explicit search above. That path asks
// Nominatim in parallel and would get the same OpenStreetMap places
// twice; this one never asks Nominatim, because its usage policy names
// autocomplete as unacceptable use.
//
// Measured on the case that surfaced the layer: "Tokio station" typed
// from Tokyo returned a weigh station in Ritzville and a station in
// Mexico from the index alone, and "Tokio Hauptbahnhof" in second place
// with the layer. The index holds businesses; stations, temples and
// bridges are in OpenStreetMap, and so is every exonym a traveller types.
//
// Photon beside them (#2877). The layer is a slice of OpenStreetMap, and
// a place missing from it showed up only once the search was sent.
// Photon is built for this keystroke and answers null when the services
// are off, so the pair costs the slower one and an install without
// Photon reads exactly as before. Each side swallows its own failure, so
// one that is down does not throw away what the other found.
const [found, photon] = await Promise.all([
trekPlacesSearch(input, {
lat: boxCentre?.lat,
lng: boxCentre?.lng,
limit: 8,
sources: 'index,osm',
lang,
});
if (found.length > 0) return { suggestions: indexSuggestions(found, lang), source: 'trek-places' };
} catch (err: unknown) {
console.warn('TREK Places autocomplete failed, falling back:', (err as Error).message);
}).catch((err: unknown) => {
console.warn('TREK Places autocomplete failed, falling back:', (err as Error).message);
return [];
}),
this.osm.autocompletePhoton(input, lang, boxCentre),
]);
const suggestions = mergeSuggestions(indexSuggestions(found, lang), photon ?? []);
if (suggestions.length > 0) {
return { suggestions, source: found.length > 0 ? 'trek-places' : 'openstreetmap' };
}
}
+11 -1
View File
@@ -48,7 +48,7 @@ import {
import { overpassSelector, tagsMatchFilter } from '../osm-filter.helpers';
import { OverpassBusy, hedged, lastAnsweredFirst, retryAfterMs, soonestRetryMs } from '../overpass-hedge.helpers';
import { PhotonClient } from './photon.client';
import { asNominatimHit, asNominatimReverse } from './photon.mapping';
import { asNominatimHit, asNominatimReverse, asSuggestion } from './photon.mapping';
import { readWikiIdentity } from './wiki-identity';
import { Injectable } from '@nestjs/common';
import { normalizePlaceWebsite } from '@trek/shared';
@@ -526,6 +526,16 @@ export class OsmClient {
}
}
/**
* Suggestions from Photon, which is built for search-as-you-type, or null when
* it is not in use or did not answer. Never Nominatim in its place: its usage
* policy rules out autocomplete.
*/
async autocompletePhoton(input: string, lang?: string, bias?: { lat: number; lng: number }) {
const found = await this.photon.search(input, { lang, bias });
return found?.map(asSuggestion).filter((row) => row !== null) ?? null;
}
/**
* Finds the OpenStreetMap record for a place we only know by name and
* coordinate, and hands back its tags.
@@ -10,6 +10,7 @@
*/
import { toApiLang } from '../maps.helpers';
import type { NominatimResult, NominatimReverse } from './osm.client';
import type { MapsAutocompleteResult } from '@trek/shared';
import { z } from 'zod';
@@ -74,6 +75,7 @@ export const photonAnswerSchema = z.object({ features: z.array(photonFeatureSche
export type PhotonFeature = z.infer<typeof photonFeatureSchema>;
type PhotonProperties = PhotonFeature['properties'];
type Suggestion = MapsAutocompleteResult['suggestions'][number];
/**
* The language Photon is asked in: TREK's code through toApiLang (`br` is
@@ -133,6 +135,33 @@ export function asNominatimHit(feature: PhotonFeature): NominatimResult | null {
};
}
/**
* A search hit as one row of the suggestion list, in the id form the details
* lookup reads (`node:123`, as the index's OpenStreetMap layer hands it over),
* or null for one without an id or a name to show.
*/
export function asSuggestion(feature: PhotonFeature): Suggestion | null {
const hit = asNominatimHit(feature);
const [lng, lat] = feature.geometry.coordinates;
if (!hit || !Number.isFinite(lat) || !Number.isFinite(lng)) return null;
// The address line starts with the name, so the rest of it reads as where the
// place is. Cut by the name rather than at its first comma, which a name can
// hold; a house without a name is titled by its number, as Nominatim does.
const name = feature.properties.name?.trim();
const line = hit.display_name;
const [head = '', ...rest] = line.split(', ');
const mainText = name || head;
if (!mainText) return null;
return {
placeId: `${hit.osm_type}:${hit.osm_id}`,
mainText,
secondaryText: name ? line.slice(name.length).replace(/^, /, '') : rest.join(', '),
source: 'openstreetmap',
lat,
lng,
};
}
/**
* The point a reverse lookup found, in the fields Nominatim's reverse answer
* has. A town itself is its own `city`, and a named place is filed under its