Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): offer the nearest notes a search matches, past their accents

Elian Doran6131cb840700
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
index e12867d678..568fcde1a0 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
@@ -194,6 +194,40 @@ describe("geo map SearchBox", () => {
]);
});
+ it("matches a title past its accents and in whatever order the terms are typed", async () => {
+ mockGeocoder([]);
+ const container = renderSearchBox(fakeMap(), [
+ buildNote({ title: "Zürich Hauptbahnhof", "#geolocation": "47.37,8.54" }),
+ buildNote({ title: "Paris Hotel", "#geolocation": "48.85,2.35" })
+ ]);
+
+ // Matched by the terms the app filters its own lists by, which fold accents on both sides.
+ await type(container, "zurich");
+ expect(labels()).toEqual([ "Zürich Hauptbahnhof", "geo-map.search-online(zurich)" ]);
+
+ // Every term rather than the whole query in one piece, so their order is the typist's.
+ await type(container, "hotel paris");
+ expect(labels()).toEqual([ "Paris Hotel", "geo-map.search-online(hotel paris)" ]);
+ });
+
+ it("offers the nearest of the notes that match, rather than the first the map holds", async () => {
+ mockGeocoder([]);
+ // More matches than the list has room for, the nearest of them held last: a cap taken while
+ // gathering would offer the nine furthest and leave the one at hand out altogether.
+ const far = Array.from({ length: 9 }, (_, index) =>
+ buildNote({ title: `Cafe ${index}`, "#geolocation": "10,10" }));
+ const container = renderSearchBox(
+ fakeMap(), [ ...far, buildNote({ title: "Cafe next door", "#geolocation": "39.66,19.96" }) ]);
+
+ await type(container, "cafe");
+
+ // The last of them stands beside what the map is showing (see CENTRE), so it comes first —
+ // and the list holds the eight it has room for, with the geocoder's row under them.
+ expect(labels()[0]).toBe("Cafe next door");
+ expect(labels()).toHaveLength(9);
+ expect(labels().at(-1)).toBe("geo-map.search-online(cafe)");
+ });
+
it("offers nothing for a query below the minimum length", async () => {
const container = renderSearchBox(fakeMap(), mapNotes());
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
index 6a957e23ce..cefaee77a0 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
@@ -10,6 +10,7 @@ import { t } from "../../../services/i18n";
import { logError } from "../../../services/ws";
import { getMeasurementSystem } from "../../../utils/formatters";
import { formatDistance } from "../../../utils/units";
+import { filterTokens, matchesFilter } from "../../react/filter";
import FormAutocomplete from "../../react/FormAutocomplete";
import Icon from "../../react/Icon";
import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";
@@ -340,14 +341,22 @@ function viewportOf(map: MapLibreGLMap): GeoBounds {
return [ [ bounds.getWest(), bounds.getSouth() ], [ bounds.getEast(), bounds.getNorth() ] ];
}
-/** The notes on the map whose title contains the query and that are drawn somewhere. */
+/**
+ * The notes on the map that the query names and that are drawn somewhere.
+ *
+ * Matched by the terms the app filters its own lists by (see `filterTokens`), which ignores case and
+ * accents on both sides and asks for every term rather than the whole query in one piece: "zurich"
+ * finds "Zürich Hauptbahnhof", and "hotel paris" finds "Paris Hotel".
+ *
+ * All of them, however many: which of them the list has room for is settled once they can be ordered
+ * by how far off they stand (see {@link grouped}).
+ */
function matchMarkers(notes: FNote[], query: string): SearchEntry[] {
- const needle = query.toLowerCase();
+ const tokens = filterTokens(query);
const matches: SearchEntry[] = [];
for (const note of notes) {
- if (matches.length >= MAX_MARKER_RESULTS) break;
- if (!note.title.toLowerCase().includes(needle)) continue;
+ if (!matchesFilter(tokens, note.title)) continue;
// A note without a readable location has no marker to fly to. GPX tracks are skipped for the
// same reason: their route is in the file rather than on a label.
@@ -425,7 +434,9 @@ function grouped(entries: SearchEntry[]): SearchEntry[] {
const isAtHand = (entry: SearchEntry) => (entry.distance ?? Infinity) <= SEARCH_RADIUS_M;
const groups: { key: string; label: string; rows: SearchEntry[] }[] = [
- { key: "on-map", label: t("geo-map.results-on-map"), rows: sorted.filter((entry) => entry.kind === "marker") },
+ // Capped once sorted, so what the list offers is the nearest of the notes that match rather
+ // than whichever of them the map happens to hold first.
+ { key: "on-map", label: t("geo-map.results-on-map"), rows: sorted.filter((entry) => entry.kind === "marker").slice(0, MAX_MARKER_RESULTS) },
{ key: "nearby", label: t("geo-map.results-nearby"), rows: places.filter(isAtHand) },
{ key: "far", label: t("geo-map.results-far"), rows: places.filter((entry) => !isAtHand(entry)) }
].filter((group) => group.rows.length);
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
index 90bb286099..4dbb586303 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html
@@ -59,9 +59,13 @@
already on the map, and — when asked — an online place search.</p>
<h3>Searching the notes already on the map</h3>
<p>Type in the search bar and the map's own notes are matched by their title as
- you type. Notes without a <code spellcheck="false">#geolocation</code> attribute are not offered,
- since there would be nowhere to go; GPS tracks are left out for the same
- reason.</p>
+ you type. Accents are ignored on both sides, so typing <code spellcheck="false">zurich</code> finds
+ <em>Zürich Hauptbahnhof</em>, and each word is looked for on its own, so
+ <code spellcheck="false">hotel paris</code> finds <em>Paris Hotel</em>. Notes without a
+ <code spellcheck="false">#geolocation</code> attribute are not offered, since there would be nowhere
+ to go; GPS tracks are left out for the same reason.</p>
+<p>Where more notes match than the list has room for, the ones nearest to what
+ the map is showing are offered.</p>
<h3>Searching for places online</h3>
<p>Looking up a place sends what you typed to a third-party service, so it is
never done while typing. Instead, the last row of the result list offers it
diff --git a/docs/User Guide/User Guide/Collections/Geo Map.md b/docs/User Guide/User Guide/Collections/Geo Map.md
index 380124e881..36e00fd580 100644
--- a/docs/User Guide/User Guide/Collections/Geo Map.md
+++ b/docs/User Guide/User Guide/Collections/Geo Map.md
@@ -46,7 +46,9 @@ The search bar at the top-left of the map looks in two places: the notes already
### Searching the notes already on the map
-Type in the search bar and the map's own notes are matched by their title as you type. Notes without a `#geolocation` attribute are not offered, since there would be nowhere to go; GPS tracks are left out for the same reason.
+Type in the search bar and the map's own notes are matched by their title as you type. Accents are ignored on both sides, so typing `zurich` finds _Zürich Hauptbahnhof_, and each word is looked for on its own, so `hotel paris` finds _Paris Hotel_. Notes without a `#geolocation` attribute are not offered, since there would be nowhere to go; GPS tracks are left out for the same reason.
+
+Where more notes match than the list has room for, the ones nearest to what the map is showing are offered.
### Searching for places online