This commit is contained in:
seb
2026-07-19 19:43:23 +02:00
parent eea8e99358
commit 92dc32bf12
2 changed files with 24 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ Fuzzy article search. Use this to build a search box / result list with category
Pass category IDs as a comma-separated list: `filter=16,39`. Pass category IDs as a comma-separated list: `filter=16,39`.
Whenever `filter` is present, **`categories` always includes every id from `filter`**, even when `length` is `0` or a filtered id has no hits. Those entries use master-data `name` / `parent` and may have `count: 0`. Other facet categories with hits are still returned as usual. Whenever `filter` is present, **`categories` always includes every id from `filter` and its full ancestor chain** (parent → … → root), even when `length` is `0` or a filtered id has no hits. Those entries use master-data `name` / `parent` and may have `count: 0`. Other facet categories with hits are still returned as usual.
## Response `200` ## Response `200`
@@ -58,14 +58,15 @@ Whenever `filter` is present, **`categories` always includes every id from `filt
} }
``` ```
Empty result with an active filter still returns the applied category: Empty result with a nested filter still returns the leaf and its ancestors:
```json ```json
{ {
"length": 0, "length": 0,
"recordset": [], "recordset": [],
"categories": { "categories": {
"16": { "name": "Dünger", "count": 0, "parent": 0 } "16": { "name": "Dünger", "count": 0, "parent": 0 },
"39": { "name": "Erde", "count": 0, "parent": 16 }
} }
} }
``` ```

View File

@@ -77,18 +77,27 @@ function shapeCategories(counts, byId) {
return categories; return categories;
} }
/** Ensure every applied filter id appears in categories (count 0 if no hits). */ /**
function ensureFilterCategories(categories, filter, byId) { * Ensure every applied filter id and its ancestors appear in categories
* (count 0 if no hits), so the frontend can render hierarchy via parent links.
*/
function ensureFilterCategories(categories, filter, byId, parentMap) {
for (const id of filter) { for (const id of filter) {
if (id === '' || id === null || categories[id]) { if (id === '' || id === null) {
continue; continue;
} }
const meta = byId[id] ?? {}; for (const ancestorId of getUpPath(id, parentMap)) {
categories[id] = { const key = String(ancestorId);
count: 0, if (categories[key]) {
name: meta.name ?? null, continue;
parent: meta.parent ?? null, }
}; const meta = byId[ancestorId] ?? byId[key] ?? {};
categories[key] = {
count: 0,
name: meta.name ?? null,
parent: meta.parent ?? null,
};
}
} }
return categories; return categories;
} }
@@ -206,7 +215,8 @@ export async function searchArticles(target, filter = [''], offset = 0, maxCount
categoryData.byId categoryData.byId
), ),
filter, filter,
categoryData.byId categoryData.byId,
categoryData.parentMap
); );
const length = result.recordset.length; const length = result.recordset.length;
const recordset = result.recordset const recordset = result.recordset