-
Notifications
You must be signed in to change notification settings - Fork 0
/
reindex.mjs
76 lines (61 loc) · 1.87 KB
/
reindex.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import algoliasearch from 'algoliasearch'
const contentEndpoint = 'https://api-eu-central-1.hygraph.com/v2/ckzhgf7f30mi901xs88ok02gc/master'
const client = algoliasearch('J8YFF4CZ4C', process.env.ALGOLIA_ADMIN_KEY)
const updatedAt = new Date().getTime()
async function graphql(query, variables = {}) {
const res = await fetch(contentEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: process.env.HYGRAPH_AUTH_TOKEN
},
body: JSON.stringify({
query,
variables,
}),
})
return res.json()
}
function loadRecipes(after = null, limit = 100) {
return graphql(`
query LoadRecipes($after: String, $limit: Int) {
recipes(first: $limit, after: $after, where: { documentInStages_some: { stage: PUBLISHED } }) {
id
title
slug
headline
ingredients {
id
content
}
instructions {
id
content
}
}
}
`, { after, limit })
}
async function main() {
let after = null
const limit = 100
const index = client.initIndex('prod_recipes')
do {
const { data: recipesData } = await loadRecipes(after, limit)
const dataset = recipesData.recipes.map(recipe => ({
objectID: recipe.id,
slug: recipe.slug,
title: recipe.title,
description: recipe.headline,
content: recipe.ingredients.map(ingredient => ingredient.content).join(' ') + ' ' + recipe.instructions.map(instruction => instruction.content).join(' '),
updated_at: updatedAt
}))
await index.saveObjects(dataset, { autoGenerateObjectIDIfNotExist: true })
after = recipesData.recipes.length === limit ? recipesData.recipes[recipesData.recipes.length - 1].id : null
} while (after)
await index.deleteBy({
filters: 'updated_at < ' + updatedAt
})
}
main()