API Docs for:
Show:

Client Class

Defined in: lib/client.js:13
Module: client
Parent Module: elastical

Creates a new Elastical client associated with the specified host. The client uses ElasticSearch's REST API to interact with the host, so connections are established as needed and are not persistent.

Constructor

Client

(
  • [host="127.0.0.1"]
  • [options]
)

Defined in lib/client.js:13

Parameters:

  • [host="127.0.0.1"] String optional

    Hostname to connect to.

  • [options] Object optional

    Client options.

    • [auth] String optional

      Username and password (delimited by a ":") to pass to ElasticSearch using basic HTTP auth. If not specified, no authentication will be used. Be sure to set options.protocol to 'https' unless you're comfortable sending passwords in plaintext.

    • [curlDebug=false] Boolean optional

      If true, runnable curl commands will be written to stderr for every request the client makes. This is useful for debugging requests by hand.

    • [port=9200] Number optional

      Port to connect to.

    • [protocol='http'] String optional

      Protocol to use. May be "http" or "https".

    • [basePath=''] String optional

      Optional base path to prepend to all query paths. This can be useful if acessing a cluster on a host that uses paths to namespace customer indexes.

    • [timeout=60000] Number optional

      Number of milliseconds to wait before aborting a request. Be sure to increase this if you do large bulk operations.

Example:

// Create a client that connects to http://127.0.0.1:9200
var elastical = require('elastical'),
    client    = new elastical.client();

Methods

_request

(
  • path
  • [options]
  • [callback]
)
protected

Defined in lib/client.js:944

Makes an HTTP request using the request module.

Parameters:

  • path String

    Request path.

  • [options] Object optional

    Request options.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • body Object | Buffer | String

      Response body (parsed as JSON if possible).

analyze

(
  • text
  • [options]
  • [callback]
)
static

Defined in lib/client.js:541

Parameters:

  • text String

    Text to analyze

  • [options] Object optional

    Options. See ElasticSearch docs for details.

    • [index="indexname"] String optional

      Specify indexname to use a specific index analyzer

    • [analyzer="standard"] String optional

      Analyzer to use for analysis

    • [tokenizer="keyword"] String optional

      Tokenizer to use for anaysis when using a custom transient anayzer

    • [field="obj1.field1"] String optional

      Use the analyzer configured in the mapping for this field

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

applyAliasesActions

(
  • actions
  • callback
)

Defined in lib/client.js:384

Apply aliases actions.

ElasticSearch docs

Parameters:

  • actions Object

    Aliases actions.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

bulk

(
  • operations
  • [options]
  • [callback]
)

Defined in lib/client.js:120

Performs multiple document create/index/delete operations in a single request.

See Index.bulk() for detailed usage instructions.

ElasticSearch docs

Parameters:

  • operations Object

    Array of operations to perform. See Index.bulk() for a description of the expected object format.

  • [options] Object optional

    Options. See Index.bulk() for details.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

Example:

client.bulk([
    {create: {index: 'blog', type: 'post', id: '1', data: {
        title: 'Hello',
        body : 'Welcome to my stupid blog.'
    }}},

    {index: {index: 'blog', type: 'post', id: '2', data: {
        title: 'Breaking news',
        body : 'Today I ate a sandwich.'
    }}},

    {delete: {index: 'blog', type: 'post', id: '42'}}
], function (err, res) {
    // ...
});

count

(
  • options
  • [otions.query]
  • [callback]
)
static

Defined in lib/client.js:156

Get the number of matches for a query

ElasticSearch docs

Parameters:

  • options Object

    the options object

    • [index] String optional

      Index name.

    • [type] String optional

      type Type name.

  • [otions.query] String optional

    Query to get the number of match for.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

createIndex

(
  • name
  • options
  • [callback]
)

Defined in lib/client.js:231

Creates a new index.

ElasticSearch docs

Parameters:

  • name String

    Name of the new index.

  • options Object

    Index options (see ElasticSearch docs for details).

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • index Index

      Index instance for the newly created index.

    • data Object

      ElasticSearch response data.

delete

(
  • index
  • type
  • id
  • [options]
  • [callback]
)

Defined in lib/client.js:247

Deletes a document from the specified index. See Index.delete() for the complete list of supported options.

Parameters:

  • index String

    Index name.

  • type String

    Type name.

  • id String

    Document id to delete.

  • [options] Object optional

    Delete options.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response.

deleteIndex

(
  • [names]
  • [callback]
)

Defined in lib/client.js:263

Deletes the specified index or indices. If no indices are specified, all indices on the server will be deleted.

ElasticSearch docs

Parameters:

  • [names] String | String optional

    Name of the index to delete, or an array of names to delete multiple indices. If omitted, all indices will be deleted.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • data Object

      ElasticSearch response data.

