elasticsearch
Advanced tools
Comparing version 0.2.6 to 0.3.1
52
index.js
@@ -1,16 +0,46 @@ | ||
var Client = require('./lib/elasticsearch'); | ||
var | ||
cluster = require('./lib/cluster'), | ||
core = require('./lib/core'), | ||
indices = require('./lib/indices'), | ||
request = require('./lib/request'), | ||
function createClient () { | ||
return new Client(arguments); | ||
// defaults applied to request if | ||
// not supplied on instantiation | ||
defaults = { | ||
server : { | ||
host : 'localhost', | ||
port : 9200 | ||
} | ||
}; | ||
// let the magic begin | ||
function createClient (options) { | ||
'use strict'; | ||
options = options || {}; | ||
Object.keys(defaults).forEach(function (key) { | ||
if (!options.hasOwnProperty(key)) { | ||
options[key] = defaults[key]; | ||
} | ||
}); | ||
// backwards compatibility helper... remaps 'index' to '_index' | ||
if (options.index) { | ||
options._index = options.index; | ||
delete options.index; | ||
} | ||
var | ||
req = request.initialize(options.server), | ||
client = core(options, req); | ||
client.cluster = cluster(options, req); | ||
client.indices = indices(options, req); | ||
return client; | ||
} | ||
// Make createClient the default function | ||
// exports | ||
exports = module.exports = createClient; | ||
// Expose constructors | ||
exports.Client = Client; | ||
exports.Cluster = require('./lib/cluster'); | ||
exports.Index = require('./lib/index'); | ||
// For backwards compatibility | ||
exports.createClient = createClient; |
@@ -1,91 +0,241 @@ | ||
var utils = require('./utils'), | ||
qs = require('querystring'); | ||
var utils = require('./utils'); | ||
function Cluster() { | ||
} | ||
module.exports = Cluster; | ||
module.exports = function (config, req, self) { | ||
'use strict'; | ||
Cluster.prototype.status = function(opts, cb) { | ||
if (opts && !cb && typeof(opts) == 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
var def = { | ||
method: 'GET', | ||
path: '/_status' | ||
}; | ||
self = self || {}; | ||
utils.request(utils.mapConfig(def, utils.default_config, opts), null, cb); | ||
} | ||
var paramExcludes = Object.keys(config) | ||
.concat(['field', 'fields', 'node', 'nodes']); | ||
Cluster.prototype.deleteIndices = function(opts, cb) { | ||
if (opts && !cb && typeof(opts) == 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
this.status(function(err, status) { | ||
var count = 0, | ||
total = 0, | ||
active = true, | ||
keys = Object.keys(status.indices); | ||
function checkDone(err, res) { | ||
if (err) { | ||
active = false; | ||
cb(err, res); | ||
} else if (active && count === total) { | ||
cb(null, {success: true, indices: keys}); | ||
} | ||
} | ||
if (keys.length > 0) { | ||
for (var p in status.indices) { | ||
total++; | ||
// http://www.elasticsearch.org/guide/reference/river/ | ||
self.deleteRiver = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var def = { | ||
method: 'DELETE', | ||
path: '/' + p + '/' | ||
}; | ||
if (!options.name) { | ||
return callback(new Error('name is required')); | ||
} | ||
utils.request(utils.mapConfig(def, utils.default_config), undefined, function(err, res) { | ||
count++; | ||
checkDone(err, res); | ||
}); | ||
} | ||
} else { | ||
checkDone(null); | ||
} | ||
}); | ||
} | ||
options.path = req.pathAppend('_river') + | ||
req.pathAppend(options.name); | ||
Cluster.prototype.health = function(opts, cb) { | ||
var health_query = {}, | ||
q_prefix = ''; | ||
if (opts.level) { | ||
health_query.level = opts.level; | ||
q_prefix = '?'; | ||
} | ||
if (opts.wait_for_status) { | ||
health_query.wait_for_status = opts.wait_for_status; | ||
q_prefix = '?'; | ||
} | ||
if (opts.wait_for_relocating_shards) { | ||
health_query.wait_for_relocating_shards = opts.wait_for_relocating_shards; | ||
q_prefix = '?'; | ||
} | ||
if (opts.wait_for_nodes) { | ||
health_query.wait_for_nodes = opts.wait_for_nodes; | ||
q_prefix = '?'; | ||
} | ||
if (opts.timeout) { | ||
health_query.timeout = opts.timeout; | ||
q_prefix = '?'; | ||
} | ||
return req.delete(options, callback); | ||
}; | ||
var def = { | ||
method: 'GET', | ||
path: '_cluster/health' + q_prefix + qs.stringify(health_query) | ||
}; | ||
utils.request(utils.mapConfig(def, utils.default_config, opts), undefined, function(err, res) { | ||
cb(err, res); | ||
}); | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats/ | ||
self.fieldStats = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (!options.field && !options.fields) { | ||
return callback(new Error('field is required')); | ||
} | ||
var | ||
field = utils.getFieldSyntax(options), | ||
// specific exclude of indices param for this operation only | ||
params = req.formatParameters(options, paramExcludes.concat(['indices'])), | ||
path = req.pathAppend(options.indices ? '_stats' : '_nodes/stats/indices') + | ||
req.pathAppend('fielddata') + | ||
req.pathAppend(field); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-health/ | ||
self.health = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster/health'); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-hot-threads/ | ||
// Warning: API is an experiment as documented on ES.org | ||
self.hotThreads = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
node = utils.getNodeSyntax(options, config), | ||
path = req.pathAppend('_nodes') + | ||
req.pathAppend(node) + | ||
req.pathAppend('hot_threads'); | ||
options.path = path; | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/ | ||
self.nodesInfo = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
node = utils.getNodeSyntax(options, config), | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster/nodes') + | ||
req.pathAppend(node); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-stats/ | ||
self.nodesStats = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
node = utils.getNodeSyntax(options, config), | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster/nodes') + | ||
req.pathAppend(node) + | ||
req.pathAppend('stats'); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/river/ | ||
self.putRiver = function (options, meta, callback) { | ||
if (!callback && typeof meta === 'function') { | ||
callback = meta; | ||
meta = options; | ||
options = {}; | ||
} | ||
if (!options.name) { | ||
return callback(new Error('name is required')); | ||
} | ||
options.path = req.pathAppend('_river') + | ||
req.pathAppend(options.name) + | ||
req.pathAppend('_meta'); | ||
return req.put(options, meta, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-reroute/ | ||
self.reroute = function (options, commands, callback) { | ||
if (!callback && typeof commands === 'function') { | ||
callback = commands; | ||
commands = options; | ||
options = {}; | ||
} | ||
var | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster/reroute'); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.post(options, commands, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/river/ | ||
self.rivers = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (!options.name) { | ||
return callback(new Error('name is required')); | ||
} | ||
options.path = req.pathAppend('_river') + | ||
req.pathAppend(options.name) + | ||
req.pathAppend('_meta'); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings/ | ||
self.settings = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
options.path = req.pathAppend('_cluster/settings'); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown/ | ||
self.shutdown = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
node = utils.getNodeSyntax(options, config), | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster') + | ||
req.pathAppend('nodes') + | ||
req.pathAppend(node) + | ||
req.pathAppend('_shutdown'); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.post(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-state/ | ||
self.state = function (options, callback) { | ||
if (!callback && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
var | ||
params = req.formatParameters(options, paramExcludes), | ||
path = req.pathAppend('_cluster/state'); | ||
options.path = path + (params ? '?' + params : ''); | ||
return req.get(options, callback); | ||
}; | ||
// http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings/ | ||
self.updateSettings = function (options, data, callback) { | ||
if (!callback && typeof data === 'function') { | ||
callback = data; | ||
data = options; | ||
options = {}; | ||
} | ||
options.path = req.pathAppend('_cluster/settings'); | ||
return req.put(options, data, callback); | ||
}; | ||
return self; | ||
}; |
233
lib/utils.js
@@ -1,100 +0,151 @@ | ||
var http = require('http'); | ||
var https = require('https'); | ||
/* | ||
Looks through request options and lib config data to determine | ||
the index to use for an operation. | ||
var default_config = module.exports.default_config = { | ||
host: 'localhost', | ||
port: 9200 | ||
Supports (and favors over single index) the multi-index syntax | ||
if an array of index names are supplied (via indices). | ||
Output is formatted as 'indexName' or 'indexName1,indexName2' or '' | ||
http://www.elasticsearch.org/guide/reference/api/multi-index/ | ||
*/ | ||
exports.getIndexSyntax = function (options, config) { | ||
'use strict'; | ||
var syntax = ''; | ||
if (options._indices && Array.isArray(options._indices)) { | ||
syntax = options._indices.join(','); | ||
} else if (options._index) { | ||
syntax = options._index; | ||
} else if (config && config._indices && Array.isArray(config._indices)) { | ||
syntax = config._indices.join(','); | ||
} else if (config && config._index) { | ||
syntax = config._index; | ||
} | ||
return syntax; | ||
}; | ||
var _request = module.exports.request = function(opts, data, cb) { | ||
if (data && !cb && typeof(data) == 'function') { | ||
cb = data; | ||
data = undefined; | ||
} | ||
var callback = function(res) { | ||
var statusCode = res.statusCode; | ||
var chunks = [], | ||
length = 0; | ||
res.on('data', function(chunk) { | ||
chunks.push(chunk); | ||
length += chunk.length; //can't reliably use headers | ||
}); | ||
res.on('end', function() { | ||
var buf = new Buffer(length), | ||
message = '', | ||
total = 0; | ||
chunks.forEach(function chunk_cb(chunk) { | ||
chunk.copy(buf, total, 0); | ||
total += chunk.length; | ||
}); | ||
/* | ||
Looks through request options to determine the field to use for | ||
an operation. | ||
//TODO: figure out a better way to do this, not sure it's possible with inconsistent messages from ES... | ||
var res_json = {}; | ||
try { | ||
res_json = JSON.parse(buf.toString()); | ||
} catch(e) { | ||
res_json = {status: statusCode, error: buf.toString()}; | ||
} | ||
if (cb) { | ||
//Success based status codes. | ||
if (!((""+statusCode).match(/2\d\d/))) { | ||
if (res_json.error) { | ||
message = res_json.error; | ||
} else if (res_json.exists === false) { | ||
message = 'Doc with id "'+res_json._id+'" and type "'+res_json._type+'" not found in index "'+res_json._index+'"'; | ||
} | ||
cb(new Error(message)); | ||
} else { | ||
cb(null, res_json); | ||
} | ||
} | ||
}); | ||
}; | ||
Supports (and favors over single field) multi field syntax | ||
if an array of field names are supplied (via fields | ||
var onError = function(err) { | ||
if (cb) { | ||
cb(err); | ||
} | ||
} | ||
Output is formatted as 'fieldName' or 'fieldName1,fieldName2' or '' | ||
*/ | ||
exports.getFieldSyntax = function (options) { | ||
'use strict'; | ||
var req; | ||
if (opts.secure) { | ||
req = https | ||
.request(opts, callback) | ||
.on('error', onError); | ||
} else { | ||
req = http | ||
.request(opts, callback) | ||
.on('error', onError); | ||
} | ||
var syntax = ''; | ||
if (data) { | ||
req.write(JSON.stringify(data)); | ||
} | ||
if (!(opts.noend)) { | ||
req.end(); | ||
} | ||
} | ||
if (options.fields && Array.isArray(options.fields)) { | ||
syntax = options.fields.join(','); | ||
} else if (options.field) { | ||
syntax = options.field; | ||
} | ||
var mapConfig = module.exports.mapConfig = function() { | ||
var cb = arguments[arguments.length-1]; | ||
var sub_idx = 1; | ||
if (!(typeof(cb) == 'function')) { | ||
sub_idx = 0; | ||
cb = undefined; | ||
} | ||
var new_config = {}; | ||
var len = arguments.length-sub_idx; | ||
for (var i = 0; i < len; i++) { | ||
var o = arguments[i]; | ||
for (var p in o) { | ||
new_config[p] = o[p]; | ||
} | ||
} | ||
if (cb) { | ||
cb(new_config); | ||
} else { | ||
return new_config; | ||
} | ||
} | ||
return syntax; | ||
}; | ||
/* | ||
Looks through request options and lib config data to determine | ||
the node to use for an operation. | ||
Supports (and favors over single node) the multi node syntax | ||
if an array of node names are supplied (via nodes). | ||
Output is formatted as 'nodeName' or 'nodeName1,nodeName2' or '' | ||
*/ | ||
exports.getNodeSyntax = function (options, config) { | ||
'use strict'; | ||
var syntax = ''; | ||
if (options.nodes && Array.isArray(options.nodes)) { | ||
syntax = options.nodes.join(','); | ||
} else if (options.node) { | ||
syntax = options.node; | ||
} else if (config && config.nodes && Array.isArray(config.nodes)) { | ||
syntax = config.nodes.join(','); | ||
} else if (config && config.node) { | ||
syntax = config.node; | ||
} | ||
return syntax; | ||
}; | ||
/* | ||
Looks through request options and lib config data to determine | ||
the type syntax to use for an operation. | ||
Supports (and favors over single type) the multi-type syntax | ||
if an array of type names are supplied (via types). | ||
Output is formatted as 'typeName' or 'typeName1,typeName2' or '' | ||
*/ | ||
exports.getTypeSyntax = function (options, config) { | ||
'use strict'; | ||
var syntax = ''; | ||
if (options._types && Array.isArray(options._types)) { | ||
syntax = options._types.join(','); | ||
} else if (options._type) { | ||
syntax = options._type; | ||
} else if (config && config._types && Array.isArray(config._types)) { | ||
syntax = config._types.join(','); | ||
} else if (config && config._type) { | ||
syntax = config._type; | ||
} | ||
return syntax; | ||
}; | ||
/* | ||
Convenience method for ensuring an expected key exists either in | ||
request options or lib config data. As the operation iterates | ||
through each key supplied in keys, if it is not found as a property | ||
in options or config, the function returns an Error. | ||
If the specified keys are found, false is returned (indicating no | ||
options are undefined). | ||
if '_index' or '_type' is specified, the method will accept the | ||
pluralized versions of those properties without returning an | ||
Error. | ||
*/ | ||
exports.optionsUndefined = function (options, config, keys) { | ||
'use strict'; | ||
var error; | ||
keys.every(function (key) { | ||
if (key === '_index' && | ||
(options.hasOwnProperty('_indices') || | ||
config.hasOwnProperty('_indices'))) { | ||
return true; | ||
} | ||
if (key === '_type' && | ||
(options.hasOwnProperty('_types') || | ||
config.hasOwnProperty('_types'))) { | ||
return true; | ||
} | ||
if (!options.hasOwnProperty(key) && !config.hasOwnProperty(key)) { | ||
error = new Error(key + ' is required'); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return error || false; | ||
}; |
{ | ||
"name": "elasticsearch", | ||
"description": "API around the ElasticSearch RESTful API -- mostly convenience.", | ||
"main": "index.js", | ||
"version": "0.2.6", | ||
"author": "Nick Campbell (http://github.com/ncb000gt)", | ||
"engines": { | ||
"node": ">= 0.6.0" | ||
"name" : "elasticsearch", | ||
"description" : "API around the ElasticSearch RESTful API -- mostly convenience.", | ||
"main" : "index.js", | ||
"version" : "0.3.1", | ||
"author" : "Nick Campbell (http://github.com/ncb000gt)", | ||
"contributors" : [ | ||
"Nick Campbell (http://github.com/ncb000gt)", | ||
"Gabriel Farrel (http://github.com/gsf)", | ||
"Richard Marr (http://github.com/richmarr)", | ||
"Joshua Thomas (http://github.com/brozeph)"], | ||
"engines" : { | ||
"node" : ">= 0.8.0" | ||
}, | ||
"keywords": [ | ||
"keywords" : [ | ||
"elastic", | ||
@@ -17,12 +22,20 @@ "search", | ||
], | ||
"repository": "git://github.com/ncb000gt/node-elasticsearch.git", | ||
"license": "MIT", | ||
"homepage": "http://github.com/ncb000gt/node-elasticsearch", | ||
"bugs": "http://github.com/ncb000gt/node-elasticsearch/issues", | ||
"devDependencies": { | ||
"tap": "~0.4.1" | ||
"repository" : "git://github.com/ncb000gt/node-elasticsearch.git", | ||
"license" : "MIT", | ||
"homepage" : "http://github.com/ncb000gt/node-elasticsearch", | ||
"bugs" : "http://github.com/ncb000gt/node-elasticsearch/issues", | ||
"devDependencies" : { | ||
"chai" : "*", | ||
"coveralls" : "*", | ||
"jscoverage" : "*", | ||
"jshint" : "*", | ||
"mocha" : "*", | ||
"mocha-lcov-reporter" : "*" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"scripts" : { | ||
"coverage" : "rm -rf ./reports ; mkdir -p ./reports ; NODE_ELASTICSEARCH_COVERAGE=true mocha -R html-cov -r ./test/common.js -u bdd ./test/lib > reports/coverage.html", | ||
"pretest" : "jshint *.js ./lib/*.js ./test/*.js ; jscoverage ./lib ./lib-cov", | ||
"test" : "mocha --check-leaks -R spec -r ./test/common.js -u bdd ./test/lib", | ||
"posttest" : "NODE_ELASTICSEARCH_COVERAGE=true mocha -R mocha-lcov-reporter -r ./test/common.js -u bdd ./test/lib | ./node_modules/coveralls/bin/coveralls.js" | ||
} | ||
} |
608
README.md
@@ -1,170 +0,532 @@ | ||
node-elasticsearch | ||
================== | ||
# node-elasticsearch | ||
This is a Node.js module for the [elasticsearch](http://www.elasticsearch.org/) REST API. | ||
[![Build Status](https://travis-ci.org/ncb000gt/node-elasticsearch.png)](https://travis-ci.org/ncb000gt/node-elasticsearch) | ||
[![Build Status](https://travis-ci.org/ncb000gt/node-elasticsearch.png)](https://travis-ci.org/ncb000gt/node-elasticsearch) [![Coverage Status](https://coveralls.io/repos/ncb000gt/node-elasticsearch/badge.png)](https://coveralls.io/r/ncb000gt/node-elasticsearch) | ||
## Install | ||
Usage | ||
===== | ||
```Javascript | ||
npm install elasticsearch | ||
``` | ||
```js | ||
var elasticsearch = require('elasticsearch'); | ||
var es = elasticsearch({index: 'kitteh'}); | ||
## Usage | ||
es.query({query: {field: {field1: 'hai'}}}, function(err, results) { | ||
console.log(results); | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
config = { | ||
_index : 'kittehs' | ||
}, | ||
es = elasticsearch(config); | ||
es.search({ | ||
query : { | ||
field : { | ||
animal : 'kitteh' | ||
} | ||
} | ||
}, function (err, data) { | ||
// work with data here | ||
// response data is according to ElasticSearch spec | ||
}); | ||
``` | ||
## API | ||
Unless otherwise stated, all callback signatures are `function (err, data)`, with `data` being the parsed JSON response from elasticsearch. | ||
#### createClient | ||
Calling `elasticsearch.createClient(config)` is the same as `elasticsearch(config)`. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
es = elasticsearch.createClient(config); | ||
``` | ||
##### config._index | ||
When initializing the library, you may choose to specify an index and/or type to work with at the start to save from having to supply this information in the options for each operation request: | ||
```Javascript | ||
var config = { | ||
_index : 'pet', | ||
_type : 'kitteh' | ||
}; | ||
``` | ||
Additionally, if working with multiple indexes or types, you may specify them as arrays: | ||
```Javascript | ||
var config = { | ||
_indices : ['pet', 'family'], | ||
_types : ['kitteh', 'canine'] | ||
}; | ||
``` | ||
*Note:* When index, indices, type or types are supplied via operation options, those settings will take precident over the base configuration for the library: | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
config = { | ||
_index : 'kitteh' | ||
}, | ||
es = elasticsearch.createClient(config); | ||
es.indices.exist({ _index : 'canine' }, function (err, data) { | ||
// will result in a HEAD request to /canine instead of /kitteh | ||
}); | ||
``` | ||
##### config.server | ||
API | ||
=== | ||
If omitted from configuration, the server settings default to the following: | ||
Unless otherwise stated, all callbacks are called with `cb(err, res)`, with `res` being the parsed JSON response from elasticsearch. | ||
```Javascript | ||
var config = | ||
// optional - when not supplied, defaults to the following: | ||
server : { | ||
host : 'localhost', | ||
port : 9200 | ||
} | ||
}; | ||
``` | ||
elasticsearch(opts) | ||
------------------- | ||
Shortcut for `elasticsearch.createClient(opts)`. | ||
Anything specified within the server element of config is passed directly through to each HTTP/HTTPS request. You may configure additional options for connecting to Elasticsearch: | ||
elasticsearch.createClient(opts) | ||
-------------------------------- | ||
Returns a new client object. | ||
```Javascript | ||
var config = { | ||
server : { | ||
agent : false, | ||
auth : 'user:pass', | ||
host : 'localhost', | ||
port : 9243, | ||
rejectUnauthorized : false, | ||
secure : true // toggles between https and http | ||
} | ||
}; | ||
``` | ||
Options: | ||
* `auth`: Basic authentication for elasticsearch in 'username:password' format | ||
* `index`: The name of the index to act upon | ||
* `host`: The hostname of the elasticsearch server (defaults to localhost) | ||
* `port`: The port of the elasticsearch server (defaults to 9200) | ||
* `rejectUnauthorized`: If specifying secure this may be set to false to bypass certificate validation | ||
* `secure`: Specify true if the elasticsearch server requires TLS/SSL | ||
#### options for any operation | ||
We'll call the returned client `es`. | ||
For each ES operation, options may be specified as the first argument to the function. In most cases, these are entirely optional, but when supplied, the values specified will take precident over the config values passed to the library constructor. | ||
Additionally, if there are extra option keys supplied beyond what is required for the operation, they are mapped directly to the querystring. | ||
es.status(opts, cb) | ||
------------------- | ||
Get the status of the index. Maps to *GET /index/_status*. | ||
``` | ||
var options = { | ||
_index : 'bawss', | ||
_type : 'man', | ||
refresh : true | ||
}; | ||
es.add(opts, doc, cb) | ||
--------------------- | ||
Add a document to the index. Maps to *PUT /index/type/id* or *POST /index/type*. | ||
var doc = { | ||
field1 : 'test value' | ||
}; | ||
Options: | ||
* `id`: Optional ID for the document. A UUID will be chosen by elasticsearch if no ID is specified. | ||
* `type`: Optional type for the document (default: `doc`). | ||
* `refresh`: Set this to true to refresh the index after add. | ||
es.index(options, doc, function (err, data) { | ||
// this will result in a POST with path /bawss/man?refresh=true | ||
}); | ||
``` | ||
es.delete(opts, cb) | ||
------------------- | ||
Delete a document or documents from the index. Maps to *DELETE /index/type/id*. | ||
### Core | ||
Options: | ||
* `id`: Optional ID for the document. All documents of this type will be deleted if no ID is specified. | ||
* `type`: Optional type for the document (default: `doc`). | ||
* `refresh`: Set this to true to refresh the index after delete. | ||
For more specifics and details regarding the core API for ElasticSearch, please refer to the documentation at <http://www.elasticsearch.org/guide/reference/api/>. | ||
es.get(opts, cb) | ||
---------------- | ||
Get a document from the index. Maps to *GET /index/type/id*. | ||
##### Bulk | ||
Options: | ||
* `id`: ID for the document. | ||
* `type`: Optional type for the document (default: `doc`). | ||
`es.bulk(options, commands, callback)` | ||
es.query(opts, query, cb) | ||
------------------------- | ||
Query the index. Maps to *POST /index/_search*. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
es = elasticsearch(); | ||
es.count(opts, query, cb) | ||
------------------------- | ||
Get the count of a query result set. Maps to *POST /index/_count*. | ||
var commands = [ | ||
{ index : { _index : 'dieties', _type : 'kitteh' } }, | ||
{ name : 'hamish', breed : 'manx', color : 'tortoise' }, | ||
{ index : { _index : 'dieties', _type : 'kitteh' } }, | ||
{ name : 'dugald', breed : 'siamese', color : 'white' }, | ||
{ index : { _index : 'dieties', _type : 'kitteh' } }, | ||
{ name : 'keelin', breed : 'domestic long-hair', color : 'russian blue' } | ||
]; | ||
es.queryAll(opts, query, cb) | ||
---------------------------- | ||
Query all indexes. Maps to *POST /_search*. | ||
es.bulk(commands, function (err, data) { | ||
// teh datas | ||
}); | ||
``` | ||
es.putRiver(opts, river, cb) | ||
---------------------------- | ||
Put a new or updated river. Maps to *PUT /_river/name/_meta*. | ||
##### Count | ||
Options: | ||
* `name`: Name for the river. | ||
`es.count(options, callback)` | ||
es.getRiver(opts, name, cb) | ||
--------------------------- | ||
Get a river. Maps to *GET /_river/name/_meta*. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'); | ||
es = elasticsearch(); | ||
es.deleteRiver(opts, name, cb) | ||
------------------------------- | ||
Delete a river. Maps to *DELETE /_river/name/*. | ||
es.count(function (err, data) { | ||
// teh datas | ||
}); | ||
es.putMapping(opts, config, cb) | ||
------------------------------- | ||
Put a new or updated mapping. Maps to *PUT /index/name/_mapping*. | ||
// count docs in a specific index/type | ||
var options = { | ||
_index : 'bawss', | ||
_type : 'kitteh' | ||
} | ||
`name` is defined by `config.name` and `mapping` by `config.mapping`. | ||
es.count(options, function (err, data) { | ||
// counted... like a bawss | ||
}); | ||
``` | ||
es.getMapping(opts, type, cb) | ||
----------------------------- | ||
Get a mapping. Maps to *GET /index/type/_mapping*. | ||
##### Delete | ||
If type is an array its values will be joined. | ||
Requires `_index` be specified either via lib config (as shown below) or via options when calling the operation. | ||
es.deleteMapping(opts, config, cb) | ||
---------------------------------- | ||
Delete a mapping. Maps to *DELETE /index/name/_mapping*. | ||
`es.count(options, callback)` | ||
`name` is defined by `config.name`. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
es = elasticsearch(); | ||
new elasticsearch.Index(opts) | ||
----------------------------- | ||
Returns a new index object. | ||
core.count(function (err, data) { | ||
// teh datas | ||
}); | ||
``` | ||
Options: | ||
* `name`: The name of the index to act upon. | ||
##### Delete By Query | ||
We'll call the returned index `index`. | ||
Requires `_index` be specified either via lib config (as shown below) or via options when calling the operation. | ||
index.status(opts, cb) | ||
---------------------- | ||
Get the status of the index. Maps to *GET /index/_status*. | ||
`es.deleteByQuery(options, query, callback)` | ||
index.refresh(opts, cb) | ||
----------------------- | ||
Refresh the index. Maps to *POST /index/_refresh*. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
es = elasticsearch({ _index : 'kitteh' }); | ||
index.create(opts, config, cb) | ||
------------------------------ | ||
Create the index. Maps to *PUT /index/* or *POST /index/* depending on the existence of `config.mappings`. | ||
var query = { | ||
query : { | ||
field : { breed : 'siamese' } | ||
} | ||
}; | ||
index.delete(cb) | ||
---------------- | ||
Delete the index. Maps to *DELETE /index*. | ||
es.deleteByQuery(query, function (err, data) { | ||
// teh datas | ||
}); | ||
``` | ||
new elasticsearch.Cluster() | ||
--------------------------- | ||
Returns a new cluster object. | ||
##### Exists | ||
We'll call the returned cluster `cluster`. | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
cluster.status(opts, cb) | ||
------------------------ | ||
Get the status of the cluster. Maps to *GET /_status*. | ||
`es.exists(options, callback)` | ||
cluster.deleteIndices(opts, cb) | ||
------------------------------- | ||
Delete all indices in this cluster. Maps to multiple calls to *DELETE /index/*. | ||
```Javascript | ||
var | ||
elasticsearch = require('elasticsearch'), | ||
es = elasticsearch(); | ||
cluster.health(opts, cb) | ||
------------------------ | ||
Get health of the cluster. Maps to *GET _cluster/health*. | ||
es.exists({ _index : 'kitteh' }, function (err, data) { | ||
// teh datas | ||
}); | ||
``` | ||
Options map to query parameters. | ||
##### Explain | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
Also requires `_id`, but this must be specified via options. | ||
Testing | ||
======= | ||
`es.explain(options, query, callback)` | ||
``` | ||
##### Get | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
Also requires `_id`, but this must be specified via options. | ||
`es.get(options, callback)` | ||
##### Index | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
`es.index(options, doc, callback)` | ||
##### More Like This | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
Also requires `_id`, but this must be specified via options. | ||
`es.moreLikeThis(options, callback)` | ||
##### Multi Get | ||
If `_index` and/or `_type` are supplied via options (or lib config), the will applied to the doc that is transmitted for the operation. | ||
`es.multiGet(options, docs, callback)` | ||
##### Multi Search | ||
`es.multiSearch(options, queries, callback)` | ||
##### Percolate | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
`es.percolate(options, doc, callback)` | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
Also requires `name`, but this must be specified via options. | ||
`es.registerPercolator(options, query, callback)` | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
Also requires `name`, but this must be specified via options. | ||
`es.unregisterPercolator(options, callback)` | ||
##### Search | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.search(options, query, callback)` | ||
##### Update | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
Also requires `_id`, but this must be specified via options. | ||
`es.update(options, doc, callback)` | ||
##### Validate | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.validate(options, query, callback)` | ||
### Indices | ||
All operations here interact with the indices segment of the Elasticsearch API. | ||
##### Alias | ||
`es.indices.alias(options, data, callback)` | ||
##### Aliases | ||
Requires `alias`, but this must be specified via options. | ||
`es.indices.aliases(options, callback)` | ||
##### Analyze | ||
`es.indices.analyze(options, data, callback)` | ||
##### Clear Cache | ||
`es.indices.clearCache(options, callback)` | ||
##### Close Index | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.closeIndex(options, callback)` | ||
##### Create Index | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.createIndex(options, data, callback)` | ||
##### Create Template | ||
Requires `name`, but this must be specified via options. | ||
`es.indices.createTemplate(options, template, callback)` | ||
##### Delete Alias | ||
Requires `_index` and `_alias` be specified either via lib config or via options when calling the operation. | ||
`es.indices.deleteAlias(opitons, callback)` | ||
##### Delete Index | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.deleteIndex(options, callback)` | ||
##### Delete Mapping | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
`es.indices.deleteMapping(options, callback)` | ||
##### Delete Template | ||
Requires `name`, but this must be specified via options. | ||
`es.indices.deleteTemplate(options, callback)` | ||
##### Delete Warmer | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
Also requires `name`, but this must be specified via options. | ||
`es.indices.deleteWarmer(options, callback)` | ||
##### Exists | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.exists(options, callback)` | ||
##### Flush | ||
`es.indices.flush(options, callback)` | ||
##### Mappings | ||
`es.indices.mappings(options, callback)` | ||
##### Open Index | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.openIndex(options, callback)` | ||
##### Optimize | ||
`es.indices.optimize(options, callback)` | ||
##### Put Mapping | ||
Requires `_index` and `_type` be specified either via lib config or via options when calling the operation. | ||
`es.indices.putMapping(options, mapping, callback)` | ||
##### Put Warmer | ||
Requires `name`, but this must be specified via options. | ||
`es.indices.putWarmer(options, warmer, callback)` | ||
##### Refresh | ||
`es.indices.refresh(options, callback)` | ||
##### Segments | ||
`es.indices.segments(options, callback)` | ||
##### Settings | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.settings(options, callback)` | ||
##### Snapshot | ||
`es.indices.snapshot(options, callback)` | ||
##### Stats | ||
`es.indices.stats(options, callback)` | ||
##### Status | ||
`es.indices.status(options, callback` | ||
##### Templates | ||
Requires `name`, but this must be specified via options. | ||
`es.indices.templates(options, callback)` | ||
##### Update Settings | ||
`es.indices.updateSettings(options, settings, callback)` | ||
##### Warmers | ||
Requires `_index` be specified either via lib config or via options when calling the operation. | ||
`es.indices.warmers(options, callback)` | ||
### Cluster | ||
All operations here interact with the Cluster portion of the Elasticsearch API. | ||
##### Delete River | ||
Requires `name`, but this must be specified via options. | ||
`es.cluster.deleteRiver(options, callback)` | ||
##### Field Stats | ||
Requires `field` or `fields`, but this must be specified via options. | ||
`es.cluster.fieldStats(options, callback)` | ||
##### Health | ||
`es.cluster.health(options, callback)` | ||
##### Hot Threads | ||
`es.cluster.hotThreads(options, callback)` | ||
##### Nodes Info | ||
`es.cluster.nodesInfo(options, callback)` | ||
##### Nodes Status | ||
`es.cluster.nodesStatus(options, callback)` | ||
##### Put River | ||
Requires `name`, but this must be specified via options. | ||
`es.cluster.putRiver(options, meta, callback)` | ||
##### Reroute | ||
`es.cluster.reroute(options, commands, callback)` | ||
##### Rivers | ||
Requires `name`, but this must be specified via options. | ||
`es.cluster.rivers(options, callback)` | ||
##### Settings | ||
`es.cluster.settings(options, callback)` | ||
##### Shutdown | ||
`es.cluster.shutdown(options, callback)` | ||
##### State | ||
`es.cluster.state(options, callback)` | ||
##### Update Settings | ||
`es.cluster.updateSettings(options, updates, callback)` | ||
# Testing | ||
Note that a test coverage report is sent to coveralls.io during CI... running locally will result in a response similar to `Bad response: 500 {"message":"Build processing error.","error":true,"url":""}`. | ||
Code coverage data generated from npm test is located in `./lib-cov` and is not included in the git repo. | ||
```Javascript | ||
npm install | ||
@@ -174,6 +536,10 @@ npm test | ||
To run code coverage and generate local report at `./reports/coverage.html`: | ||
Requirements | ||
============ | ||
```Javascript | ||
npm run-script coverage | ||
``` | ||
# Requirements | ||
* Node.js | ||
@@ -183,6 +549,4 @@ * elasticsearch | ||
# License | ||
License | ||
======= | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122085
16
3461
551
6
4
3