From 70aaa4f727eedc6fd1f3002de268dbbbe52cc1ea Mon Sep 17 00:00:00 2001 From: Eric Willis Date: Wed, 20 Jun 2018 23:17:35 +0000 Subject: [PATCH 1/3] Fixing over-zealous logs --- lib/createIndexIfNotExists.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/createIndexIfNotExists.js b/lib/createIndexIfNotExists.js index 4e37327..edf8368 100644 --- a/lib/createIndexIfNotExists.js +++ b/lib/createIndexIfNotExists.js @@ -6,17 +6,17 @@ 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) { + if (options.log || !client) { + server.log(['search', 'info'], { + message: 'creating index', + opts + }); + } await server.search.createIndex({ index: opts.index, log: options.elasticLog }); } }; From 8865e4659ed13aefc4a126fbdb08348013a83656 Mon Sep 17 00:00:00 2001 From: Eric Willis Date: Thu, 21 Jun 2018 19:27:59 +0000 Subject: [PATCH 2/3] Adding autocreate on server start ... and adding settings --- index.js | 13 +++++++++++++ lib/createIndexIfNotExists.js | 6 +++++- test/test.elasticsearch.js | 32 +++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) 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 edf8368..9983e47 100644 --- a/lib/createIndexIfNotExists.js +++ b/lib/createIndexIfNotExists.js @@ -17,6 +17,10 @@ module.exports = async function(opts) { opts }); } - await server.search.createIndex({ index: opts.index, log: options.elasticLog }); + 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..5cf9010 100644 --- a/test/test.elasticsearch.js +++ b/test/test.elasticsearch.js @@ -5,12 +5,13 @@ const plugin = require('../index.js'); tap.test('can load plugin', async (t) => { const server = new Hapi.Server({ port: 8080 }); 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 }); + 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(); From c7650efdd970c93730e8eb8c0ca3f4c6a6af99e4 Mon Sep 17 00:00:00 2001 From: Eric Willis Date: Thu, 21 Jun 2018 19:31:33 +0000 Subject: [PATCH 3/3] logging --- test/test.elasticsearch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.elasticsearch.js b/test/test.elasticsearch.js index 5cf9010..52bff40 100644 --- a/test/test.elasticsearch.js +++ b/test/test.elasticsearch.js @@ -3,7 +3,7 @@ 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({ @@ -27,7 +27,7 @@ tap.test('can load plugin', async (t) => { }); tap.test('autocreate index with settings', 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({