-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.js
166 lines (137 loc) · 3.31 KB
/
es.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
'use strict';
require('dotenv').config()
var fs = require('fs');
var elasticsearch = require('elasticsearch');
const es = elasticsearch.Client({
host: process.env.elasticsearch,
log: 'trace',
"apiVersion": "6.8"
});
function indexExists(indexName) {
return es.indices.exists({
index: indexName
});
}
function createIndex(indexName) {
return es.indices.create({
index: indexName
});
}
function deleteIndex(indexName) {
return es.indices.delete({
index: indexName
});
}
function getDefaultObject(keyword) {
return {
keyword: keyword,
weight: 1
}
}
function indexMapping(indexName) {
return es.indices.putMapping({
index: indexName,
type: 'doc',
body: {
properties: {
keyword: {
type: "completion",
analyzer: "simple",
search_analyzer: "simple"
},
weight: {
type: "integer"
},
meta: {
type: 'object'
}
}
}
});
}
function addDocument(indexName, documents) {
var items = [];
documents.forEach(element => {
let keyCopy = getTokenizedArray(element.keyword);
element.keyword = { input: keyCopy, weight: element.weight };
items.push({ index: { _index: indexName } }, element);
});
return es.bulk({
body: items,
type: 'doc',
refresh: "true"
});
}
function getTokenizedArray(keyword) {
let toReturn = [];
let split = keyword.split(' ');
for (var i = 0; i < split.length; i++) {
let toAdd = split.slice(i).join(' ');
if (toAdd && toAdd.trim()) {
toReturn.push(toAdd)
}
}
return toReturn;
}
function deleteDocument(indexName, docId) {
return es.delete({
index: indexName,
id: docId,
type: 'doc',
refresh: "true"
});
}
function getSuggestions(indexName, text, size) {
return es.search({
index: indexName,
type: 'doc',
body: {
suggest: {
theSuggester: {
prefix: text,
completion: {
field: "keyword",
size: size,
fuzzy: {
fuzziness: "auto"
}
}
}
}
}
});
}
function deleteByQuery(indexName, query) {
return es.deleteByQuery({
index: indexName,
type: 'doc',
body: {
query: {
match: query
}
}
});
}
function getStat(indexName, id) {
return es.search({
index: indexName,
type: 'doc',
body: {
query: {
term: {
"_id": id
}
}
}
});
}
exports.deleteIndex = deleteIndex;
exports.createIndex = createIndex;
exports.indexExists = indexExists;
exports.indexMapping = indexMapping;
exports.addDocument = addDocument;
exports.deleteDocument = deleteDocument;
exports.getSuggestions = getSuggestions;
exports.getStat = getStat;
exports.getDefaultObject = getDefaultObject;
exports.deleteByQuery = deleteByQuery;