Installation

npm install sage

Sage exports the sage global object when used in the browser.

Synopsis

var sage = require('sage');
var es   = sage();
var esi  = es.index('my-index');

esi.exists(function(err, exists){
  console.log(exists);
});

Create a Sage Client

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");

Properties

Name Description
uri The base URI for this index.

Methods

#index

Select a search index and return an Index object.

Properties

Name Description
client The Sage client associated with this index.
name The index name.
uri The base URI for this index.

Methods

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.

#all

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.

Example

esi.all({
  docs: [
    { _type: "type", _id: "1" },
    { _type: "type", _id: "2" }
  ]
}, function(err, result){
  console.log(result);
});

#create

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.

Example

esi.create(function(err, result){
  console.log(result); // { ok: true, acknowledged: true }
});

#destroy

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.

Example

esi.destroy(function(err, result){
  console.log(result); // { ok: true, acknowledged: true }
});

#exists

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.

Example

esi.exists(function(err, exists){
  console.log(exists); // true or false
});

#status

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.

Example

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] } } }

#type

Select a document type and return a Type object.

Argument Type Required Description
type String or String[] The document type(s).

Example

est = esi.type('my-type');
est = esi.type('my-type,that-type');
est = esi.type(['my-type', 'that-type']);

Properties

Name Description
index The Sage index associated with this type.
name The type name.
uri The base URI for this type.

Methods

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.

#all

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.

Example

est.all({ ids: [ "1", "2" ] }, function(err, result){
  console.log(result);
});

#del

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.

Example

est.del({ _id: '1' }, function(err, result){
  console.log(result); // { "ok": true }
});

#find

Find documents for the selected type via the Search API.

#get

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.

Example

est.get('1', function(err, result){
  console.log(result);
});

#post

Add or update one or more document for indexing via the Index API or the Bulk API.

#put

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.

Example

est.put('1', function(err, result){
  console.log(result);
});
{ ok: true,
  _index: "test",
  _type: "my_type",
  _id: "1",
  _version: 1
}