deleteRiver

(
  • client
  • name
  • [callback]
)
static

Defined in lib/client.js:927

Deletes a river config from the cluster.

ElasticSearch docs

Parameters:

  • client Client

    Client instance.

  • name String

    A name for this river.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

get

(
  • name
  • id
  • [options]
  • callback
)

Defined in lib/client.js:280

Gets a document from the specified index based on its id.

ElasticSearch docs

Parameters:

  • name String

    Index name.

  • id String

    Document id.

  • [options] Object optional

    Options.

    • [fields] String | String optional

      Document field name or array of field names to retrieve. By default, all fields are retrieved.

    • [ignoreMissing=false] Boolean optional

      If true, an error will not be returned if the index, type, or document do not exist. Instead, a null document will be returned.

    • [preference] String optional

      Controls which shard replicas the request should be executed on. By default, the operation will be randomized between the shard replicas. See the ElasticSearch docs for possible values.

    • [realtime=true] Boolean optional

      Whether or not to use realtime GET. See the ElasticSearch docs for details.

    • [refresh=false] Boolean optional

      If true, the relevant shard will be refreshed before the get operation to ensure that it's searchable. This may cause heavy server load, so use with caution.

    • [routing] String optional

      Value that determines what shard this document will be routed to. If not specified, a hash of the document's id will be used. Note that an incorrectly routed get operation will fail, so it's best to leave this alone unless you know your business.

    • [type="_all"] String optional

      If specified, the get operation will be limited to documents of this type.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • doc Object | Null

      Retrieved document or document fields, or null if the document was not found and options.ignoreMissing is true.

    • res Object

      Full ElasticSearch response data.

getAliases

(
  • names
  • callback
)

Defined in lib/client.js:398

Get aliases.

ElasticSearch docs

Parameters:

  • names String | String | Null

    Index name or array of names.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

getIndex

(
  • name
)
Index

Defined in lib/client.js:319

Gets an Index instance for interacting with the specified ElasticSearch index.

Parameters:

  • name String

    Index name.

Returns:

Index: Index instance.

Example:

var client = new require('elastical').Client(),
    tweets = client.getIndex('tweets');

getMapping

(
  • names
  • type
  • callback
)
static

Defined in lib/client.js:338

Gets mapping definitions for the specified type within the specified index.

ElasticSearch docs

Parameters:

  • names String | String

    Index name or array of names.

  • type String

    Document type. If omitted, mappings for all type are returned.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

getRiver

(
  • client
  • name
  • [callback]
)
static

Defined in lib/client.js:913

Gets river config from the cluster. ElasticSearch docs

Parameters:

  • client Client

    Client instance.

  • name String

    A name for this river.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

getSettings

(
  • names
  • callback
)

Defined in lib/client.js:354

Gets settings for the specified index/indices.

ElasticSearch docs

Parameters:

  • names String | String

    Index name or array of names.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

index

(
  • index
  • type
  • doc
  • [options]
  • [callback]
)

Defined in lib/client.js:412

Adds a document to the specified index.

If the specified index doesn't exist, it will be created.

If a document already exists in that index with the specified type and id, it will be updated. Otherwise, a new document will be created.

ElasticSearch docs

Parameters:

  • index String

    Index name.

  • type String

    Document type.

  • doc Object

    Document data to index.

  • [options] Object optional

    Options.

    • [consistency="quorum"] String optional

      Write consistency to use for this indexing operation. Permitted values are "one", "quorum" and "all". See the ElasticSearch docs for details.

    • [create=false] Boolean optional

      Only create the document if it doesn't already exist.

    • [id] String optional

      Document id. One will be automatically generated if not specified.

    • [parent] String optional

      Parent document id.

    • [percolate] String optional

      Percolation query to check against this document. See the ElasticSearch docs for details.

    • [refresh=false] Boolean optional

      If true, the document will be made searchable immediately after it is indexed.

    • [replication="sync"] String optional

      Replication mode for this indexing operation. Maybe be set to "sync" or "async".

    • [routing] String optional

      Value that determines what shard this document will be routed to. If not specified, a hash of the document's id will be used.

    • [timeout="1m"] String optional

      How long to wait for the primary shard to become available to index this document before aborting. See the ElasticSearch docs for details. This should be a value like "5m" (5 minutes) or "15s" (15 seconds).

    • [version] Number optional

      Document version to create/update. If this is set and options.version_type is not set, options.version_type will automatically be set to "external".

    • [version_type="internal"] String optional

      Version type (either "internal" or "external"). See the ElasticSearch docs for details.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

Example:

client.index('blog', 'post', {
    title  : "Welcome to my stupid blog",
    content: "This is the first and last time I'll post anything.",
    tags   : ['welcome', 'first post', 'last post'],
    created: Date.now()
}, function (err, res) {
    if (err) { throw err; }
    console.log('Indexed a blog post');
});

