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
6 changes: 5 additions & 1 deletion lib/createIndexIfNotExists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
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