-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
populateTypesenseIndex.js
171 lines (157 loc) · 4.19 KB
/
populateTypesenseIndex.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require('dotenv').config();
const Typesense = require('typesense');
module.exports = (async () => {
// Create a client
const typesense = new Typesense.Client({
nodes: [
{
host: process.env.TYPESENSE_HOST,
port: process.env.TYPESENSE_PORT,
protocol: process.env.TYPESENSE_PROTOCOL,
},
],
apiKey: process.env.TYPESENSE_ADMIN_API_KEY,
connectionTimeoutSeconds: 60 * 60,
});
const schema = {
name: 'products',
num_documents: 0,
fields: [
{
name: 'name',
type: 'string',
facet: false,
},
{
name: 'description',
type: 'string',
facet: false,
},
{
name: 'brand',
type: 'string',
facet: true,
},
{
name: 'categories',
type: 'string[]',
facet: true,
},
{
name: 'categories.lvl0',
type: 'string[]',
facet: true,
},
{
name: 'categories.lvl1',
type: 'string[]',
facet: true,
optional: true,
},
{
name: 'categories.lvl2',
type: 'string[]',
facet: true,
optional: true,
},
{
name: 'categories.lvl3',
type: 'string[]',
facet: true,
optional: true,
},
{
name: 'price',
type: 'float',
facet: true,
},
{
name: 'popularity',
type: 'int32',
facet: false,
},
{
name: 'free_shipping',
type: 'bool',
facet: true,
},
{
name: 'rating',
type: 'int32',
facet: true,
},
{
name: 'vectors',
type: 'float[]',
num_dim: 384,
},
// This is a field that exists in the document, but we only want to use it for display purposes and not search purposes.
// So we can just leave it off the schema, but still send it in the document.
// These fields are considered un-indexed fields, and stored on disk and returned when the document is a hit.
// These un-indexed fields do not count towards RAM usage.
// {
// name: 'image',
// type: 'string',
// facet: false,
// },
],
default_sorting_field: 'popularity',
};
console.log('Populating index in Typesense');
const products = require('./data/ecommerce-with-vectors.json');
let reindexNeeded = false;
try {
const collection = await typesense.collections('products').retrieve();
console.log('Found existing schema');
// console.log(JSON.stringify(collection, null, 2));
if (
collection.num_documents !== products.length ||
process.env.FORCE_REINDEX === 'true'
) {
console.log('Deleting existing schema');
reindexNeeded = true;
await typesense.collections('products').delete();
}
} catch (e) {
reindexNeeded = true;
}
if (!reindexNeeded) {
return true;
}
console.log('Creating schema: ');
console.log(JSON.stringify(schema, null, 2));
await typesense.collections().create(schema);
// const collectionRetrieved = await typesense
// .collections("products")
// .retrieve();
// console.log("Retrieving created schema: ");
// console.log(JSON.stringify(collectionRetrieved, null, 2));
console.log('Adding records: ');
// Bulk Import
products.forEach((product) => {
product.free_shipping = product.name.length % 2 === 1; // We need this to be deterministic for tests
product.rating = (product.description.length % 5) + 1; // We need this to be deterministic for tests
product.categories.forEach((category, index) => {
product[`categories.lvl${index}`] = [
product.categories.slice(0, index + 1).join(' > '),
];
});
});
try {
const returnData = await typesense
.collections('products')
.documents()
.import(products);
console.log(returnData);
console.log('Done indexing.');
const failedItems = returnData.filter((item) => item.success === false);
if (failedItems.length > 0) {
throw new Error(
`Error indexing items ${JSON.stringify(failedItems, null, 2)}`
);
}
return returnData;
} catch (error) {
console.log(error);
}
})();