This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (225 loc) · 8.27 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const mongolastic = require("mongolastic");
class mlcl_elastic {
constructor() {
if (mlcl_elastic._instance) {
throw new Error("Error: Instantiation failed. Singleton module! Use .getInstance() instead of new.");
}
mlcl_elastic._instance = this;
mlcl_elastic.molecuel.on('mlcl::core::init:post', (molecuel) => {
this.config = molecuel.config.search;
this.connect((err, connection) => {
if (err) {
mlcl_elastic.molecuel.log.error('mlcl_elastic', 'Error while connecting' + err);
}
else {
this.connection = connection;
mlcl_elastic.molecuel.emit('mlcl::search::connection:success', this);
}
});
});
mlcl_elastic.molecuel.on('mlcl::database::registerModel:pre', (database, modelname, schema, options) => {
if (options.indexable) {
options.modelname = modelname;
schema.plugin(this.plugin, options);
mlcl_elastic.molecuel.emit('mlcl::elastic::registerPlugin:post', this, modelname, schema);
}
});
mlcl_elastic.molecuel.on('mlcl::database::registerModel:post', (database, modelname, model) => __awaiter(this, void 0, void 0, function* () {
mongolastic.registerModel(model, (err) => __awaiter(this, void 0, void 0, function* () {
if (err) {
mlcl_elastic.molecuel.log.error('mlcl_elastic', 'Error while registering model to elasticsearch' + err);
}
else {
if (!mlcl_elastic.models.has(modelname)) {
mlcl_elastic.models.set(modelname, model);
}
}
}));
}));
}
static getInstance() {
if (mlcl_elastic._instance === null) {
mlcl_elastic._instance = new mlcl_elastic();
}
return mlcl_elastic._instance;
}
static init(m) {
mlcl_elastic.molecuel = m;
return mlcl_elastic.getInstance();
}
connect(callback) {
mongolastic.connect(this.config.prefix, this.config, callback);
}
ensureIndex(modelname, callback) {
mongolastic.indices.exists((modelname, err, exists) => {
if (!exists) {
var mappings = {};
mappings[modelname] = {
properties: {
url: {
type: 'keyword',
index: true
},
'location': {
'properties': {
geo: {
type: 'geo_point',
'lat_lon': true
}
}
}
}
};
var settings = {};
mongolastic.indices.create(modelname, settings, mappings, (err) => {
if (err) {
mlcl_elastic.molecuel.log.error('mlcl_elastic', 'Error while creating indices' + err);
}
});
callback();
}
else {
callback();
}
});
}
index(modelname, entry, callback) {
mongolastic.index(modelname, entry, callback);
}
delete(modelname, entry, callback) {
mongolastic.delete(modelname, entry, callback);
}
deleteIndex(modelname, callback) {
mongolastic.deleteIndex(modelname, callback);
}
sync(model, modelname, callback) {
mongolastic.sync(model, modelname, callback);
}
search(query, callback) {
var elast = mlcl_elastic.getInstance();
if (query && query.index) {
var i = query.index.split(',');
query.index = i.map(function (index) {
return elast.getIndexName(index);
}).join(',');
}
mongolastic.search(query, callback);
}
searchByUrl(url, lang, callback) {
mlcl_elastic.getInstance().search({
body: {
from: 0,
size: 1,
query: {
bool: {
filter: {
term: {
url: url
}
},
should: [
{
term: {
lang: lang
}
},
{
bool: {
must_not: {
exists: {
field: 'lang'
}
}
}
}
]
}
}
}
}, callback);
}
searchById(id, callback) {
mlcl_elastic.getInstance().search({
body: {
query: {
term: {
_id: id
}
}
}
}, callback);
}
exists(indexname, callback) {
this.connection.indices.exists({
index: this.getIndexName(indexname),
}, callback);
}
create(indexname, settings, mappings, callback) {
var elast = mlcl_elastic.getInstance();
elast.connection.indices.create({
index: elast.getIndexName(indexname),
body: {
settings: settings,
mappings: mappings
}
}, callback);
}
checkCreateIndex(indexname, settings, mappings, callback) {
var elast = mlcl_elastic.getInstance();
elast.exists(indexname, function (err, response) {
if (!response) {
elast.create(indexname, settings, mappings, function (err) {
callback(err, true);
});
}
else {
callback(err, false);
}
});
}
getMapping(indexname, callback) {
var elast = mlcl_elastic.getInstance();
elast.connection.indices.getMapping({
index: elast.getIndexName(indexname)
}, callback);
}
plugin(schema, options) {
schema.plugin(mongolastic.plugin, options);
var mylastic = mlcl_elastic.getInstance();
schema.statics.searchByUrl = mylastic.searchByUrl;
schema.statics.searchById = mylastic.searchById;
schema.methods.searchByUrl = mylastic.searchByUrl;
schema.methods.searchById = mylastic.searchById;
}
getIndexName(name) {
var elast = mlcl_elastic.getInstance();
if (elast.config.prefix) {
if (name.indexOf(elast.config.prefix + '-') === 0) {
return name.toLowerCase();
}
else {
return elast.config.prefix + '-' + name.toLowerCase();
}
}
else {
return name.toLowerCase();
}
}
}
mlcl_elastic._instance = null;
mlcl_elastic.models = new Map();
function init(m) {
return mlcl_elastic.init(m);
}
;
module.exports = init;