-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindexr.js
More file actions
118 lines (106 loc) · 3.08 KB
/
indexr.js
File metadata and controls
118 lines (106 loc) · 3.08 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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: 100)
}
}
}
}
`;
function pageToTypesenseRecord({ node }) {
const { id, frontmatter, fields = {}, headings = [], ...rest } = node;
const formattedHeadings = headings.map(h => h.value || '').filter(Boolean);
return {
objectID: id,
title: frontmatter.title || '',
search_keyword: String(frontmatter.search_keyword || ''),
slug: fields.slug || '',
excerpt: frontmatter.excerpt || '',
headings: formattedHeadings,
...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,
});
let collectionExists = false;
try {
await client.collections(process.env.TYPESENSE_COLLECTION).retrieve();
collectionExists = true;
} catch (error) {
if (error.httpStatus !== 404) {
throw error;
}
}
if (collectionExists) {
await client.collections(process.env.TYPESENSE_COLLECTION).delete();
console.log(`Collection ${process.env.TYPESENSE_COLLECTION} deleted successfully.`);
}
await client.collections().create({
name: process.env.TYPESENSE_COLLECTION,
fields: [
{ name: 'objectID', type: 'string' },
{ name: 'title', type: 'string' },
{ name: 'search_keyword', type: 'string' },
{ name: 'slug', type: 'string' },
{ name: 'excerpt', type: 'string' },
{ name: 'headings', type: 'string[]', facet: false }
]
});
console.log(`Collection ${process.env.TYPESENSE_COLLECTION} created successfully.`);
const response = await request('http://127.0.0.1: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();