From 8fd2200c7eba6ca7b12d0f95abfb17a47ff3acbb Mon Sep 17 00:00:00 2001 From: William Mikal Ruud Schmidt Date: Wed, 25 Nov 2026 15:33:48 +0100 Subject: [PATCH] Search matching updated to match for any word --- README.md | 4 ++-- backend/src/wine-query.test.ts | 22 +++++++++++++++++++++- backend/src/wine-query.ts | 18 ++++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c741aed..008c676 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ This section describes what users can do. "Backend" means the API supports the f | Feature | Backend | UI | Description | |---|---|---|---| -| Search | Done | Done | Ignores case and accents. Matches name, winery, region and grapes as one phrase. Waits 300 ms after the last key press | +| Search | Done | Done | Ignores case and accents. Matches name, winery, region and grapes. Every word must match, in any order. Waits 300 ms after the last key press | | Filtering | Done | Done | Type, country, grapes, food pairing, minimum rating and alcohol range. Several values can be selected. The counts follow the search and the other filters. On small screens the filters open in a dialog | | Sorting | Done | Done | Top rated, most rated, name and alcohol. Always sorts the whole result, not only the loaded page. «Best vurdert» puts wines with at least 50 ratings first, so a 5.0 from five people does not top the list | | Pagination | Done | Done | Pages behind an opaque cursor that holds an offset. The user loads more results with a button, not with infinite scroll. Focus moves to the first new wine | @@ -460,7 +460,7 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a ### Some known issues -- Search matches the text as one phrase, so "noir pinot" finds nothing. Country is not part of the search text, because the country filter covers it. +- Country is not part of the search text, because the country filter covers it. - The cursors in the API hold an offset, which is slower deep into large results. To be solved together with the larger dataset. - The database and its user are still called `vinmonopolet` and `vinmonopolet_app`, from before we switched dataset. Renaming would mean everyone rebuilding their database, so we kept the names. - Reviews from before login have no account and are shown as «Anonym student». The old `student` table they belong to will be removed later. diff --git a/backend/src/wine-query.test.ts b/backend/src/wine-query.test.ts index 9b4c422..53f7394 100644 --- a/backend/src/wine-query.test.ts +++ b/backend/src/wine-query.test.ts @@ -6,6 +6,7 @@ import { encodeCursor, MAX_FACET_VALUES, MAX_PAGE_SIZE, + MAX_SEARCH_WORDS, TOP_RATED_MIN_COUNT, } from './wine-query'; @@ -34,7 +35,26 @@ describe('buildWinesQuery', () => { it('never puts the search text in the SQL itself', () => { const query = buildWinesQuery({ search: "'; DROP TABLE wine; --" }); expect(query.listSql).not.toContain('DROP TABLE'); - expect(query.listParams[0]).toBe("%'; drop table wine; --%"); + expect(query.countParams).toEqual(["%';%", '%drop%', '%table%', '%wine;%', '%--%']); + }); + + it('requires each word of the search, in any order', () => { + const query = buildWinesQuery({ search: 'Noir pinot' }); + expect(query.countSql).toContain('WHERE search_text ILIKE $1 AND search_text ILIKE $2'); + expect(query.countParams).toEqual(['%noir%', '%pinot%']); + }); + + it('uses a repeated word once', () => { + expect(buildWinesQuery({ search: 'pinot Pinot' }).countParams).toEqual(['%pinot%']); + }); + + it('ignores a search with only spaces', () => { + expect(buildWinesQuery({ search: ' ' }).countSql).not.toContain('WHERE'); + }); + + it(`uses at most ${MAX_SEARCH_WORDS} words`, () => { + const search = Array.from({ length: 30 }, (_, index) => `word${index}`).join(' '); + expect(buildWinesQuery({ search }).countParams).toHaveLength(MAX_SEARCH_WORDS); }); it('makes the search case and accent insensitive', () => { diff --git a/backend/src/wine-query.ts b/backend/src/wine-query.ts index d4c5a33..52189f4 100644 --- a/backend/src/wine-query.ts +++ b/backend/src/wine-query.ts @@ -72,6 +72,17 @@ function escapeLike(text: string): string { return text.replace(/[\\%_]/g, (char) => `\\${char}`); } +/** More words than this are ignored, so one request cannot ask for hundreds of conditions. */ +export const MAX_SEARCH_WORDS = 10; + +/** The words of the search text, lowercase and without accents. A word is listed once. */ +function toSearchWords(search: string | null | undefined): string[] { + const words = toSearchText(search ?? '') + .split(' ') + .filter(Boolean); + return [...new Set(words)].slice(0, MAX_SEARCH_WORDS); +} + /** Adds a value to params and returns its placeholder ($1, $2, ...). Never put user input in the SQL string. */ function addParam(params: unknown[], value: unknown): string { params.push(value); @@ -83,8 +94,11 @@ function buildWhere(args: FilterArgs, params: unknown[], omit?: keyof WineFilter const where: string[] = []; const param = (value: unknown) => addParam(params, value); - const search = toSearchText(args.search ?? ''); - if (search) where.push(`search_text ILIKE ${param(`%${escapeLike(search)}%`)}`); + // One condition per word, so the words can come in any order and with other words between + // them: «noir pinot» finds Pinot Noir. Each condition can use the trigram index. + for (const word of toSearchWords(args.search)) { + where.push(`search_text ILIKE ${param(`%${escapeLike(word)}%`)}`); + } const filter: WineFilter = { ...args.filter, ...(omit && { [omit]: null }) }; if (filter.types?.length) where.push(`type = ANY(${param(filter.types)})`);