Repository navigation
Expand file tree
/
Copy pathcreateAllMappings.js
More file actions
115 lines (106 loc) · 2.93 KB
/
Copy pathcreateAllMappings.js
File metadata and controls
115 lines (106 loc) · 2.93 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
const { OpenAI } = require("openai");
const BluebirdPromise = require("bluebird");
const request = require("request");
const fs = require("fs");
const { Client } = require("@elastic/elasticsearch");
const client = new Client({
node: "http://localhost:9200",
});
require("dotenv").config();
const allMappings = [
{
index: "knowledgearticle",
properties: {
"knowledge-vector": {
type: "dense_vector",
dims: 1536,
index: true,
similarity: "cosine",
},
},
},
{
index: "faqs",
properties: {
"faqs-vector": {
type: "dense_vector",
dims: 1536,
index: true,
similarity: "cosine",
},
},
},
{
index: "vacancies",
properties: {
"vacancy-vector": {
type: "dense_vector",
dims: 1536,
index: true,
similarity: "cosine",
},
},
},
{
index: "personess",
properties: {
"personess-vector": {
type: "dense_vector",
dims: 1536,
index: true,
similarity: "cosine",
},
},
},
];
const hasIndex = (index) => new Promise((resolve, reject) => {
client.indices.exists({ index })
.then((res) => {
console.log(`is ${index} exists :`, res);
return resolve(res);
})
.catch(err => reject(err));
});
const createIndex = (index) => new Promise((resolve, reject) => {
client.indices.create({ index })
.then((res) => resolve())
.catch(err => reject(err));
});
const deleteIndex = (index) => new Promise((resolve, reject) => {
client.indices.delete({index})
.then(res => resolve())
.catch(err => reject(err));
});
const createMappings = (data) => new Promise((resolve, reject) => {
client.indices.putMapping({ index: data.index, properties: data.properties })
.then((res) => resolve())
.catch(err => reject(err));
});
const checkAndCreateIndexMappings = (data, deleteAndRecreate = false) => new Promise((resolve, reject) => {
hasIndex(data.index)
.then(exists => {
if(!exists) return false;
return deleteIndex(data.index);
})
.then((exists) => {
if (!exists) return createIndex(data.index);
})
.then(() => createMappings(data))
.then(() => resolve())
.catch(err => reject(err));
})
const putIndexMappings = (deleteAndRecreate = false) => new Promise((resolve, reject) => {
BluebirdPromise.mapSeries(allMappings, (rec) => new Promise((resolve, reject) => {
console.log('creating mapping for :', rec.index);
return resolve(checkAndCreateIndexMappings(rec));
}))
.then(() => resolve())
.catch(err => reject(err));
});
putIndexMappings() // pass `true` if u want to delete and recreate indices.
.then(() => {
console.log('mappings created....');
})
.catch(err => {
console.error('Something went wrong!!! ', err);
});