Skip to content

Commit

Permalink
Update algolia
Browse files Browse the repository at this point in the history
  • Loading branch information
ertrzyiks committed Jan 4, 2025
1 parent c537585 commit 1143450
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
16 changes: 7 additions & 9 deletions scripts/reindex.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import algoliasearch from "algoliasearch";
import { algoliasearch } from "algoliasearch";
import "dotenv/config";

const contentEndpoint =
"https://api-eu-central-1.hygraph.com/v2/ckzhgf7f30mi901xs88ok02gc/master";
const client = algoliasearch("J8YFF4CZ4C", process.env.ALGOLIA_ADMIN_KEY);
const indexName = process.env.ALGOLIA_SEARCH_INDEX || "prod_recipes";

const updatedAt = new Date().getTime();

Expand Down Expand Up @@ -54,18 +55,14 @@ function loadRecipes(after = null, limit = 100) {
}
}
`,
{ after, limit },
{ after, limit }
);
}

async function main() {
let after = null;
const limit = 100;

const index = client.initIndex(
process.env.ALGOLIA_SEARCH_INDEX || "prod_recipes",
);

do {
const { data: recipesData, errors } = await loadRecipes(after, limit);

Expand All @@ -83,16 +80,17 @@ async function main() {
updated_at: updatedAt,
}));

await index.saveObjects(dataset, { autoGenerateObjectIDIfNotExist: true });
await client.saveObjects({ indexName, objects: dataset });

after =
recipesData.recipes.length === limit
? recipesData.recipes[recipesData.recipes.length - 1].id
: null;
} while (after);

await index.deleteBy({
filters: "updated_at < " + updatedAt,
await client.deleteBy({
indexName,
deleteByParams: { filters: "updated_at < " + updatedAt },
});
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/search.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { PrismaClient } from "@prisma/client";
import algoliasearch from "algoliasearch";
import { algoliasearch } from "algoliasearch";

const prisma = new PrismaClient();
const client = algoliasearch("J8YFF4CZ4C", process.env.ALGOLIA_SEARCH_KEY);

const index = client.initIndex(
process.env.ALGOLIA_SEARCH_INDEX || "prod_recipes",
);
const indexName = process.env.ALGOLIA_SEARCH_INDEX || "prod_recipes";

export async function search(query: string) {
const { hits } = await index.search<{ slug: string }>(query);
const { hits } = await client.searchSingleIndex<{ slug: string }>({
indexName,
searchParams: { query },
});

const slugs = hits.map((hit) => hit.slug);

Expand Down

0 comments on commit 1143450

Please sign in to comment.