UNPKG

@sugarcube/plugin-elasticsearch

Version:

Use [Elasticsearch](https://www.elastic.co/products/elasticsearch) for SugarCube data.

68 lines (61 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fp = require("lodash/fp"); var _fs = _interopRequireDefault(require("fs")); var _elastic = require("../elastic"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const reindexPlugin = (envelope, { log, cfg }) => { const host = (0, _fp.get)("elastic.host", cfg); const port = (0, _fp.get)("elastic.port", cfg); const index = (0, _fp.get)("elastic.index", cfg); const toHost = (0, _fp.getOr)(host, "elastic.to_host", cfg); const toPort = (0, _fp.getOr)(port, "elastic.to_port", cfg); const toIndex = (0, _fp.get)("elastic.to_index", cfg); const mappings = (0, _fp.get)("elastic.mappings", cfg) ? JSON.parse(_fs.default.readFileSync((0, _fp.get)("elastic.mappings", cfg))) : {}; log.info(`Reindexing ${index} to http://${toHost}:${toPort}/${toIndex}`); return _elastic.Elastic.Do(function* reindexIndex({ reindex }) { yield reindex(index, host, port, toIndex); }, { host: toHost, port: toPort, mappings }).then(([, history]) => { history.forEach(([k, meta]) => log.debug(`${k}: ${JSON.stringify(meta)}.`)); return envelope; }); }; reindexPlugin.desc = "Reindex an an Elasticsearch index."; reindexPlugin.argv = { "elastic.to_index": { type: "string", nargs: 1, desc: "Specify the new index name." }, "elastic.to_host": { type: "string", default: "localhost", nargs: 1, desc: "The hostname of the target Elasticsearch server." }, "elastic.to_port": { type: "string", default: "9200", nargs: 1, desc: "The port of the target Elasticsearch server." }, "elastic.mappings": { type: "string", nargs: 1, desc: "Load custom index mappings from a JSON file." } }; var _default = reindexPlugin; exports.default = _default;