UNPKG

es

Version:

API around the ElasticSearch RESTful API -- mostly convenience.

584 lines (457 loc) 22.1 kB
"use strict"; var _typeof = require("@babel/runtime-corejs3/helpers/typeof"); var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map"); var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"); var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass")); var utils = _interopRequireWildcard(require("./utils")); var _reqlib = require("reqlib"); function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = _Object$defineProperty && _Object$getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? _Object$getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { _Object$defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var HTTP_STATUS_NOT_FOUND = 404, HTTP_STATUS_SUCCESS = 200; var Indices = /*#__PURE__*/function () { function Indices(config, request) { var _context; (0, _classCallCheck2["default"])(this, Indices); this.config = config; this.paramExcludes = (0, _concat["default"])(_context = (0, _keys["default"])(config)).call(_context, ['_index', '_indices', 'alias']); this.request = request || new _reqlib.Request(config); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/ (0, _createClass2["default"])(Indices, [{ key: "alias", value: function alias() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var data = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof data === 'function') { callback = data; data = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(options.alias && index ? index : null, options.alias && index ? '_alias' : '_aliases', options.alias && index ? options.alias : null); if (options.alias && index) { return this.request.put(options, data, callback); } return this.request.post(options, data, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/ // Disclaimer: does not currently support pre 0.90 ways of retrieving aliases }, { key: "aliases", value: function aliases() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } if (!options.alias) { options.alias = '*'; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_alias', options.alias); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/ }, { key: "analyze", value: function analyze() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var data = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof data === 'function') { callback = data; data = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_analyze'); // documentation indicates GET method... // sending POST data via GET not typical, using POST instead return this.request.post(options, data, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/ }, { key: "clearCache", value: function clearCache() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_cache/clear'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/ }, { key: "closeIndex", value: function closeIndex() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index, '_close'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index/ }, { key: "createIndex", value: function createIndex() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var data = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof data === 'function') { callback = data; data = options; options = {}; } if (!callback && !data && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index); return this.request.put(options, data, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/ }, { key: "createTemplate", value: function createTemplate() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var template = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof template === 'function') { callback = template; template = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['name']); if (err) { return utils.promiseRejectOrCallback(err, callback); } options.path = utils.pathAppend('_template', options.name); return this.request.put(options, template, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/ }, { key: "deleteAlias", value: function deleteAlias() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index', 'alias']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index, '_alias', options.alias); return this.request["delete"](options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index/ }, { key: "deleteIndex", value: function deleteIndex() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index); return this.request["delete"](options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping/ }, { key: "deleteMapping", value: function deleteMapping() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index); return this.request["delete"](options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/ }, { key: "deleteTemplate", value: function deleteTemplate() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['name']); if (err) { return utils.promiseRejectOrCallback(err, callback); } options.path = utils.pathAppend('_template', options.name); return this.request["delete"](options, callback); } // https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html // https://www.elastic.co/guide/en/elasticsearch/reference/5.5/indices-types-exists.html // Also replicated (somewhat) in core... core.exists is more flexible, however }, { key: "exists", value: function exists() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config), statusCode; options.path = utils.pathAppend(index); this.request.once('response', function (context) { return statusCode = context.state.statusCode; }); return this.request.head(options, function (err, data) { if (err) { if (err.statusCode && err.statusCode === HTTP_STATUS_NOT_FOUND) { data = { exists: false, statusCode: err.statusCode }; return utils.promiseResolveOrCallback(data, callback); } return utils.promiseRejectOrCallback(err, callback); } // must listen to event... data = { exists: statusCode === HTTP_STATUS_SUCCESS }; return utils.promiseResolveOrCallback(data, callback); }); } }, { key: "flush", value: function flush() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_flush'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping/ }, { key: "mappings", value: function mappings() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_mapping'); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/ }, { key: "openIndex", value: function openIndex() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_open'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/ }, { key: "putMapping", value: function putMapping() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var mapping = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof mapping === 'function') { callback = mapping; mapping = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } // prepare mapping if necessary var outer = mapping; if (outer.properties) { outer = { mappings: { _doc: mapping } }; } // prepare URL var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index); return this.request.put(options, outer, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh/ }, { key: "refresh", value: function refresh() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_refresh'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-segments/ }, { key: "segments", value: function segments() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_segments'); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-get-settings/ }, { key: "settings", value: function settings() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['_index']); if (err) { return utils.promiseRejectOrCallback(err, callback); } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_settings'); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot/ }, { key: "snapshot", value: function snapshot() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.path = utils.pathAppend(index, '_gateway/snapshot'); return this.request.post(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-stats/ }, { key: "stats", value: function stats() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_stats'); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-status/ }, { key: "status", value: function status() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_status'); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/ }, { key: "templates", value: function templates() { var _context2; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var callback = arguments.length > 1 ? arguments[1] : undefined; if (!callback && typeof options === 'function') { callback = options; options = {}; } var err = utils.optionsUndefined(options, this.config, ['name']); if (err) { return utils.promiseRejectOrCallback(err, callback); } options.query = utils.exclude(options, (0, _concat["default"])(_context2 = this.paramExcludes).call(_context2, 'name')); options.path = utils.pathAppend('_template', options.name); return this.request.get(options, callback); } // http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/ }, { key: "updateSettings", value: function updateSettings() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var settings = arguments.length > 1 ? arguments[1] : undefined; var callback = arguments.length > 2 ? arguments[2] : undefined; if (!callback && typeof settings === 'function') { callback = settings; settings = options; options = {}; } var index = utils.getIndexSyntax(options, this.config); options.query = utils.exclude(options, this.paramExcludes); options.path = utils.pathAppend(index, '_settings'); return this.request.put(options, settings, callback); } }]); return Indices; }(); module.exports = { Indices: Indices }; //# sourceMappingURL=indices.js.map