testing and clients are 'online' and working know to get the tests running againts an actual ES node

This commit is contained in:
Spencer Alger
2013-09-27 12:46:45 -07:00
parent 0ac81e1989
commit e21fb21ee3
228 changed files with 51083 additions and 921 deletions

113
src/api/bulk.js Normal file
View File

@ -0,0 +1,113 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var replicationOptions = ['sync', 'async'];
/**
* Perform an elasticsearch [bulk](http://elasticsearch.org/guide/reference/api/bulk/) request
*
* @for Client
* @method bulk
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.consistency - Explicit write consistency setting for the operation
* @param {boolean} params.refresh - Refresh the index after performing the operation
* @param {String} [params.replication=sync] - Explicitely set the replication type
* @param {string} params.type - Default document type for items which don't provide one
*/
function doBulk(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'POST' || params.method === 'PUT') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, PUT');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_bulk';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_bulk';
}
else {
request.url = '/_bulk';
}
// build the query string
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
query.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doBulk;

View File

@ -0,0 +1,35 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.getSettings](http://elasticsearch.org/guide/reference/api/admin-cluster-update-settings/) request
*
* @for Client
* @method cluster.getSettings
* @param {Object} params - An object with parameters used to carry out this action
*/
function doClusterGetSettings(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
// build the url
request.url = '/_cluster/settings';
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterGetSettings;

133
src/api/cluster/health.js Normal file
View File

@ -0,0 +1,133 @@
var _ = require('../../lib/utils');
var levelOptions = ['cluster', 'indices', 'shards'];
var waitForStatusOptions = ['green', 'yellow', 'red'];
/**
* Perform an elasticsearch [cluster.health](http://elasticsearch.org/guide/reference/api/admin-cluster-health/) request
*
* @for Client
* @method cluster.health
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.level=cluster] - Specify the level of detail for returned information
* @param {boolean} params.local - Return local information, do not retrieve the state from master node (default: false)
* @param {Date|Number} params.master_timeout - Explicit operation timeout for connection to master node
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {number} params.wait_for_active_shards - Wait until the specified number of shards is active
* @param {number} params.wait_for_nodes - Wait until the specified number of nodes is available
* @param {number} params.wait_for_relocating_shards - Wait until the specified number of relocating shards is finished
* @param {String} params.wait_for_status - Wait until cluster is in a specific state
*/
function doClusterHealth(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/_cluster/health/' + url.index + '';
}
else {
request.url = '/_cluster/health';
}
// build the query string
if (typeof params.level !== 'undefined') {
if (_.contains(levelOptions, params.level)) {
query.level = params.level;
} else {
throw new TypeError(
'Invalid level: ' + params.level +
' should be one of ' + levelOptions.join(', ') + '.'
);
}
}
if (typeof params.local !== 'undefined') {
if (params.local.toLowerCase && (params.local = params.local.toLowerCase())
&& (params.local === 'no' || params.local === 'off')
) {
query.local = false;
} else {
query.local = !!params.local;
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.wait_for_active_shards !== 'undefined') {
if (_.isNumeric(params.wait_for_active_shards)) {
query.wait_for_active_shards = params.wait_for_active_shards * 1;
} else {
throw new TypeError('Invalid wait_for_active_shards: ' + params.wait_for_active_shards + ' should be a number.');
}
}
if (typeof params.wait_for_nodes !== 'undefined') {
if (_.isNumeric(params.wait_for_nodes)) {
query.wait_for_nodes = params.wait_for_nodes * 1;
} else {
throw new TypeError('Invalid wait_for_nodes: ' + params.wait_for_nodes + ' should be a number.');
}
}
if (typeof params.wait_for_relocating_shards !== 'undefined') {
if (_.isNumeric(params.wait_for_relocating_shards)) {
query.wait_for_relocating_shards = params.wait_for_relocating_shards * 1;
} else {
throw new TypeError('Invalid wait_for_relocating_shards: ' + params.wait_for_relocating_shards + ' should be a number.');
}
}
if (typeof params.wait_for_status !== 'undefined') {
if (_.contains(waitForStatusOptions, params.wait_for_status)) {
query.wait_for_status = params.wait_for_status;
} else {
throw new TypeError(
'Invalid wait_for_status: ' + params.wait_for_status +
' should be one of ' + waitForStatusOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterHealth;

View File

@ -0,0 +1,91 @@
var _ = require('../../lib/utils');
var typeOptions = ['cpu', 'wait', 'block'];
/**
* Perform an elasticsearch [cluster.nodeHotThreads](http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-hot-threads/) request
*
* @for Client
* @method cluster.nodeHotThreads
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.interval - The interval for the second sampling of threads
* @param {number} params.snapshots - Number of samples of thread stacktrace (default: 10)
* @param {number} params.threads - Specify the number of threads to provide information for (default: 3)
* @param {String} params.type - The type to sample (default: cpu)
*/
function doClusterNodeHotThreads(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.node_id !== 'undefined') {
if (typeof params.node_id === 'string') {
url.node_id = params.node_id;
} else if (_.isArray(params.node_id)) {
url.node_id = params.node_id.join(',');
} else {
throw new TypeError('Invalid node_id: ' + params.node_id + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('node_id')) {
request.url = '/_nodes/' + url.node_id + '/hotthreads';
}
else {
request.url = '/_nodes/hotthreads';
}
// build the query string
if (typeof params.interval !== 'undefined') {
if (params.interval instanceof Date) {
query.interval = params.interval.getTime();
} else if (_.isNumeric(params.interval)) {
query.interval = params.interval;
} else {
throw new TypeError('Invalid interval: ' + params.interval + ' should be be some sort of time.');
}
}
if (typeof params.snapshots !== 'undefined') {
if (_.isNumeric(params.snapshots)) {
query.snapshots = params.snapshots * 1;
} else {
throw new TypeError('Invalid snapshots: ' + params.snapshots + ' should be a number.');
}
}
if (typeof params.threads !== 'undefined') {
if (_.isNumeric(params.threads)) {
query.threads = params.threads * 1;
} else {
throw new TypeError('Invalid threads: ' + params.threads + ' should be a number.');
}
}
if (typeof params.type !== 'undefined') {
if (_.contains(typeOptions, params.type)) {
query.type = params.type;
} else {
throw new TypeError(
'Invalid type: ' + params.type +
' should be one of ' + typeOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterNodeHotThreads;

180
src/api/cluster/nodeInfo.js Normal file
View File

@ -0,0 +1,180 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.nodeInfo](http://elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/) request
*
* @for Client
* @method cluster.nodeInfo
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.all - Return all available information
* @param {boolean} params.clear - Reset the default settings
* @param {boolean} params.http - Return information about HTTP
* @param {boolean} params.jvm - Return information about the JVM
* @param {boolean} params.network - Return information about network
* @param {boolean} params.os - Return information about the operating system
* @param {boolean} params.plugin - Return information about plugins
* @param {boolean} params.process - Return information about the Elasticsearch process
* @param {boolean} params.settings - Return information about node settings
* @param {boolean} params.thread_pool - Return information about the thread pool
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {boolean} params.transport - Return information about transport
*/
function doClusterNodeInfo(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.node_id !== 'undefined') {
if (typeof params.node_id === 'string') {
url.node_id = params.node_id;
} else if (_.isArray(params.node_id)) {
url.node_id = params.node_id.join(',');
} else {
throw new TypeError('Invalid node_id: ' + params.node_id + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('node_id')) {
request.url = '/_nodes/' + url.node_id + '';
}
else {
request.url = '/_nodes';
}
// build the query string
if (typeof params.all !== 'undefined') {
if (params.all.toLowerCase && (params.all = params.all.toLowerCase())
&& (params.all === 'no' || params.all === 'off')
) {
query.all = false;
} else {
query.all = !!params.all;
}
}
if (typeof params.clear !== 'undefined') {
if (params.clear.toLowerCase && (params.clear = params.clear.toLowerCase())
&& (params.clear === 'no' || params.clear === 'off')
) {
query.clear = false;
} else {
query.clear = !!params.clear;
}
}
if (typeof params.http !== 'undefined') {
if (params.http.toLowerCase && (params.http = params.http.toLowerCase())
&& (params.http === 'no' || params.http === 'off')
) {
query.http = false;
} else {
query.http = !!params.http;
}
}
if (typeof params.jvm !== 'undefined') {
if (params.jvm.toLowerCase && (params.jvm = params.jvm.toLowerCase())
&& (params.jvm === 'no' || params.jvm === 'off')
) {
query.jvm = false;
} else {
query.jvm = !!params.jvm;
}
}
if (typeof params.network !== 'undefined') {
if (params.network.toLowerCase && (params.network = params.network.toLowerCase())
&& (params.network === 'no' || params.network === 'off')
) {
query.network = false;
} else {
query.network = !!params.network;
}
}
if (typeof params.os !== 'undefined') {
if (params.os.toLowerCase && (params.os = params.os.toLowerCase())
&& (params.os === 'no' || params.os === 'off')
) {
query.os = false;
} else {
query.os = !!params.os;
}
}
if (typeof params.plugin !== 'undefined') {
if (params.plugin.toLowerCase && (params.plugin = params.plugin.toLowerCase())
&& (params.plugin === 'no' || params.plugin === 'off')
) {
query.plugin = false;
} else {
query.plugin = !!params.plugin;
}
}
if (typeof params.process !== 'undefined') {
if (params.process.toLowerCase && (params.process = params.process.toLowerCase())
&& (params.process === 'no' || params.process === 'off')
) {
query.process = false;
} else {
query.process = !!params.process;
}
}
if (typeof params.settings !== 'undefined') {
if (params.settings.toLowerCase && (params.settings = params.settings.toLowerCase())
&& (params.settings === 'no' || params.settings === 'off')
) {
query.settings = false;
} else {
query.settings = !!params.settings;
}
}
if (typeof params.thread_pool !== 'undefined') {
if (params.thread_pool.toLowerCase && (params.thread_pool = params.thread_pool.toLowerCase())
&& (params.thread_pool === 'no' || params.thread_pool === 'off')
) {
query.thread_pool = false;
} else {
query.thread_pool = !!params.thread_pool;
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.transport !== 'undefined') {
if (params.transport.toLowerCase && (params.transport = params.transport.toLowerCase())
&& (params.transport === 'no' || params.transport === 'off')
) {
query.transport = false;
} else {
query.transport = !!params.transport;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterNodeInfo;

View File

@ -0,0 +1,70 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.nodeShutdown](http://elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown/) request
*
* @for Client
* @method cluster.nodeShutdown
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.delay - Set the delay for the operation (default: 1s)
* @param {boolean} params.exit - Exit the JVM as well (default: true)
*/
function doClusterNodeShutdown(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'POST';
// find the url's params
if (typeof params.node_id !== 'undefined') {
if (typeof params.node_id === 'string') {
url.node_id = params.node_id;
} else if (_.isArray(params.node_id)) {
url.node_id = params.node_id.join(',');
} else {
throw new TypeError('Invalid node_id: ' + params.node_id + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('node_id')) {
request.url = '/_cluster/nodes/' + url.node_id + '/_shutdown';
}
else {
request.url = '/_shutdown';
}
// build the query string
if (typeof params.delay !== 'undefined') {
if (params.delay instanceof Date) {
query.delay = params.delay.getTime();
} else if (_.isNumeric(params.delay)) {
query.delay = params.delay;
} else {
throw new TypeError('Invalid delay: ' + params.delay + ' should be be some sort of time.');
}
}
if (typeof params.exit !== 'undefined') {
if (params.exit.toLowerCase && (params.exit = params.exit.toLowerCase())
&& (params.exit === 'no' || params.exit === 'off')
) {
query.exit = false;
} else {
query.exit = !!params.exit;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterNodeShutdown;

View File

@ -0,0 +1,215 @@
var _ = require('../../lib/utils');
var metricFamilyOptions = ['all', 'fs', 'http', 'indices', 'jvm', 'network', 'os', 'process', 'thread_pool', 'transport'];
var metricOptions = ['docs', 'fielddata', 'filter_cache', 'flush', 'get', 'id_cache', 'indexing', 'merges', 'refresh', 'search', 'store', 'warmer'];
/**
* Perform an elasticsearch [cluster.nodeStats](http://elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats/) request
*
* @for Client
* @method cluster.nodeStats
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.all - Return all available information
* @param {boolean} params.clear - Reset the default level of detail
* @param {list} params.fields - A comma-separated list of fields for `fielddata` metric (supports wildcards)
* @param {boolean} params.fs - Return information about the filesystem
* @param {boolean} params.http - Return information about HTTP
* @param {boolean} params.indices - Return information about indices
* @param {boolean} params.jvm - Return information about the JVM
* @param {boolean} params.network - Return information about network
* @param {boolean} params.os - Return information about the operating system
* @param {boolean} params.process - Return information about the Elasticsearch process
* @param {boolean} params.thread_pool - Return information about the thread pool
* @param {boolean} params.transport - Return information about transport
*/
function doClusterNodeStats(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
url.fields = params.fields;
} else if (_.isArray(params.fields)) {
url.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.metric_family !== 'undefined') {
if (_.contains(metricFamilyOptions, params.metric_family)) {
url.metric_family = params.metric_family;
} else {
throw new TypeError(
'Invalid metric_family: ' + params.metric_family +
' should be one of ' + metricFamilyOptions.join(', ') + '.'
);
}
}
if (typeof params.metric !== 'undefined') {
if (_.contains(metricOptions, params.metric)) {
url.metric = params.metric;
} else {
throw new TypeError(
'Invalid metric: ' + params.metric +
' should be one of ' + metricOptions.join(', ') + '.'
);
}
}
if (typeof params.node_id !== 'undefined') {
if (typeof params.node_id === 'string') {
url.node_id = params.node_id;
} else if (_.isArray(params.node_id)) {
url.node_id = params.node_id.join(',');
} else {
throw new TypeError('Invalid node_id: ' + params.node_id + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('node_id')) {
request.url = '/_nodes/' + url.node_id + '/stats';
}
else {
request.url = '/_nodes/stats';
}
// build the query string
if (typeof params.all !== 'undefined') {
if (params.all.toLowerCase && (params.all = params.all.toLowerCase())
&& (params.all === 'no' || params.all === 'off')
) {
query.all = false;
} else {
query.all = !!params.all;
}
}
if (typeof params.clear !== 'undefined') {
if (params.clear.toLowerCase && (params.clear = params.clear.toLowerCase())
&& (params.clear === 'no' || params.clear === 'off')
) {
query.clear = false;
} else {
query.clear = !!params.clear;
}
}
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.fs !== 'undefined') {
if (params.fs.toLowerCase && (params.fs = params.fs.toLowerCase())
&& (params.fs === 'no' || params.fs === 'off')
) {
query.fs = false;
} else {
query.fs = !!params.fs;
}
}
if (typeof params.http !== 'undefined') {
if (params.http.toLowerCase && (params.http = params.http.toLowerCase())
&& (params.http === 'no' || params.http === 'off')
) {
query.http = false;
} else {
query.http = !!params.http;
}
}
if (typeof params.indices !== 'undefined') {
if (params.indices.toLowerCase && (params.indices = params.indices.toLowerCase())
&& (params.indices === 'no' || params.indices === 'off')
) {
query.indices = false;
} else {
query.indices = !!params.indices;
}
}
if (typeof params.jvm !== 'undefined') {
if (params.jvm.toLowerCase && (params.jvm = params.jvm.toLowerCase())
&& (params.jvm === 'no' || params.jvm === 'off')
) {
query.jvm = false;
} else {
query.jvm = !!params.jvm;
}
}
if (typeof params.network !== 'undefined') {
if (params.network.toLowerCase && (params.network = params.network.toLowerCase())
&& (params.network === 'no' || params.network === 'off')
) {
query.network = false;
} else {
query.network = !!params.network;
}
}
if (typeof params.os !== 'undefined') {
if (params.os.toLowerCase && (params.os = params.os.toLowerCase())
&& (params.os === 'no' || params.os === 'off')
) {
query.os = false;
} else {
query.os = !!params.os;
}
}
if (typeof params.process !== 'undefined') {
if (params.process.toLowerCase && (params.process = params.process.toLowerCase())
&& (params.process === 'no' || params.process === 'off')
) {
query.process = false;
} else {
query.process = !!params.process;
}
}
if (typeof params.thread_pool !== 'undefined') {
if (params.thread_pool.toLowerCase && (params.thread_pool = params.thread_pool.toLowerCase())
&& (params.thread_pool === 'no' || params.thread_pool === 'off')
) {
query.thread_pool = false;
} else {
query.thread_pool = !!params.thread_pool;
}
}
if (typeof params.transport !== 'undefined') {
if (params.transport.toLowerCase && (params.transport = params.transport.toLowerCase())
&& (params.transport === 'no' || params.transport === 'off')
) {
query.transport = false;
} else {
query.transport = !!params.transport;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterNodeStats;

View File

@ -0,0 +1,36 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.putSettings](http://elasticsearch.org/guide/reference/api/admin-cluster-update-settings/) request
*
* @for Client
* @method cluster.putSettings
* @param {Object} params - An object with parameters used to carry out this action
*/
function doClusterPutSettings(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'PUT';
// find the url's params
// build the url
request.url = '/_cluster/settings';
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterPutSettings;

View File

@ -0,0 +1,57 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.reroute](http://elasticsearch.org/guide/reference/api/admin-cluster-reroute/) request
*
* @for Client
* @method cluster.reroute
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.dry_run - Simulate the operation only and return the resulting state
* @param {boolean} params.filter_metadata - Don't return cluster state metadata (default: false)
*/
function doClusterReroute(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'POST';
// find the url's params
// build the url
request.url = '/_cluster/reroute';
// build the query string
if (typeof params.dry_run !== 'undefined') {
if (params.dry_run.toLowerCase && (params.dry_run = params.dry_run.toLowerCase())
&& (params.dry_run === 'no' || params.dry_run === 'off')
) {
query.dry_run = false;
} else {
query.dry_run = !!params.dry_run;
}
}
if (typeof params.filter_metadata !== 'undefined') {
if (params.filter_metadata.toLowerCase && (params.filter_metadata = params.filter_metadata.toLowerCase())
&& (params.filter_metadata === 'no' || params.filter_metadata === 'off')
) {
query.filter_metadata = false;
} else {
query.filter_metadata = !!params.filter_metadata;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterReroute;

122
src/api/cluster/state.js Normal file
View File

@ -0,0 +1,122 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [cluster.state](http://elasticsearch.org/guide/reference/api/admin-cluster-state/) request
*
* @for Client
* @method cluster.state
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.filter_blocks - Do not return information about blocks
* @param {boolean} params.filter_index_templates - Do not return information about index templates
* @param {list} params.filter_indices - Limit returned metadata information to specific indices
* @param {boolean} params.filter_metadata - Do not return information about indices metadata
* @param {boolean} params.filter_nodes - Do not return information about nodes
* @param {boolean} params.filter_routing_table - Do not return information about shard allocation (`routing_table` and `routing_nodes`)
* @param {boolean} params.local - Return local information, do not retrieve the state from master node (default: false)
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doClusterState(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
// build the url
request.url = '/_cluster/state';
// build the query string
if (typeof params.filter_blocks !== 'undefined') {
if (params.filter_blocks.toLowerCase && (params.filter_blocks = params.filter_blocks.toLowerCase())
&& (params.filter_blocks === 'no' || params.filter_blocks === 'off')
) {
query.filter_blocks = false;
} else {
query.filter_blocks = !!params.filter_blocks;
}
}
if (typeof params.filter_index_templates !== 'undefined') {
if (params.filter_index_templates.toLowerCase && (params.filter_index_templates = params.filter_index_templates.toLowerCase())
&& (params.filter_index_templates === 'no' || params.filter_index_templates === 'off')
) {
query.filter_index_templates = false;
} else {
query.filter_index_templates = !!params.filter_index_templates;
}
}
if (typeof params.filter_indices !== 'undefined') {
if (typeof params.filter_indices === 'string') {
query.filter_indices = params.filter_indices;
} else if (_.isArray(params.filter_indices)) {
query.filter_indices = params.filter_indices.join(',');
} else {
throw new TypeError('Invalid filter_indices: ' + params.filter_indices + ' should be a comma seperated list or array.');
}
}
if (typeof params.filter_metadata !== 'undefined') {
if (params.filter_metadata.toLowerCase && (params.filter_metadata = params.filter_metadata.toLowerCase())
&& (params.filter_metadata === 'no' || params.filter_metadata === 'off')
) {
query.filter_metadata = false;
} else {
query.filter_metadata = !!params.filter_metadata;
}
}
if (typeof params.filter_nodes !== 'undefined') {
if (params.filter_nodes.toLowerCase && (params.filter_nodes = params.filter_nodes.toLowerCase())
&& (params.filter_nodes === 'no' || params.filter_nodes === 'off')
) {
query.filter_nodes = false;
} else {
query.filter_nodes = !!params.filter_nodes;
}
}
if (typeof params.filter_routing_table !== 'undefined') {
if (params.filter_routing_table.toLowerCase && (params.filter_routing_table = params.filter_routing_table.toLowerCase())
&& (params.filter_routing_table === 'no' || params.filter_routing_table === 'off')
) {
query.filter_routing_table = false;
} else {
query.filter_routing_table = !!params.filter_routing_table;
}
}
if (typeof params.local !== 'undefined') {
if (params.local.toLowerCase && (params.local = params.local.toLowerCase())
&& (params.local === 'no' || params.local === 'off')
) {
query.local = false;
} else {
query.local = !!params.local;
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doClusterState;

120
src/api/count.js Normal file
View File

@ -0,0 +1,120 @@
var _ = require('../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [count](http://elasticsearch.org/guide/reference/api/count/) request
*
* @for Client
* @method count
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {number} params.min_score - Include only documents with a specific `_score` value in the result
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {string} params.routing - Specific routing value
* @param {string} params.source - The URL-encoded query definition (instead of using the request body)
*/
function doCount(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_count';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_count';
}
else {
request.url = '/_count';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.min_score !== 'undefined') {
if (_.isNumeric(params.min_score)) {
query.min_score = params.min_score * 1;
} else {
throw new TypeError('Invalid min_score: ' + params.min_score + ' should be a number.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doCount;

197
src/api/create.js Normal file
View File

@ -0,0 +1,197 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var replicationOptions = ['sync', 'async'];
var versionTypeOptions = ['internal', 'external'];
/**
* Perform an elasticsearch [create](http://elasticsearch.org/guide/reference/api/index_/) request
*
* @for Client
* @method create
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.consistency - Explicit write consistency setting for the operation
* @param {string} params.id - Specific document ID (when the POST method is used)
* @param {string} params.parent - ID of the parent document
* @param {string} params.percolate - Percolator queries to execute while indexing the document
* @param {boolean} params.refresh - Refresh the index after performing the operation
* @param {String} [params.replication=sync] - Specific replication type
* @param {string} params.routing - Specific routing value
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.timestamp - Explicit timestamp for the document
* @param {duration} params.ttl - Expiration time for the document
* @param {number} params.version - Explicit version number for concurrency control
* @param {String} params.version_type - Specific version type
*/
function doCreate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'POST' || params.method === 'PUT') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, PUT');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.id !== 'undefined') {
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '/_create';
}
else if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.id !== 'undefined') {
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
query.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.percolate !== 'undefined') {
if (typeof params.percolate !== 'object' && typeof params.percolate !== 'undefined') {
query.percolate = '' + params.percolate;
} else {
throw new TypeError('Invalid percolate: ' + params.percolate + ' should be a string.');
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.timestamp !== 'undefined') {
if (params.timestamp instanceof Date) {
query.timestamp = params.timestamp.getTime();
} else if (_.isNumeric(params.timestamp)) {
query.timestamp = params.timestamp;
} else {
throw new TypeError('Invalid timestamp: ' + params.timestamp + ' should be be some sort of time.');
}
}
if (typeof params.ttl !== 'undefined') {
if (_.isInterval(params.ttl)) {
query.ttl = params.ttl;
} else {
throw new TypeError('Invalid ttl: ' + params.ttl + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}
}
if (typeof params.version !== 'undefined') {
if (_.isNumeric(params.version)) {
query.version = params.version * 1;
} else {
throw new TypeError('Invalid version: ' + params.version + ' should be a number.');
}
}
if (typeof params.version_type !== 'undefined') {
if (_.contains(versionTypeOptions, params.version_type)) {
query.version_type = params.version_type;
} else {
throw new TypeError(
'Invalid version_type: ' + params.version_type +
' should be one of ' + versionTypeOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doCreate;

145
src/api/delete.js Normal file
View File

@ -0,0 +1,145 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var replicationOptions = ['sync', 'async'];
var versionTypeOptions = ['internal', 'external'];
/**
* Perform an elasticsearch [delete](http://elasticsearch.org/guide/reference/api/delete/) request
*
* @for Client
* @method delete
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.consistency - Specific write consistency setting for the operation
* @param {string} params.parent - ID of parent document
* @param {boolean} params.refresh - Refresh the index after performing the operation
* @param {String} [params.replication=sync] - Specific replication type
* @param {string} params.routing - Specific routing value
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {number} params.version - Explicit version number for concurrency control
* @param {String} params.version_type - Specific version type
*/
function doDelete(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.version !== 'undefined') {
if (_.isNumeric(params.version)) {
query.version = params.version * 1;
} else {
throw new TypeError('Invalid version: ' + params.version + ' should be a number.');
}
}
if (typeof params.version_type !== 'undefined') {
if (_.contains(versionTypeOptions, params.version_type)) {
query.version_type = params.version_type;
} else {
throw new TypeError(
'Invalid version_type: ' + params.version_type +
' should be one of ' + versionTypeOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doDelete;

169
src/api/deleteByQuery.js Normal file
View File

@ -0,0 +1,169 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var defaultOperatorOptions = ['AND', 'OR'];
var ignoreIndicesOptions = ['none', 'missing'];
var replicationOptions = ['sync', 'async'];
/**
* Perform an elasticsearch [deleteByQuery](http://www.elasticsearch.org/guide/reference/api/delete-by-query/) request
*
* @for Client
* @method deleteByQuery
* @param {Object} params - An object with parameters used to carry out this action
* @param {string} params.analyzer - The analyzer to use for the query string
* @param {String} params.consistency - Specific write consistency setting for the operation
* @param {String} [params.default_operator=OR] - The default operator for query string query (AND or OR)
* @param {string} params.df - The field to use as default where no field prefix is given in the query string
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {String} [params.replication=sync] - Specific replication type
* @param {string} params.q - Query in the Lucene query string syntax
* @param {string} params.routing - Specific routing value
* @param {string} params.source - The URL-encoded query definition (instead of using the request body)
* @param {Date|Number} params.timeout - Explicit operation timeout
*/
function doDeleteByQuery(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'DELETE';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_query';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_query';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
if (typeof params.analyzer !== 'undefined') {
if (typeof params.analyzer !== 'object' && typeof params.analyzer !== 'undefined') {
query.analyzer = '' + params.analyzer;
} else {
throw new TypeError('Invalid analyzer: ' + params.analyzer + ' should be a string.');
}
}
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.default_operator !== 'undefined') {
if (_.contains(defaultOperatorOptions, params.default_operator)) {
query.default_operator = params.default_operator;
} else {
throw new TypeError(
'Invalid default_operator: ' + params.default_operator +
' should be one of ' + defaultOperatorOptions.join(', ') + '.'
);
}
}
if (typeof params.df !== 'undefined') {
if (typeof params.df !== 'object' && typeof params.df !== 'undefined') {
query.df = '' + params.df;
} else {
throw new TypeError('Invalid df: ' + params.df + ' should be a string.');
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.q !== 'undefined') {
if (typeof params.q !== 'object' && typeof params.q !== 'undefined') {
query.q = '' + params.q;
} else {
throw new TypeError('Invalid q: ' + params.q + ' should be a string.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doDeleteByQuery;

107
src/api/exists.js Normal file
View File

@ -0,0 +1,107 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [exists](http://elasticsearch.org/guide/reference/api/get/) request
*
* @for Client
* @method exists
* @param {Object} params - An object with parameters used to carry out this action
* @param {string} params.parent - The ID of the parent document
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {boolean} params.realtime - Specify whether to perform the operation in realtime or search mode
* @param {boolean} params.refresh - Refresh the shard containing the document before performing the operation
* @param {string} params.routing - Specific routing value
*/
function doExists(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'HEAD';
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.realtime !== 'undefined') {
if (params.realtime.toLowerCase && (params.realtime = params.realtime.toLowerCase())
&& (params.realtime === 'no' || params.realtime === 'off')
) {
query.realtime = false;
} else {
query.realtime = !!params.realtime;
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doExists;

186
src/api/explain.js Normal file
View File

@ -0,0 +1,186 @@
var _ = require('../lib/utils');
var defaultOperatorOptions = ['AND', 'OR'];
/**
* Perform an elasticsearch [explain](http://elasticsearch.org/guide/reference/api/explain/) request
*
* @for Client
* @method explain
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.analyze_wildcard - Specify whether wildcards and prefix queries in the query string query should be analyzed (default: false)
* @param {string} params.analyzer - The analyzer for the query string query
* @param {String} [params.default_operator=OR] - The default operator for query string query (AND or OR)
* @param {string} params.df - The default field for query string query (default: _all)
* @param {list} params.fields - A comma-separated list of fields to return in the response
* @param {boolean} params.lenient - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
* @param {boolean} params.lowercase_expanded_terms - Specify whether query terms should be lowercased
* @param {string} params.parent - The ID of the parent document
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {string} params.q - Query in the Lucene query string syntax
* @param {string} params.routing - Specific routing value
* @param {string} params.source - The URL-encoded query definition (instead of using the request body)
*/
function doExplain(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '/_explain';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.analyze_wildcard !== 'undefined') {
if (params.analyze_wildcard.toLowerCase && (params.analyze_wildcard = params.analyze_wildcard.toLowerCase())
&& (params.analyze_wildcard === 'no' || params.analyze_wildcard === 'off')
) {
query.analyze_wildcard = false;
} else {
query.analyze_wildcard = !!params.analyze_wildcard;
}
}
if (typeof params.analyzer !== 'undefined') {
if (typeof params.analyzer !== 'object' && typeof params.analyzer !== 'undefined') {
query.analyzer = '' + params.analyzer;
} else {
throw new TypeError('Invalid analyzer: ' + params.analyzer + ' should be a string.');
}
}
if (typeof params.default_operator !== 'undefined') {
if (_.contains(defaultOperatorOptions, params.default_operator)) {
query.default_operator = params.default_operator;
} else {
throw new TypeError(
'Invalid default_operator: ' + params.default_operator +
' should be one of ' + defaultOperatorOptions.join(', ') + '.'
);
}
}
if (typeof params.df !== 'undefined') {
if (typeof params.df !== 'object' && typeof params.df !== 'undefined') {
query.df = '' + params.df;
} else {
throw new TypeError('Invalid df: ' + params.df + ' should be a string.');
}
}
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.lenient !== 'undefined') {
if (params.lenient.toLowerCase && (params.lenient = params.lenient.toLowerCase())
&& (params.lenient === 'no' || params.lenient === 'off')
) {
query.lenient = false;
} else {
query.lenient = !!params.lenient;
}
}
if (typeof params.lowercase_expanded_terms !== 'undefined') {
if (params.lowercase_expanded_terms.toLowerCase && (params.lowercase_expanded_terms = params.lowercase_expanded_terms.toLowerCase())
&& (params.lowercase_expanded_terms === 'no' || params.lowercase_expanded_terms === 'off')
) {
query.lowercase_expanded_terms = false;
} else {
query.lowercase_expanded_terms = !!params.lowercase_expanded_terms;
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.q !== 'undefined') {
if (typeof params.q !== 'object' && typeof params.q !== 'undefined') {
query.q = '' + params.q;
} else {
throw new TypeError('Invalid q: ' + params.q + ' should be a string.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doExplain;

118
src/api/get.js Normal file
View File

@ -0,0 +1,118 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [get](http://elasticsearch.org/guide/reference/api/get/) request
*
* @for Client
* @method get
* @param {Object} params - An object with parameters used to carry out this action
* @param {list} params.fields - A comma-separated list of fields to return in the response
* @param {string} params.parent - The ID of the parent document
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {boolean} params.realtime - Specify whether to perform the operation in realtime or search mode
* @param {boolean} params.refresh - Refresh the shard containing the document before performing the operation
* @param {string} params.routing - Specific routing value
*/
function doGet(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.realtime !== 'undefined') {
if (params.realtime.toLowerCase && (params.realtime = params.realtime.toLowerCase())
&& (params.realtime === 'no' || params.realtime === 'off')
) {
query.realtime = false;
} else {
query.realtime = !!params.realtime;
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doGet;

107
src/api/getSource.js Normal file
View File

@ -0,0 +1,107 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [getSource](http://elasticsearch.org/guide/reference/api/get/) request
*
* @for Client
* @method getSource
* @param {Object} params - An object with parameters used to carry out this action
* @param {string} params.parent - The ID of the parent document
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {boolean} params.realtime - Specify whether to perform the operation in realtime or search mode
* @param {boolean} params.refresh - Refresh the shard containing the document before performing the operation
* @param {string} params.routing - Specific routing value
*/
function doGetSource(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '/_source';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.realtime !== 'undefined') {
if (params.realtime.toLowerCase && (params.realtime = params.realtime.toLowerCase())
&& (params.realtime === 'no' || params.realtime === 'off')
) {
query.realtime = false;
} else {
query.realtime = !!params.realtime;
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doGetSource;

201
src/api/index.js Normal file
View File

@ -0,0 +1,201 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var opTypeOptions = ['index', 'create'];
var replicationOptions = ['sync', 'async'];
var versionTypeOptions = ['internal', 'external'];
/**
* Perform an elasticsearch [index](http://elasticsearch.org/guide/reference/api/index_/) request
*
* @for Client
* @method index
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.consistency - Explicit write consistency setting for the operation
* @param {String} [params.op_type=index] - Explicit operation type
* @param {string} params.parent - ID of the parent document
* @param {string} params.percolate - Percolator queries to execute while indexing the document
* @param {boolean} params.refresh - Refresh the index after performing the operation
* @param {String} [params.replication=sync] - Specific replication type
* @param {string} params.routing - Specific routing value
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.timestamp - Explicit timestamp for the document
* @param {duration} params.ttl - Expiration time for the document
* @param {number} params.version - Explicit version number for concurrency control
* @param {String} params.version_type - Specific version type
*/
function doIndex(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'POST' || params.method === 'PUT') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, PUT');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.id !== 'undefined') {
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '';
}
else if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.op_type !== 'undefined') {
if (_.contains(opTypeOptions, params.op_type)) {
query.op_type = params.op_type;
} else {
throw new TypeError(
'Invalid op_type: ' + params.op_type +
' should be one of ' + opTypeOptions.join(', ') + '.'
);
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.percolate !== 'undefined') {
if (typeof params.percolate !== 'object' && typeof params.percolate !== 'undefined') {
query.percolate = '' + params.percolate;
} else {
throw new TypeError('Invalid percolate: ' + params.percolate + ' should be a string.');
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.timestamp !== 'undefined') {
if (params.timestamp instanceof Date) {
query.timestamp = params.timestamp.getTime();
} else if (_.isNumeric(params.timestamp)) {
query.timestamp = params.timestamp;
} else {
throw new TypeError('Invalid timestamp: ' + params.timestamp + ' should be be some sort of time.');
}
}
if (typeof params.ttl !== 'undefined') {
if (_.isInterval(params.ttl)) {
query.ttl = params.ttl;
} else {
throw new TypeError('Invalid ttl: ' + params.ttl + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}
}
if (typeof params.version !== 'undefined') {
if (_.isNumeric(params.version)) {
query.version = params.version * 1;
} else {
throw new TypeError('Invalid version: ' + params.version + ' should be a number.');
}
}
if (typeof params.version_type !== 'undefined') {
if (_.contains(versionTypeOptions, params.version_type)) {
query.version_type = params.version_type;
} else {
throw new TypeError(
'Invalid version_type: ' + params.version_type +
' should be one of ' + versionTypeOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndex;

136
src/api/indices/analyze.js Normal file
View File

@ -0,0 +1,136 @@
var _ = require('../../lib/utils');
var formatOptions = ['detailed', 'text'];
/**
* Perform an elasticsearch [indices.analyze](http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/) request
*
* @for Client
* @method indices.analyze
* @param {Object} params - An object with parameters used to carry out this action
* @param {string} params.analyzer - The name of the analyzer to use
* @param {string} params.field - Use the analyzer configured for this field (instead of passing the analyzer name)
* @param {list} params.filters - A comma-separated list of filters to use for the analysis
* @param {string} params.index - The name of the index to scope the operation
* @param {boolean} params.prefer_local - With `true`, specify that a local shard should be used if available, with `false`, use a random shard (default: true)
* @param {string} params.text - The text on which the analysis should be performed (when request body is not used)
* @param {string} params.tokenizer - The name of the tokenizer to use for the analysis
* @param {String} [params.format=detailed] - Format of the output
*/
function doIndicesAnalyze(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_analyze';
}
else {
request.url = '/_analyze';
}
// build the query string
if (typeof params.analyzer !== 'undefined') {
if (typeof params.analyzer !== 'object' && typeof params.analyzer !== 'undefined') {
query.analyzer = '' + params.analyzer;
} else {
throw new TypeError('Invalid analyzer: ' + params.analyzer + ' should be a string.');
}
}
if (typeof params.field !== 'undefined') {
if (typeof params.field !== 'object' && typeof params.field !== 'undefined') {
query.field = '' + params.field;
} else {
throw new TypeError('Invalid field: ' + params.field + ' should be a string.');
}
}
if (typeof params.filters !== 'undefined') {
if (typeof params.filters === 'string') {
query.filters = params.filters;
} else if (_.isArray(params.filters)) {
query.filters = params.filters.join(',');
} else {
throw new TypeError('Invalid filters: ' + params.filters + ' should be a comma seperated list or array.');
}
}
if (typeof params.index !== 'undefined') {
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
query.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
}
if (typeof params.prefer_local !== 'undefined') {
if (params.prefer_local.toLowerCase && (params.prefer_local = params.prefer_local.toLowerCase())
&& (params.prefer_local === 'no' || params.prefer_local === 'off')
) {
query.prefer_local = false;
} else {
query.prefer_local = !!params.prefer_local;
}
}
if (typeof params.text !== 'undefined') {
if (typeof params.text !== 'object' && typeof params.text !== 'undefined') {
query.text = '' + params.text;
} else {
throw new TypeError('Invalid text: ' + params.text + ' should be a string.');
}
}
if (typeof params.tokenizer !== 'undefined') {
if (typeof params.tokenizer !== 'object' && typeof params.tokenizer !== 'undefined') {
query.tokenizer = '' + params.tokenizer;
} else {
throw new TypeError('Invalid tokenizer: ' + params.tokenizer + ' should be a string.');
}
}
if (typeof params.format !== 'undefined') {
if (_.contains(formatOptions, params.format)) {
query.format = params.format;
} else {
throw new TypeError(
'Invalid format: ' + params.format +
' should be one of ' + formatOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesAnalyze;

View File

@ -0,0 +1,180 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.clearCache](http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/) request
*
* @for Client
* @method indices.clearCache
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.field_data - Clear field data
* @param {boolean} params.fielddata - Clear field data
* @param {list} params.fields - A comma-separated list of fields to clear when using the `field_data` parameter (default: all)
* @param {boolean} params.filter - Clear filter caches
* @param {boolean} params.filter_cache - Clear filter caches
* @param {boolean} params.filter_keys - A comma-separated list of keys to clear when using the `filter_cache` parameter (default: all)
* @param {boolean} params.id - Clear ID caches for parent/child
* @param {boolean} params.id_cache - Clear ID caches for parent/child
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {list} params.index - A comma-separated list of index name to limit the operation
* @param {boolean} params.recycler - Clear the recycler cache
*/
function doIndicesClearCache(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_cache/clear';
}
else {
request.url = '/_cache/clear';
}
// build the query string
if (typeof params.field_data !== 'undefined') {
if (params.field_data.toLowerCase && (params.field_data = params.field_data.toLowerCase())
&& (params.field_data === 'no' || params.field_data === 'off')
) {
query.field_data = false;
} else {
query.field_data = !!params.field_data;
}
}
if (typeof params.fielddata !== 'undefined') {
if (params.fielddata.toLowerCase && (params.fielddata = params.fielddata.toLowerCase())
&& (params.fielddata === 'no' || params.fielddata === 'off')
) {
query.fielddata = false;
} else {
query.fielddata = !!params.fielddata;
}
}
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.filter !== 'undefined') {
if (params.filter.toLowerCase && (params.filter = params.filter.toLowerCase())
&& (params.filter === 'no' || params.filter === 'off')
) {
query.filter = false;
} else {
query.filter = !!params.filter;
}
}
if (typeof params.filter_cache !== 'undefined') {
if (params.filter_cache.toLowerCase && (params.filter_cache = params.filter_cache.toLowerCase())
&& (params.filter_cache === 'no' || params.filter_cache === 'off')
) {
query.filter_cache = false;
} else {
query.filter_cache = !!params.filter_cache;
}
}
if (typeof params.filter_keys !== 'undefined') {
if (params.filter_keys.toLowerCase && (params.filter_keys = params.filter_keys.toLowerCase())
&& (params.filter_keys === 'no' || params.filter_keys === 'off')
) {
query.filter_keys = false;
} else {
query.filter_keys = !!params.filter_keys;
}
}
if (typeof params.id !== 'undefined') {
if (params.id.toLowerCase && (params.id = params.id.toLowerCase())
&& (params.id === 'no' || params.id === 'off')
) {
query.id = false;
} else {
query.id = !!params.id;
}
}
if (typeof params.id_cache !== 'undefined') {
if (params.id_cache.toLowerCase && (params.id_cache = params.id_cache.toLowerCase())
&& (params.id_cache === 'no' || params.id_cache === 'off')
) {
query.id_cache = false;
} else {
query.id_cache = !!params.id_cache;
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
query.index = params.index;
} else if (_.isArray(params.index)) {
query.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.recycler !== 'undefined') {
if (params.recycler.toLowerCase && (params.recycler = params.recycler.toLowerCase())
&& (params.recycler === 'no' || params.recycler === 'off')
) {
query.recycler = false;
} else {
query.recycler = !!params.recycler;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesClearCache;

66
src/api/indices/close.js Normal file
View File

@ -0,0 +1,66 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.close](http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/) request
*
* @for Client
* @method indices.close
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesClose(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'POST';
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_close';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesClose;

75
src/api/indices/create.js Normal file
View File

@ -0,0 +1,75 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.create](http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index/) request
*
* @for Client
* @method indices.create
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesCreate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'PUT' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of PUT, POST');
}
} else {
request.method = 'PUT';
}
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesCreate;

70
src/api/indices/delete.js Normal file
View File

@ -0,0 +1,70 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.delete](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index/) request
*
* @for Client
* @method indices.delete
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesDelete(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '';
}
else {
request.url = '/';
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesDelete;

View File

@ -0,0 +1,72 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.deleteAlias](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.deleteAlias
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit timestamp for the document
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesDeleteAlias(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_alias/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, name');
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesDeleteAlias;

View File

@ -0,0 +1,63 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.deleteMapping](http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping/) request
*
* @for Client
* @method indices.deleteMapping
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesDeleteMapping(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesDeleteMapping;

View File

@ -0,0 +1,66 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.deleteTemplate](http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/) request
*
* @for Client
* @method indices.deleteTemplate
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesDeleteTemplate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('name')) {
request.url = '/_template/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least name');
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesDeleteTemplate;

View File

@ -0,0 +1,81 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.deleteWarmer](http://www.elasticsearch.org/guide/reference/api/admin-indices-warmers/) request
*
* @for Client
* @method indices.deleteWarmer
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesDeleteWarmer(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'DELETE';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.name !== 'undefined') {
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/' + url.type + '/_warmer/' + url.name + '';
}
else if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_warmer/' + url.name + '';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_warmer';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesDeleteWarmer;

47
src/api/indices/exists.js Normal file
View File

@ -0,0 +1,47 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.exists](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists/) request
*
* @for Client
* @method indices.exists
* @param {Object} params - An object with parameters used to carry out this action
*/
function doIndicesExists(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'HEAD';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesExists;

View File

@ -0,0 +1,73 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.existsAlias](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.existsAlias
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
*/
function doIndicesExistsAlias(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'HEAD';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.name === 'string') {
url.name = params.name;
} else if (_.isArray(params.name)) {
url.name = params.name.join(',');
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a comma seperated list or array.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_alias/' + url.name + '';
}
else if (url.hasOwnProperty('name')) {
request.url = '/_alias/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least name');
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesExistsAlias;

View File

@ -0,0 +1,68 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.existsType](http://www.elasticsearch.org/guide/reference/api/admin-indices-types-exists/) request
*
* @for Client
* @method indices.existsType
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
*/
function doIndicesExistsType(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'HEAD';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesExistsType;

103
src/api/indices/flush.js Normal file
View File

@ -0,0 +1,103 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.flush](http://www.elasticsearch.org/guide/reference/api/admin-indices-flush/) request
*
* @for Client
* @method indices.flush
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.force - TODO: ?
* @param {boolean} params.full - TODO: ?
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {boolean} params.refresh - Refresh the index after performing the operation
*/
function doIndicesFlush(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_flush';
}
else {
request.url = '/_flush';
}
// build the query string
if (typeof params.force !== 'undefined') {
if (params.force.toLowerCase && (params.force = params.force.toLowerCase())
&& (params.force === 'no' || params.force === 'off')
) {
query.force = false;
} else {
query.force = !!params.force;
}
}
if (typeof params.full !== 'undefined') {
if (params.full.toLowerCase && (params.full = params.full.toLowerCase())
&& (params.full === 'no' || params.full === 'off')
) {
query.full = false;
} else {
query.full = !!params.full;
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesFlush;

View File

@ -0,0 +1,73 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.getAlias](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.getAlias
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
*/
function doIndicesGetAlias(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.name === 'string') {
url.name = params.name;
} else if (_.isArray(params.name)) {
url.name = params.name.join(',');
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a comma seperated list or array.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_alias/' + url.name + '';
}
else if (url.hasOwnProperty('name')) {
request.url = '/_alias/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least name');
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetAlias;

View File

@ -0,0 +1,59 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.getAliases](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.getAliases
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
*/
function doIndicesGetAliases(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_aliases';
}
else {
request.url = '/_aliases';
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetAliases;

View File

@ -0,0 +1,62 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.getMapping](http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping/) request
*
* @for Client
* @method indices.getMapping
* @param {Object} params - An object with parameters used to carry out this action
*/
function doIndicesGetMapping(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_mapping';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_mapping';
}
else {
request.url = '/_mapping';
}
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetMapping;

View File

@ -0,0 +1,49 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.getSettings](http://www.elasticsearch.org/guide/reference/api/admin-indices-get-settings/) request
*
* @for Client
* @method indices.getSettings
* @param {Object} params - An object with parameters used to carry out this action
*/
function doIndicesGetSettings(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_settings';
}
else {
request.url = '/_settings';
}
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetSettings;

View File

@ -0,0 +1,45 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.getTemplate](http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/) request
*
* @for Client
* @method indices.getTemplate
* @param {Object} params - An object with parameters used to carry out this action
*/
function doIndicesGetTemplate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('name')) {
request.url = '/_template/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least name');
}
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetTemplate;

View File

@ -0,0 +1,71 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.getWarmer](http://www.elasticsearch.org/guide/reference/api/admin-indices-warmers/) request
*
* @for Client
* @method indices.getWarmer
* @param {Object} params - An object with parameters used to carry out this action
*/
function doIndicesGetWarmer(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.name !== 'undefined') {
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/' + url.type + '/_warmer/' + url.name + '';
}
else if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_warmer/' + url.name + '';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_warmer';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesGetWarmer;

66
src/api/indices/open.js Normal file
View File

@ -0,0 +1,66 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.open](http://www.elasticsearch.org/guide/reference/api/admin-indices-open-close/) request
*
* @for Client
* @method indices.open
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesOpen(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'POST';
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_open';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index');
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesOpen;

128
src/api/indices/optimize.js Normal file
View File

@ -0,0 +1,128 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.optimize](http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize/) request
*
* @for Client
* @method indices.optimize
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.flush - Specify whether the index should be flushed after performing the operation (default: true)
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {number} params.max_num_segments - The number of segments the index should be merged into (default: dynamic)
* @param {boolean} params.only_expunge_deletes - Specify whether the operation should only expunge deleted documents
* @param {*} params.operation_threading - TODO: ?
* @param {boolean} params.refresh - Specify whether the index should be refreshed after performing the operation (default: true)
* @param {boolean} params.wait_for_merge - Specify whether the request should block until the merge process is finished (default: true)
*/
function doIndicesOptimize(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_optimize';
}
else {
request.url = '/_optimize';
}
// build the query string
if (typeof params.flush !== 'undefined') {
if (params.flush.toLowerCase && (params.flush = params.flush.toLowerCase())
&& (params.flush === 'no' || params.flush === 'off')
) {
query.flush = false;
} else {
query.flush = !!params.flush;
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.max_num_segments !== 'undefined') {
if (_.isNumeric(params.max_num_segments)) {
query.max_num_segments = params.max_num_segments * 1;
} else {
throw new TypeError('Invalid max_num_segments: ' + params.max_num_segments + ' should be a number.');
}
}
if (typeof params.only_expunge_deletes !== 'undefined') {
if (params.only_expunge_deletes.toLowerCase && (params.only_expunge_deletes = params.only_expunge_deletes.toLowerCase())
&& (params.only_expunge_deletes === 'no' || params.only_expunge_deletes === 'off')
) {
query.only_expunge_deletes = false;
} else {
query.only_expunge_deletes = !!params.only_expunge_deletes;
}
}
if (typeof params.operation_threading !== 'undefined') {
query.operation_threading = params.operation_threading;
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.wait_for_merge !== 'undefined') {
if (params.wait_for_merge.toLowerCase && (params.wait_for_merge = params.wait_for_merge.toLowerCase())
&& (params.wait_for_merge === 'no' || params.wait_for_merge === 'off')
) {
query.wait_for_merge = false;
} else {
query.wait_for_merge = !!params.wait_for_merge;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesOptimize;

View File

@ -0,0 +1,79 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.putAlias](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.putAlias
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Explicit timestamp for the document
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesPutAlias(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'PUT';
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_alias/' + url.name + '';
}
else if (url.hasOwnProperty('name')) {
request.url = '/_alias/' + url.name + '';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_alias';
}
else {
request.url = '/_alias';
}
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesPutAlias;

View File

@ -0,0 +1,94 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.putMapping](http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping/) request
*
* @for Client
* @method indices.putMapping
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.ignore_conflicts - Specify whether to ignore conflicts while updating the mapping (default: false)
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesPutMapping(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'PUT' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of PUT, POST');
}
} else {
request.method = 'PUT';
}
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_mapping';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.ignore_conflicts !== 'undefined') {
if (params.ignore_conflicts.toLowerCase && (params.ignore_conflicts = params.ignore_conflicts.toLowerCase())
&& (params.ignore_conflicts === 'no' || params.ignore_conflicts === 'off')
) {
query.ignore_conflicts = false;
} else {
query.ignore_conflicts = !!params.ignore_conflicts;
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesPutMapping;

View File

@ -0,0 +1,60 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.putSettings](http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings/) request
*
* @for Client
* @method indices.putSettings
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesPutSettings(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'PUT';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_settings';
}
else {
request.url = '/_settings';
}
// build the query string
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesPutSettings;

View File

@ -0,0 +1,84 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.putTemplate](http://www.elasticsearch.org/guide/reference/api/admin-indices-templates/) request
*
* @for Client
* @method indices.putTemplate
* @param {Object} params - An object with parameters used to carry out this action
* @param {number} params.order - The order for this template when merging multiple matching ones (higher numbers are merged later, overriding the lower numbers)
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesPutTemplate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'PUT' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of PUT, POST');
}
} else {
request.method = 'PUT';
}
// find the url's params
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('name')) {
request.url = '/_template/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least name');
}
// build the query string
if (typeof params.order !== 'undefined') {
if (_.isNumeric(params.order)) {
query.order = params.order * 1;
} else {
throw new TypeError('Invalid order: ' + params.order + ' should be a number.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesPutTemplate;

View File

@ -0,0 +1,77 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.putWarmer](http://www.elasticsearch.org/guide/reference/api/admin-indices-warmers/) request
*
* @for Client
* @method indices.putWarmer
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesPutWarmer(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'PUT';
// find the url's params
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
if (typeof params.name !== 'object' && typeof params.name !== 'undefined') {
url.name = '' + params.name;
} else {
throw new TypeError('Invalid name: ' + params.name + ' should be a string.');
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/' + url.type + '/_warmer/' + url.name + '';
}
else if (url.hasOwnProperty('index') && url.hasOwnProperty('name')) {
request.url = '/' + url.index + '/_warmer/' + url.name + '';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, name');
}
// build the query string
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesPutWarmer;

View File

@ -0,0 +1,75 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.refresh](http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh/) request
*
* @for Client
* @method indices.refresh
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {*} params.operation_threading - TODO: ?
*/
function doIndicesRefresh(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_refresh';
}
else {
request.url = '/_refresh';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.operation_threading !== 'undefined') {
query.operation_threading = params.operation_threading;
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesRefresh;

View File

@ -0,0 +1,67 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.segments](http://elasticsearch.org/guide/reference/api/admin-indices-segments/) request
*
* @for Client
* @method indices.segments
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {*} params.operation_threading - TODO: ?
*/
function doIndicesSegments(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_segments';
}
else {
request.url = '/_segments';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.operation_threading !== 'undefined') {
query.operation_threading = params.operation_threading;
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesSegments;

View File

@ -0,0 +1,62 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.snapshotIndex](http://www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot/) request
*
* @for Client
* @method indices.snapshotIndex
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
*/
function doIndicesSnapshotIndex(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'POST';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_gateway/snapshot';
}
else {
request.url = '/_gateway/snapshot';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesSnapshotIndex;

280
src/api/indices/stats.js Normal file
View File

@ -0,0 +1,280 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
var metricFamilyOptions = ['docs', 'fielddata', 'filter_cache', 'flush', 'get', 'groups', 'id_cache', 'ignore_indices', 'indexing', 'merge', 'refresh', 'search', 'store', 'warmer'];
/**
* Perform an elasticsearch [indices.stats](http://elasticsearch.org/guide/reference/api/admin-indices-stats/) request
*
* @for Client
* @method indices.stats
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.all - Return all available information
* @param {boolean} params.clear - Reset the default level of detail
* @param {boolean} params.docs - Return information about indexed and deleted documents
* @param {boolean} params.fielddata - Return information about field data
* @param {boolean} params.fields - A comma-separated list of fields for `fielddata` metric (supports wildcards)
* @param {boolean} params.filter_cache - Return information about filter cache
* @param {boolean} params.flush - Return information about flush operations
* @param {boolean} params.get - Return information about get operations
* @param {boolean} params.groups - A comma-separated list of search groups for `search` statistics
* @param {boolean} params.id_cache - Return information about ID cache
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {boolean} params.indexing - Return information about indexing operations
* @param {boolean} params.merge - Return information about merge operations
* @param {boolean} params.refresh - Return information about refresh operations
* @param {boolean} params.search - Return information about search operations; use the `groups` parameter to include information for specific search groups
* @param {boolean} params.store - Return information about the size of the index
* @param {boolean} params.warmer - Return information about warmers
*/
function doIndicesStats(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
url.fields = params.fields;
} else if (_.isArray(params.fields)) {
url.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.indexing_types !== 'undefined') {
if (typeof params.indexing_types === 'string') {
url.indexing_types = params.indexing_types;
} else if (_.isArray(params.indexing_types)) {
url.indexing_types = params.indexing_types.join(',');
} else {
throw new TypeError('Invalid indexing_types: ' + params.indexing_types + ' should be a comma seperated list or array.');
}
}
if (typeof params.metric_family !== 'undefined') {
if (_.contains(metricFamilyOptions, params.metric_family)) {
url.metric_family = params.metric_family;
} else {
throw new TypeError(
'Invalid metric_family: ' + params.metric_family +
' should be one of ' + metricFamilyOptions.join(', ') + '.'
);
}
}
if (typeof params.search_groups !== 'undefined') {
if (typeof params.search_groups === 'string') {
url.search_groups = params.search_groups;
} else if (_.isArray(params.search_groups)) {
url.search_groups = params.search_groups.join(',');
} else {
throw new TypeError('Invalid search_groups: ' + params.search_groups + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_stats';
}
else {
request.url = '/_stats';
}
// build the query string
if (typeof params.all !== 'undefined') {
if (params.all.toLowerCase && (params.all = params.all.toLowerCase())
&& (params.all === 'no' || params.all === 'off')
) {
query.all = false;
} else {
query.all = !!params.all;
}
}
if (typeof params.clear !== 'undefined') {
if (params.clear.toLowerCase && (params.clear = params.clear.toLowerCase())
&& (params.clear === 'no' || params.clear === 'off')
) {
query.clear = false;
} else {
query.clear = !!params.clear;
}
}
if (typeof params.docs !== 'undefined') {
if (params.docs.toLowerCase && (params.docs = params.docs.toLowerCase())
&& (params.docs === 'no' || params.docs === 'off')
) {
query.docs = false;
} else {
query.docs = !!params.docs;
}
}
if (typeof params.fielddata !== 'undefined') {
if (params.fielddata.toLowerCase && (params.fielddata = params.fielddata.toLowerCase())
&& (params.fielddata === 'no' || params.fielddata === 'off')
) {
query.fielddata = false;
} else {
query.fielddata = !!params.fielddata;
}
}
if (typeof params.fields !== 'undefined') {
if (params.fields.toLowerCase && (params.fields = params.fields.toLowerCase())
&& (params.fields === 'no' || params.fields === 'off')
) {
query.fields = false;
} else {
query.fields = !!params.fields;
}
}
if (typeof params.filter_cache !== 'undefined') {
if (params.filter_cache.toLowerCase && (params.filter_cache = params.filter_cache.toLowerCase())
&& (params.filter_cache === 'no' || params.filter_cache === 'off')
) {
query.filter_cache = false;
} else {
query.filter_cache = !!params.filter_cache;
}
}
if (typeof params.flush !== 'undefined') {
if (params.flush.toLowerCase && (params.flush = params.flush.toLowerCase())
&& (params.flush === 'no' || params.flush === 'off')
) {
query.flush = false;
} else {
query.flush = !!params.flush;
}
}
if (typeof params.get !== 'undefined') {
if (params.get.toLowerCase && (params.get = params.get.toLowerCase())
&& (params.get === 'no' || params.get === 'off')
) {
query.get = false;
} else {
query.get = !!params.get;
}
}
if (typeof params.groups !== 'undefined') {
if (params.groups.toLowerCase && (params.groups = params.groups.toLowerCase())
&& (params.groups === 'no' || params.groups === 'off')
) {
query.groups = false;
} else {
query.groups = !!params.groups;
}
}
if (typeof params.id_cache !== 'undefined') {
if (params.id_cache.toLowerCase && (params.id_cache = params.id_cache.toLowerCase())
&& (params.id_cache === 'no' || params.id_cache === 'off')
) {
query.id_cache = false;
} else {
query.id_cache = !!params.id_cache;
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.indexing !== 'undefined') {
if (params.indexing.toLowerCase && (params.indexing = params.indexing.toLowerCase())
&& (params.indexing === 'no' || params.indexing === 'off')
) {
query.indexing = false;
} else {
query.indexing = !!params.indexing;
}
}
if (typeof params.merge !== 'undefined') {
if (params.merge.toLowerCase && (params.merge = params.merge.toLowerCase())
&& (params.merge === 'no' || params.merge === 'off')
) {
query.merge = false;
} else {
query.merge = !!params.merge;
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.search !== 'undefined') {
if (params.search.toLowerCase && (params.search = params.search.toLowerCase())
&& (params.search === 'no' || params.search === 'off')
) {
query.search = false;
} else {
query.search = !!params.search;
}
}
if (typeof params.store !== 'undefined') {
if (params.store.toLowerCase && (params.store = params.store.toLowerCase())
&& (params.store === 'no' || params.store === 'off')
) {
query.store = false;
} else {
query.store = !!params.store;
}
}
if (typeof params.warmer !== 'undefined') {
if (params.warmer.toLowerCase && (params.warmer = params.warmer.toLowerCase())
&& (params.warmer === 'no' || params.warmer === 'off')
) {
query.warmer = false;
} else {
query.warmer = !!params.warmer;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesStats;

89
src/api/indices/status.js Normal file
View File

@ -0,0 +1,89 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.status](http://elasticsearch.org/guide/reference/api/admin-indices-status/) request
*
* @for Client
* @method indices.status
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {*} params.operation_threading - TODO: ?
* @param {boolean} params.recovery - Return information about shard recovery
* @param {boolean} params.snapshot - TODO: ?
*/
function doIndicesStatus(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.method = 'GET';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_status';
}
else {
request.url = '/_status';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.operation_threading !== 'undefined') {
query.operation_threading = params.operation_threading;
}
if (typeof params.recovery !== 'undefined') {
if (params.recovery.toLowerCase && (params.recovery = params.recovery.toLowerCase())
&& (params.recovery === 'no' || params.recovery === 'off')
) {
query.recovery = false;
} else {
query.recovery = !!params.recovery;
}
}
if (typeof params.snapshot !== 'undefined') {
if (params.snapshot.toLowerCase && (params.snapshot = params.snapshot.toLowerCase())
&& (params.snapshot === 'no' || params.snapshot === 'off')
) {
query.snapshot = false;
} else {
query.snapshot = !!params.snapshot;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesStatus;

View File

@ -0,0 +1,66 @@
var _ = require('../../lib/utils');
/**
* Perform an elasticsearch [indices.updateAliases](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases/) request
*
* @for Client
* @method indices.updateAliases
* @param {Object} params - An object with parameters used to carry out this action
* @param {Date|Number} params.timeout - Request timeout
* @param {Date|Number} params.master_timeout - Specify timeout for connection to master
*/
function doIndicesUpdateAliases(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'POST';
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
request.url = '/_aliases';
// build the query string
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.master_timeout !== 'undefined') {
if (params.master_timeout instanceof Date) {
query.master_timeout = params.master_timeout.getTime();
} else if (_.isNumeric(params.master_timeout)) {
query.master_timeout = params.master_timeout;
} else {
throw new TypeError('Invalid master_timeout: ' + params.master_timeout + ' should be be some sort of time.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesUpdateAliases;

View File

@ -0,0 +1,118 @@
var _ = require('../../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [indices.validateQuery](http://www.elasticsearch.org/guide/reference/api/validate/) request
*
* @for Client
* @method indices.validateQuery
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.explain - Return detailed information about the error
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {*} params.operation_threading - TODO: ?
* @param {string} params.source - The URL-encoded query definition (instead of using the request body)
* @param {string} params.q - Query in the Lucene query string syntax
*/
function doIndicesValidateQuery(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_validate/query';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_validate/query';
}
else {
request.url = '/_validate/query';
}
// build the query string
if (typeof params.explain !== 'undefined') {
if (params.explain.toLowerCase && (params.explain = params.explain.toLowerCase())
&& (params.explain === 'no' || params.explain === 'off')
) {
query.explain = false;
} else {
query.explain = !!params.explain;
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.operation_threading !== 'undefined') {
query.operation_threading = params.operation_threading;
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
if (typeof params.q !== 'undefined') {
if (typeof params.q !== 'object' && typeof params.q !== 'undefined') {
query.q = '' + params.q;
} else {
throw new TypeError('Invalid q: ' + params.q + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doIndicesValidateQuery;

43
src/api/info.js Normal file
View File

@ -0,0 +1,43 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [info](http://elasticsearch.org/guide/) request
*
* @for Client
* @method info
* @param {Object} params - An object with parameters used to carry out this action
*/
function doInfo(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
if (params.method) {
if (params.method === 'GET' || params.method === 'HEAD') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, HEAD');
}
} else {
request.method = 'GET';
}
// find the url's params
// build the url
request.url = '/';
// build the query string
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doInfo;

108
src/api/mget.js Normal file
View File

@ -0,0 +1,108 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [mget](http://elasticsearch.org/guide/reference/api/multi-get/) request
*
* @for Client
* @method mget
* @param {Object} params - An object with parameters used to carry out this action
* @param {list} params.fields - A comma-separated list of fields to return in the response
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {boolean} params.realtime - Specify whether to perform the operation in realtime or search mode
* @param {boolean} params.refresh - Refresh the shard containing the document before performing the operation
*/
function doMget(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_mget';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_mget';
}
else {
request.url = '/_mget';
}
// build the query string
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.realtime !== 'undefined') {
if (params.realtime.toLowerCase && (params.realtime = params.realtime.toLowerCase())
&& (params.realtime === 'no' || params.realtime === 'off')
) {
query.realtime = false;
} else {
query.realtime = !!params.realtime;
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doMget;

244
src/api/mlt.js Normal file
View File

@ -0,0 +1,244 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [mlt](http://elasticsearch.org/guide/reference/api/more-like-this/) request
*
* @for Client
* @method mlt
* @param {Object} params - An object with parameters used to carry out this action
* @param {number} params.boost_terms - The boost factor
* @param {number} params.max_doc_freq - The word occurrence frequency as count: words with higher occurrence in the corpus will be ignored
* @param {number} params.max_query_terms - The maximum query terms to be included in the generated query
* @param {number} params.max_word_len - The minimum length of the word: longer words will be ignored
* @param {number} params.min_doc_freq - The word occurrence frequency as count: words with lower occurrence in the corpus will be ignored
* @param {number} params.min_term_freq - The term frequency as percent: terms with lower occurence in the source document will be ignored
* @param {number} params.min_word_len - The minimum length of the word: shorter words will be ignored
* @param {list} params.mlt_fields - Specific fields to perform the query against
* @param {number} params.percent_terms_to_match - How many terms have to match in order to consider the document a match (default: 0.3)
* @param {string} params.routing - Specific routing value
* @param {number} params.search_from - The offset from which to return results
* @param {list} params.search_indices - A comma-separated list of indices to perform the query against (default: the index containing the document)
* @param {string} params.search_query_hint - The search query hint
* @param {string} params.search_scroll - A scroll search request definition
* @param {number} params.search_size - The number of documents to return (default: 10)
* @param {string} params.search_source - A specific search request definition (instead of using the request body)
* @param {string} params.search_type - Specific search type (eg. `dfs_then_fetch`, `count`, etc)
* @param {list} params.search_types - A comma-separated list of types to perform the query against (default: the same type as the document)
* @param {list} params.stop_words - A list of stop words to be ignored
*/
function doMlt(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '/_mlt';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.boost_terms !== 'undefined') {
if (_.isNumeric(params.boost_terms)) {
query.boost_terms = params.boost_terms * 1;
} else {
throw new TypeError('Invalid boost_terms: ' + params.boost_terms + ' should be a number.');
}
}
if (typeof params.max_doc_freq !== 'undefined') {
if (_.isNumeric(params.max_doc_freq)) {
query.max_doc_freq = params.max_doc_freq * 1;
} else {
throw new TypeError('Invalid max_doc_freq: ' + params.max_doc_freq + ' should be a number.');
}
}
if (typeof params.max_query_terms !== 'undefined') {
if (_.isNumeric(params.max_query_terms)) {
query.max_query_terms = params.max_query_terms * 1;
} else {
throw new TypeError('Invalid max_query_terms: ' + params.max_query_terms + ' should be a number.');
}
}
if (typeof params.max_word_len !== 'undefined') {
if (_.isNumeric(params.max_word_len)) {
query.max_word_len = params.max_word_len * 1;
} else {
throw new TypeError('Invalid max_word_len: ' + params.max_word_len + ' should be a number.');
}
}
if (typeof params.min_doc_freq !== 'undefined') {
if (_.isNumeric(params.min_doc_freq)) {
query.min_doc_freq = params.min_doc_freq * 1;
} else {
throw new TypeError('Invalid min_doc_freq: ' + params.min_doc_freq + ' should be a number.');
}
}
if (typeof params.min_term_freq !== 'undefined') {
if (_.isNumeric(params.min_term_freq)) {
query.min_term_freq = params.min_term_freq * 1;
} else {
throw new TypeError('Invalid min_term_freq: ' + params.min_term_freq + ' should be a number.');
}
}
if (typeof params.min_word_len !== 'undefined') {
if (_.isNumeric(params.min_word_len)) {
query.min_word_len = params.min_word_len * 1;
} else {
throw new TypeError('Invalid min_word_len: ' + params.min_word_len + ' should be a number.');
}
}
if (typeof params.mlt_fields !== 'undefined') {
if (typeof params.mlt_fields === 'string') {
query.mlt_fields = params.mlt_fields;
} else if (_.isArray(params.mlt_fields)) {
query.mlt_fields = params.mlt_fields.join(',');
} else {
throw new TypeError('Invalid mlt_fields: ' + params.mlt_fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.percent_terms_to_match !== 'undefined') {
if (_.isNumeric(params.percent_terms_to_match)) {
query.percent_terms_to_match = params.percent_terms_to_match * 1;
} else {
throw new TypeError('Invalid percent_terms_to_match: ' + params.percent_terms_to_match + ' should be a number.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.search_from !== 'undefined') {
if (_.isNumeric(params.search_from)) {
query.search_from = params.search_from * 1;
} else {
throw new TypeError('Invalid search_from: ' + params.search_from + ' should be a number.');
}
}
if (typeof params.search_indices !== 'undefined') {
if (typeof params.search_indices === 'string') {
query.search_indices = params.search_indices;
} else if (_.isArray(params.search_indices)) {
query.search_indices = params.search_indices.join(',');
} else {
throw new TypeError('Invalid search_indices: ' + params.search_indices + ' should be a comma seperated list or array.');
}
}
if (typeof params.search_query_hint !== 'undefined') {
if (typeof params.search_query_hint !== 'object' && typeof params.search_query_hint !== 'undefined') {
query.search_query_hint = '' + params.search_query_hint;
} else {
throw new TypeError('Invalid search_query_hint: ' + params.search_query_hint + ' should be a string.');
}
}
if (typeof params.search_scroll !== 'undefined') {
if (typeof params.search_scroll !== 'object' && typeof params.search_scroll !== 'undefined') {
query.search_scroll = '' + params.search_scroll;
} else {
throw new TypeError('Invalid search_scroll: ' + params.search_scroll + ' should be a string.');
}
}
if (typeof params.search_size !== 'undefined') {
if (_.isNumeric(params.search_size)) {
query.search_size = params.search_size * 1;
} else {
throw new TypeError('Invalid search_size: ' + params.search_size + ' should be a number.');
}
}
if (typeof params.search_source !== 'undefined') {
if (typeof params.search_source !== 'object' && typeof params.search_source !== 'undefined') {
query.search_source = '' + params.search_source;
} else {
throw new TypeError('Invalid search_source: ' + params.search_source + ' should be a string.');
}
}
if (typeof params.search_type !== 'undefined') {
if (typeof params.search_type !== 'object' && typeof params.search_type !== 'undefined') {
query.search_type = '' + params.search_type;
} else {
throw new TypeError('Invalid search_type: ' + params.search_type + ' should be a string.');
}
}
if (typeof params.search_types !== 'undefined') {
if (typeof params.search_types === 'string') {
query.search_types = params.search_types;
} else if (_.isArray(params.search_types)) {
query.search_types = params.search_types.join(',');
} else {
throw new TypeError('Invalid search_types: ' + params.search_types + ' should be a comma seperated list or array.');
}
}
if (typeof params.stop_words !== 'undefined') {
if (typeof params.stop_words === 'string') {
query.stop_words = params.stop_words;
} else if (_.isArray(params.stop_words)) {
query.stop_words = params.stop_words.join(',');
} else {
throw new TypeError('Invalid stop_words: ' + params.stop_words + ' should be a comma seperated list or array.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doMlt;

84
src/api/msearch.js Normal file
View File

@ -0,0 +1,84 @@
var _ = require('../lib/utils');
var searchTypeOptions = ['query_then_fetch', 'query_and_fetch', 'dfs_query_then_fetch', 'dfs_query_and_fetch', 'count', 'scan'];
/**
* Perform an elasticsearch [msearch](http://www.elasticsearch.org/guide/reference/api/multi-search/) request
*
* @for Client
* @method msearch
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.search_type - Search operation type
*/
function doMsearch(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_msearch';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_msearch';
}
else {
request.url = '/_msearch';
}
// build the query string
if (typeof params.search_type !== 'undefined') {
if (_.contains(searchTypeOptions, params.search_type)) {
query.search_type = params.search_type;
} else {
throw new TypeError(
'Invalid search_type: ' + params.search_type +
' should be one of ' + searchTypeOptions.join(', ') + '.'
);
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doMsearch;

70
src/api/percolate.js Normal file
View File

@ -0,0 +1,70 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [percolate](http://elasticsearch.org/guide/reference/api/percolate/) request
*
* @for Client
* @method percolate
* @param {Object} params - An object with parameters used to carry out this action
* @param {boolean} params.prefer_local - With `true`, specify that a local shard should be used if available, with `false`, use a random shard (default: true)
*/
function doPercolate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_percolate';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type');
}
// build the query string
if (typeof params.prefer_local !== 'undefined') {
if (params.prefer_local.toLowerCase && (params.prefer_local = params.prefer_local.toLowerCase())
&& (params.prefer_local === 'no' || params.prefer_local === 'off')
) {
query.prefer_local = false;
} else {
query.prefer_local = !!params.prefer_local;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doPercolate;

73
src/api/scroll.js Normal file
View File

@ -0,0 +1,73 @@
var _ = require('../lib/utils');
/**
* Perform an elasticsearch [scroll](http://www.elasticsearch.org/guide/reference/api/search/scroll/) request
*
* @for Client
* @method scroll
* @param {Object} params - An object with parameters used to carry out this action
* @param {duration} params.scroll - Specify how long a consistent view of the index should be maintained for scrolled search
* @param {string} params.scroll_id - The scroll ID for scrolled search
*/
function doScroll(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.scroll_id !== 'undefined') {
if (typeof params.scroll_id !== 'object' && typeof params.scroll_id !== 'undefined') {
url.scroll_id = '' + params.scroll_id;
} else {
throw new TypeError('Invalid scroll_id: ' + params.scroll_id + ' should be a string.');
}
}
// build the url
if (url.hasOwnProperty('scroll_id')) {
request.url = '/_search/scroll/' + url.scroll_id + '';
}
else {
request.url = '/_search/scroll';
}
// build the query string
if (typeof params.scroll !== 'undefined') {
if (_.isInterval(params.scroll)) {
query.scroll = params.scroll;
} else {
throw new TypeError('Invalid scroll: ' + params.scroll + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}
}
if (typeof params.scroll_id !== 'undefined') {
if (typeof params.scroll_id !== 'object' && typeof params.scroll_id !== 'undefined') {
query.scroll_id = '' + params.scroll_id;
} else {
throw new TypeError('Invalid scroll_id: ' + params.scroll_id + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doScroll;

343
src/api/search.js Normal file
View File

@ -0,0 +1,343 @@
var _ = require('../lib/utils');
var defaultOperatorOptions = ['AND', 'OR'];
var ignoreIndicesOptions = ['none', 'missing'];
var searchTypeOptions = ['query_then_fetch', 'query_and_fetch', 'dfs_query_then_fetch', 'dfs_query_and_fetch', 'count', 'scan'];
var suggestModeOptions = ['missing', 'popular', 'always'];
/**
* Perform an elasticsearch [search](http://www.elasticsearch.org/guide/reference/api/search/) request
*
* @for Client
* @method search
* @param {Object} params - An object with parameters used to carry out this action
* @param {string} params.analyzer - The analyzer to use for the query string
* @param {boolean} params.analyze_wildcard - Specify whether wildcard and prefix queries should be analyzed (default: false)
* @param {String} [params.default_operator=OR] - The default operator for query string query (AND or OR)
* @param {string} params.df - The field to use as default where no field prefix is given in the query string
* @param {boolean} params.explain - Specify whether to return detailed information about score computation as part of a hit
* @param {list} params.fields - A comma-separated list of fields to return as part of a hit
* @param {number} params.from - Starting offset (default: 0)
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {list} params.indices_boost - Comma-separated list of index boosts
* @param {boolean} params.lenient - Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
* @param {boolean} params.lowercase_expanded_terms - Specify whether query terms should be lowercased
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {string} params.q - Query in the Lucene query string syntax
* @param {list} params.routing - A comma-separated list of specific routing values
* @param {duration} params.scroll - Specify how long a consistent view of the index should be maintained for scrolled search
* @param {String} params.search_type - Search operation type
* @param {number} params.size - Number of hits to return (default: 10)
* @param {list} params.sort - A comma-separated list of <field>:<direction> pairs
* @param {string} params.source - The URL-encoded request definition using the Query DSL (instead of using request body)
* @param {list} params.stats - Specific 'tag' of the request for logging and statistical purposes
* @param {string} params.suggest_field - Specify which field to use for suggestions
* @param {String} [params.suggest_mode=missing] - Specify suggest mode
* @param {number} params.suggest_size - How many suggestions to return in response
* @param {text} params.suggest_text - The source text for which the suggestions should be returned
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {boolean} params.version - Specify whether to return document version as part of a hit
*/
function doSearch(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'GET' || params.method === 'POST') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of GET, POST');
}
} else {
request.method = 'GET';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
if (typeof params.type !== 'undefined') {
if (typeof params.type === 'string') {
url.type = params.type;
} else if (_.isArray(params.type)) {
url.type = params.type.join(',');
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type')) {
request.url = '/' + url.index + '/' + url.type + '/_search';
}
else if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_search';
}
else {
request.url = '/_search';
}
// build the query string
if (typeof params.analyzer !== 'undefined') {
if (typeof params.analyzer !== 'object' && typeof params.analyzer !== 'undefined') {
query.analyzer = '' + params.analyzer;
} else {
throw new TypeError('Invalid analyzer: ' + params.analyzer + ' should be a string.');
}
}
if (typeof params.analyze_wildcard !== 'undefined') {
if (params.analyze_wildcard.toLowerCase && (params.analyze_wildcard = params.analyze_wildcard.toLowerCase())
&& (params.analyze_wildcard === 'no' || params.analyze_wildcard === 'off')
) {
query.analyze_wildcard = false;
} else {
query.analyze_wildcard = !!params.analyze_wildcard;
}
}
if (typeof params.default_operator !== 'undefined') {
if (_.contains(defaultOperatorOptions, params.default_operator)) {
query.default_operator = params.default_operator;
} else {
throw new TypeError(
'Invalid default_operator: ' + params.default_operator +
' should be one of ' + defaultOperatorOptions.join(', ') + '.'
);
}
}
if (typeof params.df !== 'undefined') {
if (typeof params.df !== 'object' && typeof params.df !== 'undefined') {
query.df = '' + params.df;
} else {
throw new TypeError('Invalid df: ' + params.df + ' should be a string.');
}
}
if (typeof params.explain !== 'undefined') {
if (params.explain.toLowerCase && (params.explain = params.explain.toLowerCase())
&& (params.explain === 'no' || params.explain === 'off')
) {
query.explain = false;
} else {
query.explain = !!params.explain;
}
}
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.from !== 'undefined') {
if (_.isNumeric(params.from)) {
query.from = params.from * 1;
} else {
throw new TypeError('Invalid from: ' + params.from + ' should be a number.');
}
}
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.indices_boost !== 'undefined') {
if (typeof params.indices_boost === 'string') {
query.indices_boost = params.indices_boost;
} else if (_.isArray(params.indices_boost)) {
query.indices_boost = params.indices_boost.join(',');
} else {
throw new TypeError('Invalid indices_boost: ' + params.indices_boost + ' should be a comma seperated list or array.');
}
}
if (typeof params.lenient !== 'undefined') {
if (params.lenient.toLowerCase && (params.lenient = params.lenient.toLowerCase())
&& (params.lenient === 'no' || params.lenient === 'off')
) {
query.lenient = false;
} else {
query.lenient = !!params.lenient;
}
}
if (typeof params.lowercase_expanded_terms !== 'undefined') {
if (params.lowercase_expanded_terms.toLowerCase && (params.lowercase_expanded_terms = params.lowercase_expanded_terms.toLowerCase())
&& (params.lowercase_expanded_terms === 'no' || params.lowercase_expanded_terms === 'off')
) {
query.lowercase_expanded_terms = false;
} else {
query.lowercase_expanded_terms = !!params.lowercase_expanded_terms;
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.q !== 'undefined') {
if (typeof params.q !== 'object' && typeof params.q !== 'undefined') {
query.q = '' + params.q;
} else {
throw new TypeError('Invalid q: ' + params.q + ' should be a string.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing === 'string') {
query.routing = params.routing;
} else if (_.isArray(params.routing)) {
query.routing = params.routing.join(',');
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a comma seperated list or array.');
}
}
if (typeof params.scroll !== 'undefined') {
if (_.isInterval(params.scroll)) {
query.scroll = params.scroll;
} else {
throw new TypeError('Invalid scroll: ' + params.scroll + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}
}
if (typeof params.search_type !== 'undefined') {
if (_.contains(searchTypeOptions, params.search_type)) {
query.search_type = params.search_type;
} else {
throw new TypeError(
'Invalid search_type: ' + params.search_type +
' should be one of ' + searchTypeOptions.join(', ') + '.'
);
}
}
if (typeof params.size !== 'undefined') {
if (_.isNumeric(params.size)) {
query.size = params.size * 1;
} else {
throw new TypeError('Invalid size: ' + params.size + ' should be a number.');
}
}
if (typeof params.sort !== 'undefined') {
if (typeof params.sort === 'string') {
query.sort = params.sort;
} else if (_.isArray(params.sort)) {
query.sort = params.sort.join(',');
} else {
throw new TypeError('Invalid sort: ' + params.sort + ' should be a comma seperated list or array.');
}
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
if (typeof params.stats !== 'undefined') {
if (typeof params.stats === 'string') {
query.stats = params.stats;
} else if (_.isArray(params.stats)) {
query.stats = params.stats.join(',');
} else {
throw new TypeError('Invalid stats: ' + params.stats + ' should be a comma seperated list or array.');
}
}
if (typeof params.suggest_field !== 'undefined') {
if (typeof params.suggest_field !== 'object' && typeof params.suggest_field !== 'undefined') {
query.suggest_field = '' + params.suggest_field;
} else {
throw new TypeError('Invalid suggest_field: ' + params.suggest_field + ' should be a string.');
}
}
if (typeof params.suggest_mode !== 'undefined') {
if (_.contains(suggestModeOptions, params.suggest_mode)) {
query.suggest_mode = params.suggest_mode;
} else {
throw new TypeError(
'Invalid suggest_mode: ' + params.suggest_mode +
' should be one of ' + suggestModeOptions.join(', ') + '.'
);
}
}
if (typeof params.suggest_size !== 'undefined') {
if (_.isNumeric(params.suggest_size)) {
query.suggest_size = params.suggest_size * 1;
} else {
throw new TypeError('Invalid suggest_size: ' + params.suggest_size + ' should be a number.');
}
}
if (typeof params.suggest_text !== 'undefined') {
if (typeof params.suggest_text !== 'object' && typeof params.suggest_text !== 'undefined') {
query.suggest_text = '' + params.suggest_text;
} else {
throw new TypeError('Invalid suggest_text: ' + params.suggest_text + ' should be a string.');
}
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.version !== 'undefined') {
if (params.version.toLowerCase && (params.version = params.version.toLowerCase())
&& (params.version === 'no' || params.version === 'off')
) {
query.version = false;
} else {
query.version = !!params.version;
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doSearch;

98
src/api/suggest.js Normal file
View File

@ -0,0 +1,98 @@
var _ = require('../lib/utils');
var ignoreIndicesOptions = ['none', 'missing'];
/**
* Perform an elasticsearch [suggest](http://elasticsearch.org/guide/reference/api/search/suggest/) request
*
* @for Client
* @method suggest
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} [params.ignore_indices=none] - When performed on multiple indices, allows to ignore `missing` ones
* @param {string} params.preference - Specify the node or shard the operation should be performed on (default: random)
* @param {string} params.routing - Specific routing value
* @param {string} params.source - The URL-encoded request definition (instead of using request body)
*/
function doSuggest(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
if (params.method) {
if (params.method === 'POST' || params.method === 'GET') {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of POST, GET');
}
} else {
request.method = 'POST';
}
// find the url's params
if (typeof params.index !== 'undefined') {
if (typeof params.index === 'string') {
url.index = params.index;
} else if (_.isArray(params.index)) {
url.index = params.index.join(',');
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a comma seperated list or array.');
}
}
// build the url
if (url.hasOwnProperty('index')) {
request.url = '/' + url.index + '/_suggest';
}
else {
request.url = '/_suggest';
}
// build the query string
if (typeof params.ignore_indices !== 'undefined') {
if (_.contains(ignoreIndicesOptions, params.ignore_indices)) {
query.ignore_indices = params.ignore_indices;
} else {
throw new TypeError(
'Invalid ignore_indices: ' + params.ignore_indices +
' should be one of ' + ignoreIndicesOptions.join(', ') + '.'
);
}
}
if (typeof params.preference !== 'undefined') {
if (typeof params.preference !== 'object' && typeof params.preference !== 'undefined') {
query.preference = '' + params.preference;
} else {
throw new TypeError('Invalid preference: ' + params.preference + ' should be a string.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.source !== 'undefined') {
if (typeof params.source !== 'object' && typeof params.source !== 'undefined') {
query.source = '' + params.source;
} else {
throw new TypeError('Invalid source: ' + params.source + ' should be a string.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doSuggest;

205
src/api/update.js Normal file
View File

@ -0,0 +1,205 @@
var _ = require('../lib/utils');
var consistencyOptions = ['one', 'quorum', 'all'];
var replicationOptions = ['sync', 'async'];
/**
* Perform an elasticsearch [update](http://elasticsearch.org/guide/reference/api/update/) request
*
* @for Client
* @method update
* @param {Object} params - An object with parameters used to carry out this action
* @param {String} params.consistency - Explicit write consistency setting for the operation
* @param {list} params.fields - A comma-separated list of fields to return in the response
* @param {string} params.lang - The script language (default: mvel)
* @param {string} params.parent - ID of the parent document
* @param {string} params.percolate - Perform percolation during the operation; use specific registered query name, attribute, or wildcard
* @param {boolean} params.refresh - Refresh the index after performing the operation
* @param {String} [params.replication=sync] - Specific replication type
* @param {number} params.retry_on_conflict - Specify how many times should the operation be retried when a conflict occurs (default: 0)
* @param {string} params.routing - Specific routing value
* @param {*} params.script - The URL-encoded script definition (instead of using request body)
* @param {Date|Number} params.timeout - Explicit operation timeout
* @param {Date|Number} params.timestamp - Explicit timestamp for the document
* @param {duration} params.ttl - Expiration time for the document
* @param {number} params.version - Explicit version number for concurrency control
* @param {number} params.version_type - Explicit version number for concurrency control
*/
function doUpdate(params) {
var request = {}
, url = {}
, query = {};
params = params || {};
request.body = params.body || null;
request.method = 'POST';
// find the url's params
if (typeof params.id !== 'object' && typeof params.id !== 'undefined') {
url.id = '' + params.id;
} else {
throw new TypeError('Invalid id: ' + params.id + ' should be a string.');
}
if (typeof params.index !== 'object' && typeof params.index !== 'undefined') {
url.index = '' + params.index;
} else {
throw new TypeError('Invalid index: ' + params.index + ' should be a string.');
}
if (typeof params.type !== 'object' && typeof params.type !== 'undefined') {
url.type = '' + params.type;
} else {
throw new TypeError('Invalid type: ' + params.type + ' should be a string.');
}
// build the url
if (url.hasOwnProperty('index') && url.hasOwnProperty('type') && url.hasOwnProperty('id')) {
request.url = '/' + url.index + '/' + url.type + '/' + url.id + '/_update';
}
else {
throw new TypeError('Unable to build a url with those params. Supply at least index, type, id');
}
// build the query string
if (typeof params.consistency !== 'undefined') {
if (_.contains(consistencyOptions, params.consistency)) {
query.consistency = params.consistency;
} else {
throw new TypeError(
'Invalid consistency: ' + params.consistency +
' should be one of ' + consistencyOptions.join(', ') + '.'
);
}
}
if (typeof params.fields !== 'undefined') {
if (typeof params.fields === 'string') {
query.fields = params.fields;
} else if (_.isArray(params.fields)) {
query.fields = params.fields.join(',');
} else {
throw new TypeError('Invalid fields: ' + params.fields + ' should be a comma seperated list or array.');
}
}
if (typeof params.lang !== 'undefined') {
if (typeof params.lang !== 'object' && typeof params.lang !== 'undefined') {
query.lang = '' + params.lang;
} else {
throw new TypeError('Invalid lang: ' + params.lang + ' should be a string.');
}
}
if (typeof params.parent !== 'undefined') {
if (typeof params.parent !== 'object' && typeof params.parent !== 'undefined') {
query.parent = '' + params.parent;
} else {
throw new TypeError('Invalid parent: ' + params.parent + ' should be a string.');
}
}
if (typeof params.percolate !== 'undefined') {
if (typeof params.percolate !== 'object' && typeof params.percolate !== 'undefined') {
query.percolate = '' + params.percolate;
} else {
throw new TypeError('Invalid percolate: ' + params.percolate + ' should be a string.');
}
}
if (typeof params.refresh !== 'undefined') {
if (params.refresh.toLowerCase && (params.refresh = params.refresh.toLowerCase())
&& (params.refresh === 'no' || params.refresh === 'off')
) {
query.refresh = false;
} else {
query.refresh = !!params.refresh;
}
}
if (typeof params.replication !== 'undefined') {
if (_.contains(replicationOptions, params.replication)) {
query.replication = params.replication;
} else {
throw new TypeError(
'Invalid replication: ' + params.replication +
' should be one of ' + replicationOptions.join(', ') + '.'
);
}
}
if (typeof params.retry_on_conflict !== 'undefined') {
if (_.isNumeric(params.retry_on_conflict)) {
query.retry_on_conflict = params.retry_on_conflict * 1;
} else {
throw new TypeError('Invalid retry_on_conflict: ' + params.retry_on_conflict + ' should be a number.');
}
}
if (typeof params.routing !== 'undefined') {
if (typeof params.routing !== 'object' && typeof params.routing !== 'undefined') {
query.routing = '' + params.routing;
} else {
throw new TypeError('Invalid routing: ' + params.routing + ' should be a string.');
}
}
if (typeof params.script !== 'undefined') {
query.script = params.script;
}
if (typeof params.timeout !== 'undefined') {
if (params.timeout instanceof Date) {
query.timeout = params.timeout.getTime();
} else if (_.isNumeric(params.timeout)) {
query.timeout = params.timeout;
} else {
throw new TypeError('Invalid timeout: ' + params.timeout + ' should be be some sort of time.');
}
}
if (typeof params.timestamp !== 'undefined') {
if (params.timestamp instanceof Date) {
query.timestamp = params.timestamp.getTime();
} else if (_.isNumeric(params.timestamp)) {
query.timestamp = params.timestamp;
} else {
throw new TypeError('Invalid timestamp: ' + params.timestamp + ' should be be some sort of time.');
}
}
if (typeof params.ttl !== 'undefined') {
if (_.isInterval(params.ttl)) {
query.ttl = params.ttl;
} else {
throw new TypeError('Invalid ttl: ' + params.ttl + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}
}
if (typeof params.version !== 'undefined') {
if (_.isNumeric(params.version)) {
query.version = params.version * 1;
} else {
throw new TypeError('Invalid version: ' + params.version + ' should be a number.');
}
}
if (typeof params.version_type !== 'undefined') {
if (_.isNumeric(params.version_type)) {
query.version_type = params.version_type * 1;
} else {
throw new TypeError('Invalid version_type: ' + params.version_type + ' should be a number.');
}
}
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = doUpdate;

View File

@ -1,18 +1,114 @@
var _ = require('./Utils')
var _ = require('./utils')
, transports = _.requireDir(module, './transports')
, Log = require('./Log');
, api = _.requireDir(module, '../api')
, Log = require('./log')
, Q = require('q');
// Expose the client object
// Many API commands are namespaced, like cluster.node_stats. The names of these namespaces will be
// tracked here and the namespace objects will be instantiated by reading the values from this
// array
var namespaces = [];
/**
* A client that makes requests to Elasticsearch via a {{#crossLink "Transport"}}Transport{{/crossLink}}
*
* Initializing a client might look something like:
*
* ```
* var client = new Elasticsearch.Client({
* transport: {
* hosts: [
* 'es1.net:9200',
* 'es2.net:9200'
* ],
* sniff_on_start: true
* },
* log: {
* type: 'file',
* level: 'warning'
* }
* })
* ```
*
* @class Client
* @constructor
* @param {Object} [config={}] - Configuration for the transport
* @param {Object} [config.transport] - Transport settings passed to {{#crossLink "Transport"}}Transport Constructor{{/crossLink}}
* @param {String|ArrayOfStrings>} [config.log] - Log output settings {{#crossLink "Log"}}Log Constructor{{/crossLink}}
* @param {Object} [config.trace=false] - Create a log output to stdio that only tracks trace logs
*/
function Client(config) {
config = _.defaults(config || {}, {
logger: 'warning'
});
this.client = this;
for (var i = 0; i < namespaces.length; i++) {
this[namespaces[i]] = new this[namespaces[i]](this);
}
this.hosts = config.hosts;
// For convenience
// this.transport = this.options.transport || new transports.NodeHttp(this.options);
this.logger = config.logger || new Log(this.logger);
this.log = new Log(config && config.log);
// this.serializer = this.options.serializer || new es.Serializer.json();
}
Client.prototype.sniff = function () {
};
/**
* Perform a request with the client's transport
*
* @method request
* @param {object} params
* @param {String} params.url - The url for the request
* @param {String} params.method - The HTTP method for the request
* @param {String} params.body - The body of the HTTP request
* @return {Promise} - A promise that will be resolved with the response
*/
Client.prototype.request = function (params) {
var deferred = Q.defer();
setTimeout(function () {
var response = {
status: 200,
body: {
not: 'a real response'
}
};
this.log.trace(
params.method,
'http://' + this.hosts[Math.floor(Math.random() * this.hosts.length)] + params.url,
params.body,
response.status,
JSON.stringify(response.body)
);
deferred.resolve();
}, 3);
return deferred.promise;
};
// creates a namespace, who's prototype offers the actions within that namespace and this context
// provides the API actions a link back to the client they were intended to operate on.
function makeNamespaceConstructor(actions) {
function Namespace(client) {
this.client = client;
}
Namespace.prototype = actions;
return Namespace;
}
// Extend the Client prototype with the top level API actions and the namespaces for other API actions
_.extend(Client.prototype, _.map(api, function (action, name) {
switch (typeof action) {
case 'function':
return action;
case 'object':
namespaces.push(name);
return makeNamespaceConstructor(action);
}
}));
module.exports = Client;

View File

@ -1,4 +1,4 @@
var _ = require('./Utils')
var _ = require('./utils')
, TransportAbstract = require('./transports/TransportAbstract');
function Interface(methods, properties) {

View File

@ -1,16 +1,17 @@
var _ = require('./Utils'),
var _ = require('./utils'),
EventEmitter = require('events').EventEmitter;
/**
* Log bridge, which writes using one or more Logger's. Setup these loggers by
* specifying their config with the first argument, or by calling addOutput.
* Log bridge, which is an [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
* that sends events to one or more outputs/loggers. Setup these loggers by
* specifying their config as the first argument, or by passing it to addOutput().
*
* @class Log
* @uses Loggers.Stdio
* @constructor
* @extends {@link events.EventEmitter}
*
* @param {string|Object|(string|Object)[]} output - Either the level to setup a single logger, a
* @param {string|Object|ArrayOfStrings|ArrayOfObjects} output - Either the level to setup a single logger, a
* full config object for alogger, or an array of config objects to use for creating log outputs.
* @param {string} output.level - One of the keys in {@link Log.levels} (error, warning, etc.)
* @param {string} output.level - One of the keys in Log.levels (error, warning, etc.)
* @param {string} output.type - The name of the logger to use for this output
*/
function Log(output) {
@ -40,10 +41,6 @@ function Log(output) {
throw new TypeError('Invalid Logging output config');
}
/**
* A list of the log streams, which are listening to this logger.
* @type {Array}
*/
for (i = 0; i < output.length; i++) {
this.addOutput(output[i]);
}
@ -52,45 +49,83 @@ function Log(output) {
_.inherits(Log, EventEmitter);
/**
* Levels observed by the loggers, with their rank
* @type {Object}
* Levels observed by the loggers, ordered by rank
*
* @property levels
* @type Array
* @static
*/
Log.levels = {
// unexpected exceptions and 500s
error: 1,
// correctly formatted error responses from ES (400, ...) and recoverable errors (one node unresponsive)
warning: 2,
// info on what's going on (sniffing etc)
info: 3,
// all requests with query params and no data, response codes & exec times
debug: 4,
// body and response for all requests
trace: 5
};
Log.levelsInverted = _.invert(Log.levels);
Log.levels = [
/**
* Event fired for error level log entries
* @event error
* @param {Error} error - The error object to log
*/
'error'
/**
* Event fired for "warning" level log entries, which usually represent things like
* correctly formatted error responses from ES (400, ...) and recoverable errors
* (one node unresponsive)
*
* @event warning
* @param {String} message - A message to be logged
*/
, 'warning'
/**
* Event fired for "info" level log entries, which usually describe what a client is doing
* (sniffing etc)
*
* @event info
* @param {String} message - A message to be logged
*/
, 'info'
/**
* Event fired for "debug" level log entries, which will describe requests sent, including their
* url (no data, response codes, or exec times)
*
* @event debug
* @param {String} message - A message to be logged
*/
, 'debug'
/**
* Event fired for "trace" level log entries, which provide detailed information about each request
* made from a client, including reponse codes, execution times, and a full curl command that can be
* copied and pasted into a terminal
*
* @event trace
* @param {String} method method, , body, responseStatus, responseBody
* @param {String} url - The url the request was made to
* @param {String} body - The body of the request
* @param {Integer} responseStatus - The status code returned from the response
* @param {String} responseBody - The body of the response
*/
, 'trace'
];
/**
* Converts a log identifier to an integer representing it's level
* Converts a log config value (string or array) to an array of level names which it represents
*
* @method parseLevels
* @static
* @param {*} ident - The identifying to convert, if invalid then the default will be returned
* @param {Integer} default - The default log level to use if the identifier is not valid
* @return {Integer} - The number reprsenting the log level
* @private
* @param {String|ArrayOfStrings} input - Cound be a string to specify the max level, or an array of exact levels
* @return {Array} -
*/
Log.level = function (ident, def) {
if (_.has(Log.levelsInverted, ident)) {
return _.parseInt(ident);
} else {
if (_.has(Log.levels, ident)) {
return Log.levels[ident];
} else {
return def;
}
Log.parseLevels = function (input) {
if (_.isString(input)) {
return _.first(Log.levels, _.indexOf(Log.levels, input) + 1);
}
else if (_.isArray(input)) {
return _.intersection(input, Log.levels);
}
};
/**
* Combine the array-like param into a simple string
*
* @method join
* @static
* @private
* @param {*} arrayish - An array like object that can be itterated by _.each
* @return {String} - The final string.
*/
@ -106,15 +141,20 @@ Log.join = function (arrayish) {
/**
* Create a new logger, based on the config.
* @param {object} config An object with config options for the logger. Type is used to find the
* logger class, and should be one of 'file' or 'stdio' if running in node.js,
* or one of 'console' or 'html' if running in the browser.
* @return {undefined}
*
* @method addOutput
* @param {object} config - An object with config options for the logger.
* @param {String} [config.type=stdio] - The name of an output/logger. Options can be found in the
* `src/loggers` directory.
* @param {String|ArrayOfStrings} [config.levels=warning] - The levels to output to this logger, when an
* array is specified no levels other than the ones specified will be listened to. When a string is
* specified, that and all lower levels will be logged.
* @return {Logger}
*/
Log.prototype.addOutput = function (config) {
_.defaults(config, {
type: 'StdIo',
level: Log.level(config.type, 2)
type: 'stdio',
levels: Log.parseLevels(config.levels || config.level || 'warning')
});
var Logger = require('./loggers/' + config.type);
@ -123,8 +163,10 @@ Log.prototype.addOutput = function (config) {
/**
* Log an error
* @param {Error/String} error The Error to log
* @return {undefined}
*
* @method error
* @param {Error|String} error The Error to log
* @return {Boolean} - True if any outputs accepted the message
*/
Log.prototype.error = function (e) {
if (EventEmitter.listenerCount(this, 'error')) {
@ -135,22 +177,24 @@ Log.prototype.error = function (e) {
/**
* Log a warning message
* @param {...*} msg - Any amount of messages that will be joined before logged
* @return {undefined}
*
* @method warning
* @param {*} msg* - Any amount of messages that will be joined before logged
* @return {Boolean} - True if any outputs accepted the message
*/
Log.prototype.warning = function (/* ...msg */) {
if (EventEmitter.listenerCount(this, 'warning')) {
return this.emit('warning', Log.join(arguments));
}
};
/** @alias Log.warning */
Log.prototype.warn = Log.prototype.warning;
/**
* Log useful info about what's going on
* @param {...*} msg - Any amount of messages that will be joined before logged
* @return {undefined}
*
* @method info
* @param {*} msg* - Any amount of messages that will be joined before logged
* @return {Boolean} - True if any outputs accepted the message
*/
Log.prototype.info = function (/* ...msg */) {
if (EventEmitter.listenerCount(this, 'info')) {
@ -160,8 +204,10 @@ Log.prototype.info = function (/* ...msg */) {
/**
* Log a debug level message
* @param {...*} msg - Any amount of messages that will be joined before logged
* @return {undefined}
*
* @method debug
* @param {*} msg* - Any amount of messages that will be joined before logged
* @return {Boolean} - True if any outputs accepted the message
*/
Log.prototype.debug = function (/* ...msg */) {
if (EventEmitter.listenerCount(this, 'debug')) {
@ -171,15 +217,19 @@ Log.prototype.debug = function (/* ...msg */) {
/**
* Log a trace level message
* @param {...*} msg - Any amount of messages that will be joined before logged
* @return {undefined}
*
* @method trace
* @param {String} method - HTTP request method
* @param {String} url - URL requested
* @param {String} body - The request's body
* @param {Number} responseStatus - The status code returned
* @param {String} responseBody - The body of the returned response
* @return {Boolean} - True if any outputs accepted the message
*/
Log.prototype.trace = function (/* ...msg */) {
Log.prototype.trace = function (method, url, body, responseStatus, responseBody) {
if (EventEmitter.listenerCount(this, 'trace')) {
return this.emit('trace', Log.join(arguments));
return this.emit('trace', method, url, body, responseStatus, responseBody);
}
};
module.exports = Log;

View File

@ -1,4 +1,4 @@
var _ = require('./Utils')
var _ = require('./utils')
, selectors = require('./selector');
var configDefaults = {
@ -8,6 +8,7 @@ var configDefaults = {
port: 9200
}
],
//nodes_to_host_callback : construct_hosts_list,
sniff_on_start : false,
sniff_after_requests : 0,
@ -16,6 +17,21 @@ var configDefaults = {
selector : 'roundRobin'
};
/**
* "Abstract" class responsible for managing connection pools, sniffing for nodes, and serializing/
* deserializing requests and ES errors.
*
* @main Transport
* @class Transport
* @constructor
* @param {Object} [config={}] - An object with configuration parameters
* @param {String|ArrayOfStrings} [config.hosts='localhost:9200'] - Host(s) that this client should communicate with.
* @param {Boolean} [config.sniff_on_start=false] - inspect the cluster for a list of nodes upon startup
* @param {Number} [config.sniff_after_requests=false] - Sniff after completing a certain number of request (disabled by default)
* @param {Boolean} [config.sniff_on_connection_fail=false] - Sniff after a connection fails (disabled by default)
* @param {Number} [config.max_retries=3] - The maximum number of time the client should retry connecting to a node
*/
function Transport(config) {
// These are all unique to each instance of client
config = _.defaults(config || {}, configDefaults);
@ -32,6 +48,8 @@ function Transport(config) {
/**
* Modify the defaults for the Transport class
*
* @method defaults
* @param {Object} update An object representing the changes to be made to the defaults.
* @static
* @return {undefined}

View File

@ -1,70 +1,178 @@
var _ = require('lodash')
, path = require('path')
, requireDir = require('require-directory');
// mix in the node utils
_.mixin(require('util'));
var mixins = {};
var path = require('path')
, _ = require('lodash')
, fs = require('fs')
, requireDir = require('require-directory')
, qs = require('qs');
/**
* [joinPath description]
* @type {[type]}
* Custom utils library, basically a modified version of [lodash](http://lodash.com/docs) +
* [node.uitls](http://nodejs.org/api/util.html#util_util) that doesn't use mixins to prevent
* confusion when requiring lodash itself.
*
* @class utils
* @static
*/
mixins.joinPath = path.join;
var utils = _.extend({}, _, require('util'));
/**
* Link to [path.join](http://nodejs.org/api/path.html#path_path_join_path1_path2)
*
* @method utils.joinPath
* @type {function}
*/
utils.joinPath = path.join;
/**
* Extends lodash's map function so that objects can be passed to map and will be retuned as an object with
* each value transformed by the itterator.
*
* @method utils.map
* @param [Iterable] obj - the thing to iterate over
* @param [Function] mapper - the function to call for each element in obj
* @param [*] context - the this context to use for each call to mapper
*/
utils.map = function (obj, mapper, context) {
if (_.isPlainObject(obj)) {
return _.reduce(obj, function (note, val, key) { note[key] = mapper.call(context, val, key); return note; }, {});
} else {
return _.map(obj, mapper, context);
}
};
/**
* Require all of the modules in a directory
* @param {Module} module The module object which will own the required modules.
* @param {String} path Path to the directory which will be traversed
*
* @method requireDir
* @param {Module} module - The module object which will own the required modules.
* @param {String} path - Path to the directory which will be traversed (can be relative to module)
* @return {Object} - An object with each required file
*/
mixins.requireDir = function (module, dirPath) {
utils.requireDir = function (module, dirPath) {
if (dirPath && dirPath[0] === '.') {
dirPath = path.join(path.dirname(module.filename), dirPath);
}
return requireDir(module, dirPath);
};
/**
* isArrayOf(Strings|Objects|Arrays|Finites|Functions|RegExps)
* @param {Array} arr An array to validate
* Test if a value is an array and it's contents are of a specific type
*
* @method isArrayOf<Strings|Objects|Arrays|Finites|Functions|RegExps>
* @param {Array} arr - An array to check
* @return {Boolean}
*/
'String Object Array Finite Function RegExp'.split(' ').forEach(function (type) {
'String Object PlainObject Array Finite Function RegExp'.split(' ').forEach(function (type) {
var check = _.bind(_['is' + type], _)
// used to find the first value that isn't of the sub type specified
, checkForNotType = function (val) { return !check(val); };
mixins['isArrayOf' + type + 's'] = function (arr) {
utils['isArrayOf' + type + 's'] = function (arr) {
// quick shallow check of arrays
return _.isArray(arr) && !_.find(arr.slice(0, 10), checkForNotType);
};
});
var origBind = _.bind;
/**
* Slightly modified version of bind, which can accept the context as the first
* arg and the method name as the second, like jquery's proxy
* @param {Function|Object} func - The method to bind, or the context if the method will be
* specified using a string in param 2
* @param {Object|String} context - The context when `func` is a function, or the method name to bind
* when func is an object
* @param {...*} [args] Args to be bound to the function
* @return {Function} The bound function
* Capitalize the first letter of a word
*
* @todo Tests
* @method ucfirst
* @param {string} word - The word to transform
* @return {string}
*/
mixins.bind = function (func, context) {
var args = _.rest(arguments, 2);
if (typeof context === 'string') {
// args[1] is actually a method name, like _.bind(this, 'method');
args.unshift(func[context], func);
} else {
args.unshift(func, context);
}
return origBind.apply(_, args);
utils.ucfirst = function (word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase();
};
_.mixin(mixins);
/**
* Transform a string into StudlyCase
*
* @todo Tests
* @method studlyCase
* @param {String} string
* @return {String}
*/
utils.studlyCase = function (string) {
return string.split(/\b|_/).map(function (word) {
return word.match(/^[a-z]+$/i) ? word[0].toUpperCase() + word.substr(1) : '';
}).join('');
};
module.exports = _;
/**
* Transfor a string into camelCase
*
* @todo Tests
* @method cameCase
* @param {String} string
* @return {String}
*/
utils.camelCase = function (string) {
return _.map(string.split(/\b|_/), function (word, i) {
if (word.match(/^[a-z]+$/i)) {
return (i === 0 ? word[0].toLowerCase() : word[0].toUpperCase()) + word.substr(1).toLowerCase();
} else {
return '';
}
}).join('');
};
/**
* Test if a value is "numeric" meaning that it can be transformed into something besides NaN
*
* @todo Test
* @method isNumeric
* @param {*} val
* @return {Boolean}
*/
utils.isNumeric = function (val) {
return !isNaN(val * 1);
};
// regexp to test for intervals
var intervalRE = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
/**
* Test if a string represents an interval (eg. 1m, 2Y)
*
* @todo Test
* @method isInterval
* @param {String} val
* @return {Boolean}
*/
utils.isInterval = function (val) {
return val.match && val.match(intervalRE);
};
/**
* Repeat a string n times
*
* @todo Test
* @todo TestPerformance
* @method repeat
* @param {String} what - The string to repeat
* @param {Number} times - Times the string should be repeated
* @return {String}
*/
utils.repeat = function (what, times) {
return (new Array(times + 1)).join(what);
};
/**
* Convert an object into a query string
*
* @method makeQueryString
* @param {Object} obj - The object to convert
* @param {Boolean} [start=true] - Should the query string start with a '?'
* @return {String}
*/
utils.makeQueryString = function (obj, start) {
var str = qs.stringify(obj);
return (start === false || str === '') ? str : '?' + str;
};
module.exports = utils;

View File

@ -0,0 +1,11 @@
/**
* Manager of connections to a node(s), capable of ensuring that connections are clear and living
* before providing them to the application
*
* @class ConnectionPool
* @constructor
* @param {object} [config={}] - an object describing the configuration for the ConnectionPool
*/
function ConnectionPool(config) {
}

View File

@ -1,4 +1,10 @@
/**
* Logger that writes to a file
*
* @class Loggers.File
* @constructor
* @param {Object} config - Configuration for the FileLogger object
*/
function File(config) {
}

View File

@ -1,61 +1,79 @@
var clc = require('cli-color')
, Log = require('../Log')
, _ = require('../Utils');
, Log = require('../log')
, _ = require('../utils');
/**
* Special version of the Stream logger, which logs errors and warnings to stderr and all other
* levels to stdout.
*
* @class Loggers.Stdio
* @constructor
* @param {Object} config - The configuration for the Logger
* @param {string} config.level - The highest log level for this logger to output. See {@link Log.levels}
* @param {string} config.level - The highest log level for this logger to output.
* @param {Log} bridge - The object that triggers logging events, which we will record
*/
function StdIo(config, bridge) {
function Stdio(config, bridge) {
this.bridge = bridge;
var i, method;
var handlers = this.handlers = {};
// config/state
this.color = _.has(config, 'color') ? !!config.color : true;
this.listeningLevels = [];
this.color = true;
// bound copies of the event handlers
this.handlers = _.reduce(Log.levels, function (handlers, name) {
handlers[name] = _.bindKey(this, 'on' + _.studlyCase(name));
return handlers;
}, {}, this);
_.each(Log.levels, function (i, name) {
// create a version of each log event handler that is bound to "this"
handlers[Log.levels[name]] = _.bind(this, 'on' + name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase());
}, this);
this.setupListeners(config.level);
// then the bridge closes, remove our event listeners
this.bridge.on('closing', _.bindKey(this, 'cleanUpListeners'));
this.setupListeners(config.levels);
}
/**
* Clear the current event listeners and then re-listen for events based on the level specified
*
* @method setupListeners
* @private
* @param {Integer} level - The max log level that this logger should listen to
* @return {undefined}
*/
StdIo.prototype.setupListeners = function (level) {
Stdio.prototype.setupListeners = function (levels) {
this.cleanUpListeners();
for (this.listeningLevel = level; level > 0; level--) {
this.bridge.on(Log.levelsInverted[level], this.handlers[level]);
}
this.listeningLevels = levels;
_.each(this.listeningLevels, function (level) {
this.bridge.on(level, this.handlers[level]);
}, this);
};
/**
* Clear the current event listeners
*
* @method cleanUpListeners
* @private
* @return {undefined}
*/
StdIo.prototype.cleanUpListeners = function () {
for (; this.listeningLevel > 0; this.listeningLevel--) {
// remove the listeners for each event
this.bridge.removeListener(Log.levelsInverted[this.listeningLevel], this.handlers[this.listeningLevel]);
}
Stdio.prototype.cleanUpListeners = function () {
_.each(this.listeningLevels, function (level) {
this.bridge.removeListener(level, this.handlers[level]);
}, this);
};
/**
* Sends output to a stream, does some formatting first
*
* @method write
* @private
* @param {WriteableStream} to - The stream that should receive this message
* @param {String} label - The text that should be used at the begining of the message
* @param {function} colorize - A function that recieves a string and returned a colored version of it
* @param {*} what - The message to log
* @return {undefined}
*/
StdIo.prototype.write = function (to, label, colorize, what) {
Stdio.prototype.write = function (to, label, colorize, what) {
if (this.color) {
label = colorize(label);
}
@ -64,47 +82,77 @@ StdIo.prototype.write = function (to, label, colorize, what) {
/**
* Handler for the bridges "error" event
*
* @method onError
* @private
* @param {Error} e - The Error object to log
* @return {undefined}
*/
StdIo.prototype.onError = function (e) {
Stdio.prototype.onError = function (e) {
this.write(process.stderr, e.name === 'Error' ? 'ERROR' : e.name, clc.red.bold, [e.message, '\n\nStack Trace:\n' + e.stack]);
};
/**
* Handler for the bridges "warning" event
*
* @method onWarning
* @private
* @param {String} msg - The message to be logged
* @return {undefined}
*/
StdIo.prototype.onWarning = function (msg) {
Stdio.prototype.onWarning = function (msg) {
this.write(process.stderr, 'WARNING', clc.yellow.bold, msg);
};
/**
* Handler for the bridges "info" event
*
* @method onInfo
* @private
* @param {String} msg - The message to be logged
* @return {undefined}
*/
StdIo.prototype.onInfo = function (msg) {
Stdio.prototype.onInfo = function (msg) {
this.write(process.stdout, 'INFO', clc.cyan.bold, msg);
};
/**
* Handler for the bridges "debug" event
*
* @method onDebug
* @private
* @param {String} msg - The message to be logged
* @return {undefined}
*/
StdIo.prototype.onDebug = function (msg) {
Stdio.prototype.onDebug = function (msg) {
this.write(process.stdout, 'DEBUG', clc.magentaBright.bold, msg);
};
/**
* Handler for the bridges "trace" event
*
* @method onTrace
* @private
* @param {String} msg - The message to be logged
* @return {undefined}
*/
StdIo.prototype.onTrace = function (msg) {
this.write(process.stdout, 'TRACE', clc.cyanBright.bold, msg);
Stdio.prototype.onTrace = function (method, url, body, responseStatus, responseBody) {
var message = '\nHTTP ' + method + ' ' + url + ' -> ';
if (this.color) {
if (responseStatus === 200) {
message += clc.green.bold(responseStatus);
} else {
message += clc.red.bold(responseStatus);
}
} else {
message += responseStatus;
}
message += '\n' + responseBody + '\n\n';
message += 'curl "' + url.replace('"', '\\"') + '" -X' + method.toUpperCase();
if (body) {
message += ' -d ' + JSON.stringify(JSON.stringify(body));
}
this.write(process.stdout, 'TRACE', clc.cyanBright.bold, message + '\n\n');
};
module.exports = StdIo;
module.exports = Stdio;

View File

@ -1,11 +1,11 @@
/** @module StreamLogger */
/**
* A logger object, which listens to the LogBridge for logging events based on it's config. This
* logger writes it logs to any [WriteableStream]{@link http://nodejs.org/api/stream.html#stream_class_stream_writable_1}.
* logger writes it logs to any [WriteableStream](http://nodejs.org/api/stream.html#stream_class_stream_writable_1).
*
* @class Loggers.Stream
* @constructor
* @param {Object} config A hash of the config options
* @param {[type]} logBridge [description]
* @param {Object} config - A hash of the config options
* @param {Log} logBridge - The log bridge that will emit events for each log entry
*/
function Stream(config, logBridge) {
this.setLevel(config.level);

View File

@ -1,9 +1,7 @@
var _ = require('../Utils');
var _ = require('../utils');
function Random() {}
Random.prototype.select = function (connections) {
function RandomSelect(connections) {
return _.shuffle(connections).unshift();
};
}
module.exports = Random;
module.exports = RandomSelect;

View File

@ -1,8 +0,0 @@
function RoundRobin() {}
RoundRobin.prototype.select = function (connections) {
connections.unshift(connections.pop());
return connections[0];
};
module.exports = RoundRobin;

View File

@ -0,0 +1,6 @@
function RoundRobinSelect(connections) {
connections.unshift(connections.pop());
return connections[0];
}
module.exports = RoundRobinSelect;

View File

@ -1,6 +1,6 @@
/* JSON serializer */
var _ = require('../Utils');
var _ = require('../utils');
function Json() {}

View File

@ -1 +0,0 @@
/* jquery transport client for elasticsearch-js */

View File

@ -1,9 +0,0 @@
function TransportAbstract() {
}
TransportAbstract.prototype = {
};
module.exports = TransportAbstract;

9
src/lib/transports/jquery_xhr.js vendored Normal file
View File

@ -0,0 +1,9 @@
/**
* Transport class for making HTTP requests using jQuery's ajax methods
*
* @class jQueryXhr
* @constructor
*/
function jQueryXhr() {
}

View File

@ -1,7 +1,12 @@
/* elasticsearch-js nodejs transport */
var http = require('http')
, _ = require('../Utils');
, _ = require('../utils');
/**
* Http transport to use in Node.js
*
* @class NodeHttp
*/
function NodeHttp() {
// Split hostname:port into its repective parts