indexExists

(
  • names
  • callback
)

Defined in lib/client.js:472

Checks whether the specified index or indices exist.

ElasticSearch docs

Parameters:

  • names String | String

    Index name or array of names to check.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • exists Boolean

      true if all specified indices exist, false otherwise.

multiGet

(
  • index
  • type
  • data
  • callback
)

Defined in lib/client.js:487

Multi GET API allows to get multiple documents

ElasticSearch docs

Parameters:

  • index String | Null

    optional Index name.

  • type String | Null

    optional document type.

  • data Object

    either docs or ids

    • [docs] Object optional

      docs to query (can include _index, _type, _id, fields)

    • [ids] String optional

      ids to query

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

putMapping

(
  • [names]
  • type
  • [mapping]
  • [callback]
)
static

Defined in lib/client.js:505

Registers a mapping definition for the specified type within the specified index or indices.

ElasticSearch docs

Parameters:

  • [names] String | String optional

    Index name or array of names to define the mapping within. If not specified, it will be defined in all indices.

  • type String

    Document type.

  • [mapping] Object optional

    Mapping definition. See the ElasticSearch docs for an overview.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

putRiver

(
  • name
  • config
  • [callback]
)
static

Defined in lib/client.js:899

Registers a river with the cluster. ElasticSearch docs

Parameters:

  • name String

    A name for this river.

  • config Object

    The river configuration.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

refresh

(
  • [names]
  • [callback]
)

Defined in lib/client.js:526

Refreshes the specified index or indices.

ElasticSearch docs

Parameters:

  • [names] String | String optional

    Index name or array of names to refresh. If not specified, all indices will be refreshed.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

set

()

Defined in lib/client.js:1025

Alias for index().

setPercolator

(
  • index
  • percolator
  • query
  • callback
)

Defined in lib/client.js:808

Registers a percolator for the given index or modifies the existing percolator if one with the name already exists

ElasticSearch docs

Parameters:

  • index String | String

    Index name or array of index names to register the percolator.

  • percolator String

    The identifier string of the percolator. This identifier is returned when a document matches the query in the percolator, either through percolate operation or through index opertation.

  • query Object | String

    Search query. Afull query object. See the ElasticSearch Query DSL docs for details.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      Full ElasticSearch response data.

Example:

var put =   {
                "query" : {
                    "text" : {
                        "hashtags" : {
                            "query" : 'blah blah blah ',
                            "operator" : "or"
                        }
                    }
                }
            };
client.percolator('tweets', 'mypercolator', query, function (err, res) {
    if (err) { throw err; }
    console.log(results);
});

stats

(
  • [options]
  • []
  • [callback]
)

Defined in lib/client.js:725

Provide statistics on different operations happening on an index. By default, docs, store, and indexing, get, and search stats are returned.

ElasticSearch docs

Parameters:

  • [options] Object optional

    Stats options. Technically this argument is optional, but you'll almost always want to provide at least an index.

    • [index] String | String optional

      Index name or array of index names to display stats on. By default global stats will be displayed.

    • [docs=true] Boolean optional

      The number of docs / deleted docs.

    • [store=true] Boolean optional

      The size of the index.

    • [indexing=true] Boolean optional

      Indexing statistics.

    • [get=true] Boolean optional

      Get statistics, including missing stats.

    • [search=true] Boolean optional

      Search statistics.

    • [warmer=false] Boolean optional

      Warmer statistics.

    • [merge=false] Boolean optional

      merge stats.

    • [flush=false] Boolean optional

      flush stats.

    • [refresh=false] Boolean optional

      refresh stats.

    • [clear=false] Boolean optional

      Clears all the flags (first).

  • [] Object optional

    [options.types] Comma separated list of types to provide document type level stats.

  • [callback] Function optional

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

Example:

var client = new require('elastical').Client();

client.stat({index: 'blog'}, function (err, results) {
    if (err) { throw err; }
    console.log(results);
});

updateSettings

(
  • index
  • settings
  • callback
)

Defined in lib/client.js:368

Update settings for the specified index/indices.

ElasticSearch docs

Parameters:

  • index String | String

    Index name or array of names. If not specified, it will be applied to all indices

  • settings Object

    Settings. See ElasticSearch docs for details.

  • callback Function

    Callback function.

    • err Error | Null

      Error, or null on success.

    • res Object

      ElasticSearch response data.

Properties

_SEARCH_PARAMS

String protected final

Defined in lib/client.js:71

Search options that must be passed as query parameters instead of in the request body.

baseUrl

String

Defined in lib/client.js:86

Base URL for this client, of the form "http://host:port".

port

Number

Defined in lib/client.js:108

Port number for this client.