Skip to content

Commit b9be0ea

Browse files
authored
Merge pull request #7 from firstandthird/bug-zealous-logs
Fixing over-zealous logs
2 parents 69151d8 + dc0e3e5 commit b9be0ea

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ const register = function(server, pluginOptions) {
3434
};
3535

3636
server.decorate('server', 'search', methods);
37+
if (pluginOptions.index) {
38+
server.events.on('start', async () => {
39+
const indexOpts = {
40+
index: pluginOptions.index
41+
};
42+
43+
if (pluginOptions.indexSettings) {
44+
indexOpts.settings = pluginOptions.indexSettings;
45+
}
46+
47+
await server.search.createIndexIfNotExists(indexOpts);
48+
});
49+
}
3750
};
3851

3952
exports.plugin = {

lib/createIndexIfNotExists.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ module.exports = async function(opts) {
66
//obj: { index }
77
opts.index = opts.index;
88

9-
if (options.log || !client) {
10-
server.log(['search', 'info'], {
11-
message: 'creating index',
12-
opts
13-
});
14-
}
159
if (!client) {
1610
return;
1711
}
1812
const indexExists = await client.indices.exists({ index: opts.index });
1913
if (!indexExists) {
20-
await server.search.createIndex({ index: opts.index, log: options.elasticLog });
14+
if (options.log || !client) {
15+
server.log(['search', 'info'], {
16+
message: 'creating index',
17+
opts
18+
});
19+
}
20+
const params = { index: opts.index, log: options.elasticLog };
21+
if (opts.settings) {
22+
params.body = opts.settings;
23+
}
24+
await server.search.createIndex(params);
2125
}
2226
};

test/test.elasticsearch.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ const Hapi = require('hapi');
33
const plugin = require('../index.js');
44

55
tap.test('can load plugin', async (t) => {
6-
const server = new Hapi.Server({ port: 8080 });
6+
const server = new Hapi.Server({ port: 8080, debug: { log: '*' } });
77
const host = process.env.ELASTICSEARCH_HOST;
8+
const rando = Math.floor(Math.random() * 1000);
89
await server.register({
910
plugin,
1011
options: {
1112
host: `${host}:9200`,
1213
elasticLog: 'debug',
13-
index: 'othertestindx'
14+
index: `othertestindx_${rando}`
1415
}
1516
});
1617
await server.start();
@@ -19,6 +20,35 @@ tap.test('can load plugin', async (t) => {
1920
await server.search.addToIndex({ type: 'doc', id: Math.floor(Math.random() * 10000), data: { tree: 'leafs', gone: 'fork' } });
2021

2122

23+
await new Promise(resolve => setTimeout(resolve, 2000));
24+
await server.stop();
25+
26+
t.end();
27+
});
28+
29+
tap.test('autocreate index with settings', async (t) => {
30+
const server = new Hapi.Server({ port: 8080, debug: { log: '*' } });
31+
const host = process.env.ELASTICSEARCH_HOST;
32+
const rando = Math.floor(Math.random() * 1000);
33+
await server.register({
34+
plugin,
35+
options: {
36+
host: `${host}:9200`,
37+
elasticLog: 'debug',
38+
index: `settingsindex_${rando}`,
39+
indexSettings: {
40+
settings: {
41+
index: {
42+
number_of_replicas: 2
43+
}
44+
}
45+
}
46+
}
47+
});
48+
await server.start();
49+
50+
await server.search.addToIndex({ type: 'doc', id: Math.floor(Math.random() * 10000), data: { tree: 'leafs', gone: 'fork' } });
51+
2252
await new Promise(resolve => setTimeout(resolve, 2000));
2353
await server.stop();
2454

0 commit comments

Comments
 (0)