Skip to content

Commit

Permalink
Rework getEditedPartQuery into getEditedPartEntries
Browse files Browse the repository at this point in the history
This allows us to also add relevent extra entries (e.g. `min-reverseLinks.totalItems` when searching for qualifiers)
  • Loading branch information
johanbissemattsson committed Dec 13, 2024
1 parent 22caa24 commit fe95677
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
16 changes: 10 additions & 6 deletions lxl-web/src/routes/api/[[lang=lang]]/supersearch/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand All @@ -35,24 +39,27 @@ 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(
([, key]) => key === qualifierKey
)?.[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;

0 comments on commit fe95677

Please sign in to comment.