Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Dec 8, 2011
1 parent 77bbe77 commit a53420f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 110 deletions.
33 changes: 16 additions & 17 deletions lib/client.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
76 changes: 42 additions & 34 deletions lib/index.js
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions tests/offline-tests.js
Expand Up @@ -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) {
Expand Down
115 changes: 58 additions & 57 deletions tests/online-tests.js
Expand Up @@ -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()`': {
Expand Down Expand Up @@ -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',
Expand All @@ -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()`': {
Expand All @@ -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'],
Expand Down

0 comments on commit a53420f

Please sign in to comment.