Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ const register = function(server, pluginOptions) {
};

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 = {
Expand Down
18 changes: 11 additions & 7 deletions lib/createIndexIfNotExists.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ module.exports = async function(opts) {
//obj: { index }
opts.index = opts.index;

if (options.log || !client) {
server.log(['search', 'info'], {
message: 'creating index',
opts
});
}
if (!client) {
return;
}
const indexExists = await client.indices.exists({ index: opts.index });
if (!indexExists) {
await server.search.createIndex({ index: opts.index, log: options.elasticLog });
if (options.log || !client) {
server.log(['search', 'info'], {
message: 'creating index',
opts
});
}
const params = { index: opts.index, log: options.elasticLog };
if (opts.settings) {
params.body = opts.settings;
}
await server.search.createIndex(params);
}
};
34 changes: 32 additions & 2 deletions test/test.elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ const Hapi = require('hapi');
const plugin = require('../index.js');

tap.test('can load plugin', async (t) => {
const server = new Hapi.Server({ port: 8080 });
const server = new Hapi.Server({ port: 8080, debug: { log: '*' } });
const host = process.env.ELASTICSEARCH_HOST;
const rando = Math.floor(Math.random() * 1000);
await server.register({
plugin,
options: {
host: `${host}:9200`,
elasticLog: 'debug',
index: 'othertestindx'
index: `othertestindx_${rando}`
}
});
await server.start();
Expand All @@ -19,6 +20,35 @@ tap.test('can load plugin', async (t) => {
await server.search.addToIndex({ type: 'doc', id: Math.floor(Math.random() * 10000), data: { tree: 'leafs', gone: 'fork' } });


await new Promise(resolve => setTimeout(resolve, 2000));
await server.stop();

t.end();
});

tap.test('autocreate index with settings', async (t) => {
const server = new Hapi.Server({ port: 8080, debug: { log: '*' } });
const host = process.env.ELASTICSEARCH_HOST;
const rando = Math.floor(Math.random() * 1000);
await server.register({
plugin,
options: {
host: `${host}:9200`,
elasticLog: 'debug',
index: `settingsindex_${rando}`,
indexSettings: {
settings: {
index: {
number_of_replicas: 2
}
}
}
}
});
await server.start();

await server.search.addToIndex({ type: 'doc', id: Math.floor(Math.random() * 10000), data: { tree: 'leafs', gone: 'fork' } });

await new Promise(resolve => setTimeout(resolve, 2000));
await server.stop();

Expand Down