-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindexr.js
More file actions
85 lines (77 loc) · 1.9 KB
/
indexr.js
File metadata and controls
85 lines (77 loc) · 1.9 KB
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
77
78
79
80
81
82
83
84
85
require('dotenv').config({
path: `.env`,
});
const Typesense = require('typesense');
const { request } = require("graphql-request");
const pageQuery = `
query {
tutorials: allMarkdownRemark(
filter: {
fileAbsolutePath: { regex: "/tutorials/" }
}
) {
edges {
node {
headings(depth: h3) {
value
}
objectID: id
frontmatter {
title
search_keyword
contextual_links {
type
name
url
}
}
fields {
slug
}
excerpt(pruneLength: 6700)
}
}
}
}
`;
function pageToTypesenseRecord({ node }) {
const { id, frontmatter, ...rest } = node;
console.log('node', node);
return {
objectID: id,
...frontmatter,
...rest,
};
}
async function indexData() {
try {
const client = new Typesense.Client({
nodes: [
{
host: process.env.TYPESENSE_HOST,
port: process.env.TYPESENSE_PORT,
protocol: process.env.TYPESENSE_PROTOCOL,
},
],
apiKey: process.env.TYPESENSE_API_KEY,
});
const response = await request('http://localhost:8001/___graphql', pageQuery);
console.log('response', response);
const data = await response;
const records = data.tutorials.edges.map(pageToTypesenseRecord);
await client.collections(process.env.TYPESENSE_COLLECTION).documents().import(records).then((typesenseResponse) => {
// check the output of the response in the console
console.log('typesenseResponse', typesenseResponse);
console.log(
`🎉 Successfully indexed records to Typesense search.`
);
})
.catch((error) => {
console.error(error);
});
console.log('Indexing complete!');
} catch (error) {
console.error('Indexing error:', error);
}
}
indexData();