From f176188cf8da15786bc56eee3c67f05a22b16514 Mon Sep 17 00:00:00 2001 From: Kevin Stadler Date: Mon, 2 Sep 2024 09:59:36 +0200 Subject: [PATCH] Remove custom getCounts() function for FacetedListing The count function required the grouped fields to be sortable, but nested fields (in particular contains.translators) can not be set sortable. A plain faceted query supports unsortable fields. --- components/faceted-listing.tsx | 12 +++++------- lib/data.ts | 24 ------------------------ 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/components/faceted-listing.tsx b/components/faceted-listing.tsx index 0c281b5..49e5a5a 100644 --- a/components/faceted-listing.tsx +++ b/components/faceted-listing.tsx @@ -4,7 +4,7 @@ import * as v from "valibot"; import { MainContent } from "@/components/main-content"; import { ClickablePublicationThumbnail } from "@/components/publication-cover"; import { PublicationGrid } from "@/components/publication-grid"; -import { getCounts, getFaceted } from "@/lib/data"; +import { getFaceted } from "@/lib/data"; import { AppNavLink } from "./app-nav-link"; @@ -43,28 +43,26 @@ export async function FacetedListing(props: FacetedListingProps) { facetValue: searchParams.get(props.facet), }); - const counts = await getCounts(props.facet); const data = await getFaceted([props.facet], safeParams.facetValue, safeParams.page); const publications = data.hits?.map((h) => { return h.document; }); - // {props.facet} listing return (
- {Object.entries(counts).map(([category, count]) => { + {data.facet_counts?.[0]?.counts.map(({ count, value }) => { // ugly but only way to overwrite rather than append? - searchParams.set(props.facet, category); + searchParams.set(props.facet, value); return ( -
  • +
  • - {category} + {value} {" "} ({count})
  • diff --git a/lib/data.ts b/lib/data.ts index 63c6054..6d844c0 100644 --- a/lib/data.ts +++ b/lib/data.ts @@ -24,30 +24,6 @@ const client = new Client({ // needs to match the collection name in scripts/typesense-schema.json const collection = client.collections("thomas-bernhard"); -export async function getCounts(field: string): Promise> { - const r = await collection - .documents() - // this does not work as expected when grouping by an array field (or a nested field which has - // an object[] anywhere in its path) because grouping then happens by the array of values, not - // by individual values. for example: await getDistinct("contains.work.title") - .search({ - q: "*", - // query_by: field, - group_by: field, - per_page: 250, // limit for the number of groups (250 is the hard maximum for typesense) - sort_by: `${field}:asc`, // if you omit this it will not sort the groups, i.e. only consider the first 250 *documents*, which means only ~10 groups will come out! - group_limit: 1, // we're not interested in the actual documents, but gotta retrieve at least 1 per group.. - }); - - return Object.fromEntries( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - r.grouped_hits!.map((group) => { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return [group.group_key[0]!, group.found!]; - }), - ); -} - export async function getFaceted( facetFields: Array, search = "*",