Skip to content

Commit

Permalink
Remove custom getCounts() function for FacetedListing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kevinstadler committed Sep 2, 2024
1 parent 467036f commit f176188
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
12 changes: 5 additions & 7 deletions components/faceted-listing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
});

// <PageTitle>{props.facet} listing</PageTitle>
return (
<MainContent>
<div>
{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 (
<li key={category}>
<li key={value}>
<AppNavLink
href={
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`?${searchParams}`
}
>
{category}
{value}
</AppNavLink>{" "}
({count})
</li>
Expand Down
24 changes: 0 additions & 24 deletions lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, number>> {
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<number>(
// 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<string>,
search = "*",
Expand Down

0 comments on commit f176188

Please sign in to comment.