fix(maps): show OpenStreetMap alongside the index, not instead of it

Measured against 128 places out of a real trip to Japan, all saved
through the old search: the index answered 51.6 percent inside its top
five. For most of the rest it returned the shops AROUND the landmark
rather than nothing, which is worse than nothing, because ten plausible
wrong answers look like an answer.

Overture Places is a dataset of businesses and is very good at those. It
largely does not carry temples, bridges, riverside walks, viewpoints or
observation decks, which is a fair share of what somebody planning a
trip searches for.

Two ways to detect "the index does not have this one" were measured and
both failed. Its own score does not separate the cases: a 0.75 threshold
catches 19 of 62 misses and throws away 6 of 66 hits. Name overlap does
not either: 57 false positives, because "Hase Station" shares a word
with "Hase-dera".

So this does not classify. Explicit search asks both concurrently and
merges, name matches first and the index ahead of OpenStreetMap within
each group. Autocomplete is untouched, because Nominatim's policy names
it as unacceptable use. Nominatim also gets the location bias it never
had, without which "Hase-dera" returns the temple in Nara rather than
the one in Kamakura the user is standing beside.

Two booking-import cases still stubbed searchNominatim after geocoding
moved to geocodeQuery, and stubbed it with the old array shape, which
made "has coordinates" pass on an undefined lat.
This commit is contained in:
Maurice
2026-09-06 01:27:17 +02:00
committed by Maurice
parent bac85cb98a
commit 3caafe0548
4 changed files with 202 additions and 19 deletions
+60 -1
View File
@@ -1,5 +1,6 @@
import { readEnv, getAppUrl } from '../../app-config';
import { stripHtmlTags } from '../common/stripHtmlTags';
import { haversineMetres } from '../common/geo';
/**
* Pure maps/geo helpers — no DB, no Nest, no side effects beyond reading env.
@@ -75,7 +76,7 @@ export function toWikiLang(lang: string | undefined, fallback = 'en'): string {
// Re-exported, not redefined: this was one of three copies. Kept as an export
// here because callers and a test import it from maps.helpers.
export { haversineMetres } from '../common/geo';
export { haversineMetres };
/**
* Whether two place names plausibly refer to the same thing.
@@ -91,6 +92,64 @@ export { haversineMetres } from '../common/geo';
* list for all of them is its own maintenance problem. Anything the pair rule
* lets through has already survived the distance check.
*/
/**
* Put the index's answer and OpenStreetMap's into one list.
*
* Overture Places is a dataset of businesses. It is very good at those and
* largely does not carry temples, bridges, riverside walks, viewpoints or
* observation decks, which is a fair share of what somebody planning a trip
* searches for. Measured against 128 places out of a real trip to Japan, saved
* through the old search: the index answered 51.6 percent inside its top five,
* and for most of the rest it returned the shops AROUND the landmark rather
* than nothing, which is worse than nothing because it looks like an answer.
*
* Two candidate signals for "the index does not have this one" were measured
* and both failed. The index's own score does not separate the cases (a
* threshold of 0.75 catches 19 of 62 misses and throws away 6 of 66 hits), and
* name overlap against the result set does not either (57 false positives,
* because "Hase Station" shares a word with "Hase-dera"). So this does not
* classify. It shows both, and lets the name decide the order:
*
* 1. results whose name plausibly matches what was typed, index first
* 2. everything else, index first
*
* A place both sources know is kept once, as the index's copy, because that is
* the one carrying a stable id, contact details and hours.
*/
export function mergeSearchResults(
fromIndex: Record<string, unknown>[],
fromOsm: Record<string, unknown>[],
query: string,
limit = 10,
): Record<string, unknown>[] {
const sameThing = (a: Record<string, unknown>, b: Record<string, unknown>): boolean => {
const [aLat, aLng, bLat, bLng] = [a.lat, a.lng, b.lat, b.lng];
if (![aLat, aLng, bLat, bLng].every((v) => typeof v === 'number')) return false;
// 60 m is close enough to be the same shop. It is NOT enough on its own:
// a temple and the coffee shop at its gate are fifty metres apart, and
// deduping on distance alone swallowed the temple, which is the exact
// result this function exists to surface. Two things in one spot with
// unrelated names are two things.
if (haversineMetres(aLat as number, aLng as number, bLat as number, bLng as number) >= 60) {
return false;
}
return (
typeof a.name === 'string' && typeof b.name === 'string' && namesOverlap(a.name, b.name)
);
};
const extra = fromOsm.filter((o) => !fromIndex.some((i) => sameThing(i, o)));
const matches = (p: Record<string, unknown>): boolean =>
typeof p.name === 'string' && namesOverlap(query, p.name);
return [
...fromIndex.filter(matches),
...extra.filter(matches),
...fromIndex.filter((p) => !matches(p)),
...extra.filter((p) => !matches(p)),
].slice(0, limit);
}
export function namesOverlap(a: string, b: string): boolean {
const words = (value: string): Set<string> =>
new Set(
+39 -8
View File
@@ -41,6 +41,7 @@ import {
toWikiLang,
haversineMetres,
namesOverlap,
mergeSearchResults,
type GoogleOpeningHours,
type OverpassPoi,
} from './maps.helpers';
@@ -764,7 +765,12 @@ export class MapsService {
* whole import. Yielding does not make the import faster, it stops it from
* being the only thing the process will do for half a minute.
*/
async searchNominatim(query: string, lang?: string, lane: GeoLane = 'interactive') {
async searchNominatim(
query: string,
lang?: string,
lane: GeoLane = 'interactive',
bias?: { lat: number; lng: number },
) {
const params = new URLSearchParams({
q: query,
format: 'json',
@@ -777,6 +783,15 @@ export class MapsService {
limit: '10',
'accept-language': toApiLang(lang),
});
// Prefer the area the caller is looking at, never restrict to it
// (bounded=0). Without this, "Hase-dera" returns the temple of that name in
// Nara rather than the one in Kamakura the user is standing next to; with
// bounded=1 a search for somewhere genuinely far away would return nothing.
if (bias) {
const d = 0.5;
params.set('viewbox', [bias.lng - d, bias.lat - d, bias.lng + d, bias.lat + d].join(','));
params.set('bounded', '0');
}
// Through the shared client: one throttle for the whole process.
const response = await nominatimFetch('search', params, { lane });
if (!response.ok) {
@@ -1624,13 +1639,29 @@ export class MapsService {
// this method did before.
if (this.trekPlacesEnabled()) {
try {
const found = await trekPlacesSearch(query, {
lat: locationBias?.lat,
lng: locationBias?.lng,
limit: 10,
});
if (found.length > 0) {
return { places: found.map(toPlaceRecord), source: 'trek-places' };
// Both at once. The index is a dataset of businesses and is very good
// at those; OpenStreetMap is where the temples, bridges, riverside
// walks and viewpoints are, and a travel search asks for those
// constantly. Concurrently, so the pair costs the slower one rather
// than the sum. This is the explicit search, not the keystroke path
// Nominatim's policy rules out.
const [found, osm] = await Promise.all([
trekPlacesSearch(query, {
lat: locationBias?.lat,
lng: locationBias?.lng,
limit: 10,
}),
this.searchNominatim(query, lang, 'interactive', locationBias).catch((err: unknown) => {
console.warn('OpenStreetMap search failed, index only:', (err as Error).message);
return [] as Record<string, unknown>[];
}),
]);
const places = mergeSearchResults(found.map(toPlaceRecord), osm, query);
if (places.length > 0) {
return {
places,
source: osm.length > 0 ? 'trek-places+openstreetmap' : 'trek-places',
};
}
} catch (err: unknown) {
// Logged, not surfaced: the fallback below is a working answer.