-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateTypesenseIndex.js
More file actions
62 lines (55 loc) · 1.66 KB
/
Copy pathpopulateTypesenseIndex.js
File metadata and controls
62 lines (55 loc) · 1.66 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
const Typesense = require('typesense');
module.exports = (async () => {
const typesense = new Typesense.Client({
nodes: [
{
host: 'localhost',
port: '8108',
protocol: 'http',
},
],
apiKey: 'xyz',
});
const schema = {
name: 'recipes',
fields: [
{ name: 'title', type: 'string' },
{ name: 'ingredients', type: 'string[]', facet: true },
{ name: 'url', type: 'string' },
{ name: 'type', type: 'string', facet: true },
{ name: 'dietary_restrictions', type: 'string[]', facet: true },
{ name: 'preparation_time', type: 'int32', facet: true},
{ name: 'servings', type: 'int32', facet: true },
{ name: 'image_url', type: 'string' }
]
};
console.log('Realizando la indexación en Typesense');
try {
await typesense.collections('recipes').delete();
console.log('Eliminando colección anterior.');
} catch (error) {
// Do nothing
}
console.log('Creando el esquema: ');
console.log(JSON.stringify(schema, null, 2));
await typesense.collections().create(schema);
console.log('Agregando las recetas: ');
const recipes = require('./data/recipes.json');
try {
const returnData = await typesense
.collections('recipes')
.documents()
.import(recipes);
console.log(returnData);
console.log('Recetas indexadas correctamente.');
const failedItems = returnData.filter(item => item.success === false);
if (failedItems.length > 0) {
throw new Error(
`Error al indexar los items ${JSON.stringify(failedItems, null, 2)}`
);
}
return returnData;
} catch (error) {
console.log(error);
}
})();