Skip to content

Commit

Permalink
improvement: add typesense with new endpoint in lib/data.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Aug 12, 2024
1 parent 8968fa4 commit ac8f721
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Client } from "typesense";

const client = new Client({
nodes: [
{
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
host: process.env.TYPESENSE_HOST!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
port: Number(process.env.TYPESENSE_PORT!),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
protocol: process.env.TYPESENSE_PROTOCOL!,
},
],
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: process.env.TYPESENSE_API_KEY!,
connectionTimeoutSeconds: 2,
});

const collection = client.collections("thomas-bernhard");

function getDistinct(field: string): Promise<Array<string>> {
return (
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, // 250 is the hard maximum for typesense
group_limit: 1, // we're not interested in the actual documents, but gotta retrieve at least 1..
})
.then((r) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return r
.grouped_hits!.map((l) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return l.group_key[0]!;
})
.sort();
})
);
}

export function getLanguages(): Promise<Array<string>> {
return getDistinct("language");
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"server-only": "^0.0.1",
"sharp": "^0.33.4",
"shiki": "^1.10.3",
"typesense": "^1.8.2",
"valibot": "^0.36.0"
},
"devDependencies": {
Expand Down
92 changes: 92 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac8f721

Please sign in to comment.