From fe95677805ce7fb706cfa74250eee4d794a0daf6 Mon Sep 17 00:00:00 2001 From: Johan Bisse Mattsson Date: Fri, 13 Dec 2024 14:46:04 +0100 Subject: [PATCH] Rework getEditedPartQuery into getEditedPartEntries This allows us to also add relevent extra entries (e.g. `min-reverseLinks.totalItems` when searching for qualifiers) --- .../api/[[lang=lang]]/supersearch/+server.ts | 16 ++++++++------ ...edPartQuery.ts => getEditedPartEntries.ts} | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) rename lxl-web/src/routes/api/[[lang=lang]]/supersearch/{getEditedPartQuery.ts => getEditedPartEntries.ts} (66%) diff --git a/lxl-web/src/routes/api/[[lang=lang]]/supersearch/+server.ts b/lxl-web/src/routes/api/[[lang=lang]]/supersearch/+server.ts index f85ed62ae..141852097 100644 --- a/lxl-web/src/routes/api/[[lang=lang]]/supersearch/+server.ts +++ b/lxl-web/src/routes/api/[[lang=lang]]/supersearch/+server.ts @@ -4,7 +4,7 @@ import type { RequestHandler } from './$types.ts'; import { LxlLens } from '$lib/types/display'; import { getSupportedLocale } from '$lib/i18n/locales.js'; import { toString } from '$lib/utils/xl.js'; -import getEditedPartQuery from './getEditedPartQuery.js'; +import getEditedPartEntries from './getEditedPartEntries.js'; export const GET: RequestHandler = async ({ url, params, locals }) => { const displayUtil = locals.display; @@ -13,13 +13,17 @@ export const GET: RequestHandler = async ({ url, params, locals }) => { const _q = url.searchParams.get('_q'); const cursor = parseInt(url.searchParams.get('cursor') || '0', 10); - if (_q && Number.isInteger(cursor)) { - const editedPartQuery = getEditedPartQuery(_q, cursor); + const newSearchParams = new URLSearchParams([...Array.from(url.searchParams.entries())]); - url.searchParams.set('_q', editedPartQuery); - url.searchParams.delete('cursor'); + if (_q && Number.isInteger(cursor)) { + const editedPartEntries = getEditedPartEntries(_q, cursor); - console.log('Search params sent to /find:', decodeURIComponent(url.searchParams.toString())); + editedPartEntries.forEach(([key, value]) => { + newSearchParams.set(key, value); + }); + newSearchParams.delete('cursor'); + console.log('Initial search params:', decodeURIComponent(url.searchParams.toString())); + console.log('Search params sent to /find:', decodeURIComponent(newSearchParams.toString())); } const findResponse = await fetch(`${env.API_URL}/find?${url.searchParams.toString()}`); diff --git a/lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartQuery.ts b/lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartEntries.ts similarity index 66% rename from lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartQuery.ts rename to lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartEntries.ts index 79cacbfc0..0713e7a0b 100644 --- a/lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartQuery.ts +++ b/lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartEntries.ts @@ -17,7 +17,11 @@ const QUALIFIER_KEY_BY_BASE_CLASS = { const SKIP_QUALIFIERS = ['år']; -function getEditedPartQuery(query: string, cursor: number): string { +/** + * Gets the URLSearchParams entries which should be appended/replaced with new values when editing a part of a query. + */ + +function getEditedPartEntries(query: string, cursor: number): [string, string][] { const editedRanges = getEditedRanges(query, cursor); /** @@ -35,7 +39,7 @@ function getEditedPartQuery(query: string, cursor: number): string { ); if (SKIP_QUALIFIERS.includes(qualifierKey.toLowerCase())) { - return query; // Keep query as is when editing year qualifiers + return []; // Keep query as is when editing year qualifiers } const baseClass = Object.entries(QUALIFIER_KEY_BY_BASE_CLASS).find( @@ -43,16 +47,19 @@ function getEditedPartQuery(query: string, cursor: number): string { )?.[0]; if (baseClass) { - return qualifierValue + `"rdf:type":${baseClass}`; + return [ + ['_q', qualifierValue + `"rdf:type":${baseClass}`], + ['min-reverseLinks.totalItems', '1'] // ensure results are linked/used atleast once + ]; } - return qualifierKey + qualifierOperator + qualifierValue; + return [['_q', qualifierKey + qualifierOperator + qualifierValue]]; } /** - * Otherwise keep query as is... + * Otherwise keep query entries as is */ - return query; + return []; } -export default getEditedPartQuery; +export default getEditedPartEntries;