diff --git a/lib/client.js b/lib/client.js index f044640..d0ad44f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -248,6 +248,22 @@ Client.prototype = { (this._indexCache[name] = new Index(this, name)); }, + /** + Gets mapping definitions for the specified type within the specified index. + + [ElasticSearch docs](http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html) + + @method getMapping + @param {String|String[]} names Index name or array of names. + @param {String} type Document type. If omitted, mappings for all type are returned. + @param {Function} callback Callback function. + @param {Error|null} callback.err Error, or `null` on success. + @param {Object} callback.res ElasticSearch response data. + @static + @see Client.getMapping + **/ + getMapping: wrapStaticIndexMethod('getMapping'), + /** Adds a document to the specified index. @@ -349,23 +365,6 @@ Client.prototype = { **/ putMapping: wrapStaticIndexMethod('putMapping'), - - /** - Retrieve mapping definitions for the specified type within the specified index. - - [ElasticSearch docs](http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html) - - @method getMapping - @param {String|String[]} [names] Index name or array of names to retrieve the mapping of. - @param {String} type Document type. If omitted, mapping for all type is returned. - @param {Function} [callback] Callback function. - @param {Error|null} callback.err Error, or `null` on success. - @param {Object} callback.res ElasticSearch response data. - @static - @see Client.getMapping - **/ - getMapping: wrapStaticIndexMethod('getMapping'), - /** Refreshes the specified index or indices. diff --git a/lib/index.js b/lib/index.js index d957c89..5c5aaaa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -270,35 +270,42 @@ Index.exists = function (client, names, callback) { }; /** -Retrieve mapping definition of index +Retrieve the mappings for one or more indices. [ElasticSearch docs](http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping.html) @method getMapping @param {Client} client Client instance. -@param {String|String[]} [names] Index name or array of names to retrieve the mapping of. -@param {String} type Document type. If omitted, mapping for all type is returned. -@param {Function} [callback] Callback function. +@param {String|String[]} names Index name or array of names. +@param {String} [type] Document type. If omitted, mappings for all types are returned. +@param {Function} callback Callback function. @param {Error|null} callback.err Error, or `null` on success. @param {Object} callback.res ElasticSearch response data. @static @see Client.getMapping **/ Index.getMapping = function (client, names, type, callback) { - if (Array.isArray(names)) { - names = names.join(','); - } - names = names.trim(); - if(!callback) { - callback=type; - type=null; - } - if(!callback) { - throw new Error('Invalid parameters, must give at least a callback'); - } - client._request('/' + encode(names ).trim() + (type ? ('/' + encode(type.trim())) : '') + '/_mapping', { - method: 'GET' - }, callback); + var url; + + if (typeof type === 'function') { + callback = type; + type = null; + } + + if (Array.isArray(names)) { + names = names.join(','); + } + + names = names.trim(); + url = '/' + encode(names); + + if (type) { + url += '/' + encode(type); + } + + url += '/_mapping'; + + client._request(url, callback); } /** @@ -577,6 +584,23 @@ Index.prototype = { }); }, + /** + Gets the mapping definition for this index. + + [ElasticSearch docs](http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping.html) + + @method getMapping + @param {String} [type] Document type. If omitted, mappings for all types are returned. + @param {Function} callback Callback function. + @param {Error|null} callback.err Error, or `null` on success. + @param {Object} callback.res ElasticSearch response data. + @see Client.getMapping + **/ + getMapping: function (type, callback) { + Index.getMapping.apply(null, [this.client, this.name].concat( + Array.prototype.slice.call(arguments))); + }, + /** Adds a document to this index. @@ -693,22 +717,6 @@ Index.prototype = { }, - /** - Retrieve mapping definition of index. - - [ElasticSearch docs](http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping.html) - - @method getMapping - @param {String} type Document type. If omitted, mapping for all types is returned. - @param {Function} [callback] Callback function. - @param {Error|null} callback.err Error, or `null` on success. - @param {Object} callback.res ElasticSearch response data. - @see Client.getMapping - **/ - getMapping: function (type, callback) { - Index.getMapping(this.client, this.name, type,callback); - }, - /** Registers a mapping definition for the specified type within this index. diff --git a/tests/offline-tests.js b/tests/offline-tests.js index 9531f41..e8314f5 100644 --- a/tests/offline-tests.js +++ b/tests/offline-tests.js @@ -432,8 +432,7 @@ vows.describe('Elastical').addBatch({ }, 'URL should have the correct path': function (err, options) { - assert.equal(parseUrl(options.url).pathname, '/_all/tweet/_ls - ng'); + assert.equal(parseUrl(options.url).pathname, '/_all/tweet/_mapping'); }, 'mapping definition should be passed in the request body': function (err, options) { diff --git a/tests/online-tests.js b/tests/online-tests.js index c48757e..a1b5e16 100644 --- a/tests/online-tests.js +++ b/tests/online-tests.js @@ -429,74 +429,75 @@ vows.describe('Elastical') } } }, - + '`getMapping()`': { - 'of a specific type within a specific index': { - topic: function (client) { + 'of a specific type within a specific index': { + topic: function (client) { client.getMapping('elastical-test-mapping', 'type', this.callback); - }, - 'should succeed': function (err, res) { - assert.isNull(err); - assert.isObject(res); - assert.isObject(res.type); - assert.isObject(res.type.properties.tags); - assert.isObject(res.type.properties.body); - assert.isObject(res.type.properties.title); - assert.equal(res.type.properties.body.type, 'string'); - assert.equal(res.type.properties.tags.type, 'string'); - } - }, - 'of all types within a specific index': { - topic: function (client) { + }, + 'should succeed': function (err, res) { + assert.isNull(err); + assert.isObject(res); + assert.isObject(res.type); + assert.isObject(res.type.properties.tags); + assert.isObject(res.type.properties.body); + assert.isObject(res.type.properties.title); + assert.equal(res.type.properties.body.type, 'string'); + assert.equal(res.type.properties.tags.type, 'string'); + } + }, + + 'of all types within a specific index': { + topic: function (client) { client.getMapping('elastical-test-mapping', this.callback); - }, - 'should succeed': function (err, res) { - assert.isNull(err); - assert.isObject(res); - assert.isObject(res['elastical-test-mapping'].type); - assert.isObject(res['elastical-test-mapping'].type.properties.tags); - assert.isObject(res['elastical-test-mapping'].type.properties.body); - assert.isObject(res['elastical-test-mapping'].type.properties.title); - assert.equal(res['elastical-test-mapping'].type.properties.body.type, 'string'); - assert.equal(res['elastical-test-mapping'].type.properties.tags.type, 'string'); - assert.isObject(res['elastical-test-mapping'].type2); // tweet has been set by putMapping tests - assert.isObject(res['elastical-test-mapping'].type2.properties.other); - assert.equal(res['elastical-test-mapping'].type2.properties.other.type, 'long'); - } - }, - 'within multiple indices': { - topic: function (client) { + }, + 'should succeed': function (err, res) { + assert.isNull(err); + assert.isObject(res); + assert.isObject(res['elastical-test-mapping'].type); + assert.isObject(res['elastical-test-mapping'].type.properties.tags); + assert.isObject(res['elastical-test-mapping'].type.properties.body); + assert.isObject(res['elastical-test-mapping'].type.properties.title); + assert.equal(res['elastical-test-mapping'].type.properties.body.type, 'string'); + assert.equal(res['elastical-test-mapping'].type.properties.tags.type, 'string'); + assert.isObject(res['elastical-test-mapping'].type2); // tweet has been set by putMapping tests + assert.isObject(res['elastical-test-mapping'].type2.properties.other); + assert.equal(res['elastical-test-mapping'].type2.properties.other.type, 'long'); + } + }, + 'within multiple indices': { + topic: function (client) { client.getMapping(['elastical-test-mapping', 'elastical-test-mapping2'], this.callback); - }, - 'should succeed': function (err, res) { - assert.isNull(err); - assert.isObject(res); - assert.isObject(res['elastical-test-mapping'].type); - assert.isObject(res['elastical-test-mapping2'].type); - } - }, - 'of an unexisting index': { - topic: function (client) { + }, + 'should succeed': function (err, res) { + assert.isNull(err); + assert.isObject(res); + assert.isObject(res['elastical-test-mapping'].type); + assert.isObject(res['elastical-test-mapping2'].type); + } + }, + 'of an unexisting index': { + topic: function (client) { client.getMapping('elastical-test-mapping-unexisting', 'type', this.callback); - }, - 'should return an IndexMissingException': function (err, res) { + }, + 'should return an IndexMissingException': function (err, res) { assert.instanceOf(err, Error); assert.equal(err.message, 'IndexMissingException[[elastical-test-mapping-unexisting] missing]'); assert.equal(res.status, 404); assert.equal(res.error, 'IndexMissingException[[elastical-test-mapping-unexisting] missing]'); - } - }, - 'of an unexisting type': { - topic: function (client) { + } + }, + 'of an unexisting type': { + topic: function (client) { client.getMapping('elastical-test-mapping', 'type-unexisting', this.callback); - }, - 'should return an TypeMissingException': function (err, res) { + }, + 'should return an TypeMissingException': function (err, res) { assert.instanceOf(err, Error); assert.equal(err.message, 'TypeMissingException[[elastical-test-mapping] type[type-unexisting] missing]'); assert.equal(res.status, 404); assert.equal(res.error, 'TypeMissingException[[elastical-test-mapping] type[type-unexisting] missing]'); - } - } + } + } }, '`refresh()`': { @@ -620,7 +621,7 @@ vows.describe('Elastical') assert.equal(res._type, "elastical-test-percolator-index"); assert.equal(res._id, "elastical-test-percolator-set"); } - }, + }, '`getPercolator()`': { topic: function (client) { client.getPercolator('elastical-test-percolator-index', @@ -644,7 +645,7 @@ vows.describe('Elastical') assert.equal('_percolator', res._index); assert.equal('elastical-test-percolator-index', res._type); assert.equal('elastical-test-percolator-get', res._id); - assert.equal(true, res.exists); + assert.equal(true, res.exists); } }, '`percolate()`': { @@ -670,7 +671,7 @@ vows.describe('Elastical') }, 'should return a match and the name of the percolator even if doc is absent': { topic: function(client){ - client.percolate('elastical-test-percolator-index', 'post', { + client.percolate('elastical-test-percolator-index', 'post', { title : "Welcome to my stupid blog", content: "This is the first and last time I'll post anything.", tags : ['welcome', 'first post', 'last post'],