diff --git a/index.js b/index.js index 2583e57..4e7d8bb 100644 --- a/index.js +++ b/index.js @@ -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 = { diff --git a/lib/createIndexIfNotExists.js b/lib/createIndexIfNotExists.js index 4e37327..9983e47 100644 --- a/lib/createIndexIfNotExists.js +++ b/lib/createIndexIfNotExists.js @@ -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); } }; diff --git a/test/test.elasticsearch.js b/test/test.elasticsearch.js index e886e19..52bff40 100644 --- a/test/test.elasticsearch.js +++ b/test/test.elasticsearch.js @@ -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(); @@ -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();