-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.33 KB
/
Copy pathindex.js
File metadata and controls
56 lines (48 loc) · 1.33 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
const elasticsearch = require('elasticsearch');
const defaults = {
apiVersion: '6.0',
type: '_doc'
};
const register = function(server, pluginOptions) {
let client = null;
const options = Object.assign({}, defaults, pluginOptions);
if (options.host) {
client = new elasticsearch.Client({
host: options.host,
log: options.elasticLog,
apiVersion: '6.0'
});
}
const bind = {
server,
options,
client
};
const methods = {
addToIndex: require('./lib/add.js').bind(bind),
removeFromIndex: require('./lib/remove.js').bind(bind),
clearByType: require('./lib/clearByType.js').bind(bind),
query: require('./lib/query.js').bind(bind),
dropIndex: require('./lib/dropIndex.js').bind(bind),
createIndex: require('./lib/createIndex.js').bind(bind),
createIndexIfNotExists: require('./lib/createIndexIfNotExists.js').bind(bind),
client
};
server.decorate('server', 'search', methods);
if (pluginOptions.index) {
server.events.on('start', async () => {
const indexOpts = {
index: pluginOptions.index
};
if (pluginOptions.indexSettings) {
indexOpts.settings = pluginOptions.indexSettings;
}
await server.search.createIndexIfNotExists(indexOpts);
});
}
};
exports.plugin = {
register,
once: true,
name: 'search'
};