ElasticSearch for Node.JS
npm install sage
Sage exports the sage
global object when used in the browser.
var sage = require('sage');
var es = sage();
var esi = es.index('my-index');
esi.exists(function(err, exists){
console.log(exists);
});
To create a Sage client, call the exported sage
object with an optional URI to ElasticSearch:
// create a Sage client
es = sage();
es = sage("http://localhost:9200");
es = sage("http://user:pass@localhost:9200");
// URI with index name
esi = sage("http://user:pass@localhost:9200/test-index");
Name | Description |
---|---|
uri |
The base URI for this index. |
Select a search index and return an Index
object.
Name | Description |
---|---|
client |
The Sage client associated with this index. |
name |
The index name. |
uri |
The base URI for this index. |
Name | Description |
---|---|
#all |
Fetch multiple documents by type and ID. |
#create |
Create the index. |
#destroy |
Destroy the index. |
#exists |
Check if the index exists. |
#status |
Get detailed status information for this index. |
#type |
The Sage client associated with this index. |
Fetch multiple documents by type and ID via the Multi Get API.
Argument | Type | Required | Description |
---|---|---|---|
type |
String or String[] |
— | The document type(s). |
docs |
Object or Object[] |
The document object(s). | |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
esi.all({
docs: [
{ _type: "type", _id: "1" },
{ _type: "type", _id: "2" }
]
}, function(err, result){
console.log(result);
});
Create the index via the Create Index API.
Argument | Type | Required | Description |
---|---|---|---|
mapping |
Object |
— | The index mapping. |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
esi.create(function(err, result){
console.log(result); // { ok: true, acknowledged: true }
});
Destroy the index via the Destroy Index API.
Argument | Type | Required | Description |
---|---|---|---|
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
esi.destroy(function(err, result){
console.log(result); // { ok: true, acknowledged: true }
});
Check if the index exists via the Indices Exists API.
Argument | Type | Required | Description |
---|---|---|---|
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
esi.exists(function(err, exists){
console.log(exists); // true or false
});
Get detailed status information for this index via the Status API.
Argument | Type | Required | Description |
---|---|---|---|
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
esi.status(function(err, result){
console.log(result);
});
{ ok: true,
_shards: { total: 10, successful: 5, failed: 0 },
indices:
{ test:
{ index: [Object],
translog: [Object],
docs: [Object],
merges: [Object],
refresh: [Object],
flush: [Object],
shards: [Object] } } }
Select a document type and return a Type
object.
Argument | Type | Required | Description |
---|---|---|---|
type |
String or String[] |
The document type(s). |
est = esi.type('my-type');
est = esi.type('my-type,that-type');
est = esi.type(['my-type', 'that-type']);
Name | Description |
---|---|
index |
The Sage index associated with this type. |
name |
The type name. |
uri |
The base URI for this type. |
Name | Description |
---|---|
#all |
Fetch multiple documents by ID. |
#del |
Remove a document from the index. |
#find |
Find documents for the selected type. |
#get |
Fetch a document by type and ID. |
#post |
Add or update one or more documents for indexing, with automatic ID generation. |
#put |
Add or update a document for indexing. |
Fetch multiple documents by ID via the Multi Get API.
Argument | Type | Required | Description |
---|---|---|---|
docs |
Object or Object[] |
The document object(s). | |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
est.all({ ids: [ "1", "2" ] }, function(err, result){
console.log(result);
});
Remove the document from the index via the Delete API.
Argument | Type | Required | Description |
---|---|---|---|
docs |
Object or Object[] |
The document object(s). | |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
est.del({ _id: '1' }, function(err, result){
console.log(result); // { "ok": true }
});
Find documents for the selected type via the Search API.
Fetch a document from the index via the Get API.
Argument | Type | Required | Description |
---|---|---|---|
id |
String |
— | The document ID. |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
est.get('1', function(err, result){
console.log(result);
});
Add or update one or more document for indexing via the Index API or the Bulk API.
Add or update a document for indexing via the Index API.
Argument | Type | Required | Description |
---|---|---|---|
doc |
Object |
The document object. | |
query |
Object |
— | The query string parameters. |
headers |
Object |
— | The header values. |
callback |
Function |
— | The callback function. |
est.put('1', function(err, result){
console.log(result);
});
{ ok: true,
_index: "test",
_type: "my_type",
_id: "1",
_version: 1
}