Client Class
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]
Parameters:
-
[host="127.0.0.1"]
String optionalHostname to connect to.
-
[options]
Object optionalClient options.
-
[auth]
String optionalUsername 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 optionalIf
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 optionalPort to connect to.
-
[protocol='http']
String optionalProtocol to use. May be "http" or "https".
-
[basePath='']
String optionalOptional 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 optionalNumber 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();
Item Index
Methods
- _request
- analyze static
- applyAliasesActions
- bulk
- count static
- createIndex
- delete
- deleteIndex
- deleteRiver static
- get
- getAliases
- getIndex
- getMapping static
- getRiver static
- getSettings
- index
- indexExists
- multiGet
- putMapping static
- putRiver static
- refresh
- search
- set
- setPercolator
- stats
- updateSettings
Properties
Methods
_request
-
path
-
[options]
-
[callback]
Makes an HTTP request using the request
module.
Parameters:
-
path
StringRequest path.
-
[options]
Object optionalRequest options.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
body
Object | Buffer | StringResponse body (parsed as JSON if possible).
-
analyze
-
text
-
[options]
-
[callback]
Analyze
Parameters:
-
text
StringText to analyze
-
[options]
Object optionalOptions. See ElasticSearch docs for details.
-
[index="indexname"]
String optionalSpecify indexname to use a specific index analyzer
-
[analyzer="standard"]
String optionalAnalyzer to use for analysis
-
[tokenizer="keyword"]
String optionalTokenizer to use for anaysis when using a custom transient anayzer
-
[field="obj1.field1"]
String optionalUse the analyzer configured in the mapping for this field
-
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
applyAliasesActions
-
actions
-
callback
Apply aliases actions.
Parameters:
-
actions
ObjectAliases actions.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
bulk
-
operations
-
[options]
-
[callback]
Performs multiple document create/index/delete operations in a single request.
See Index.bulk()
for detailed usage instructions.
Parameters:
-
operations
ObjectArray of operations to perform. See
Index.bulk()
for a description of the expected object format. -
[options]
Object optionalOptions. See
Index.bulk()
for details. -
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch 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]
Get the number of matches for a query
Parameters:
-
options
Objectthe options object
-
[index]
String optionalIndex name.
-
[type]
String optionaltype Type name.
-
-
[otions.query]
String optionalQuery to get the number of match for.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
createIndex
-
name
-
options
-
[callback]
Creates a new index.
Parameters:
-
name
StringName of the new index.
-
options
ObjectIndex options (see ElasticSearch docs for details).
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
index
IndexIndex instance for the newly created index.
-
data
ObjectElasticSearch response data.
-
delete
-
index
-
type
-
id
-
[options]
-
[callback]
Deletes a document from the specified index. See Index.delete()
for the
complete list of supported options.
Parameters:
-
index
StringIndex name.
-
type
StringType name.
-
id
StringDocument id to delete.
-
[options]
Object optionalDelete options.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response.
-
deleteIndex
-
[names]
-
[callback]
Deletes the specified index or indices. If no indices are specified, all indices on the server will be deleted.
Parameters:
-
[names]
String | String optionalName of the index to delete, or an array of names to delete multiple indices. If omitted, all indices will be deleted.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
data
ObjectElasticSearch response data.
-
deleteRiver
-
client
-
name
-
[callback]
Deletes a river config from the cluster.
Parameters:
-
client
ClientClient instance.
-
name
StringA name for this river.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
get
-
name
-
id
-
[options]
-
callback
Gets a document from the specified index based on its id.
Parameters:
-
name
StringIndex name.
-
id
StringDocument id.
-
[options]
Object optionalOptions.
-
[fields]
String | String optionalDocument field name or array of field names to retrieve. By default, all fields are retrieved.
-
[ignoreMissing=false]
Boolean optionalIf
true
, an error will not be returned if the index, type, or document do not exist. Instead, anull
document will be returned. -
[preference]
String optionalControls 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 optionalWhether or not to use realtime GET. See the ElasticSearch docs for details.
-
[refresh=false]
Boolean optionalIf
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 optionalValue 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 optionalIf specified, the get operation will be limited to documents of this type.
-
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
doc
Object | NullRetrieved document or document fields, or
null
if the document was not found andoptions.ignoreMissing
istrue
. -
res
ObjectFull ElasticSearch response data.
-
getAliases
-
names
-
callback
Get aliases.
Parameters:
-
names
String | String | NullIndex name or array of names.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
getIndex
-
name
Gets an Index instance for interacting with the specified ElasticSearch index.
Parameters:
-
name
StringIndex name.
Returns:
Example:
var client = new require('elastical').Client(),
tweets = client.getIndex('tweets');
getMapping
-
names
-
type
-
callback
Gets mapping definitions for the specified type within the specified index.
Parameters:
-
names
String | StringIndex name or array of names.
-
type
StringDocument type. If omitted, mappings for all type are returned.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
getRiver
-
client
-
name
-
[callback]
Gets river config from the cluster. ElasticSearch docs
Parameters:
-
client
ClientClient instance.
-
name
StringA name for this river.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
getSettings
-
names
-
callback
Gets settings for the specified index/indices.
Parameters:
-
names
String | StringIndex name or array of names.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
index
-
index
-
type
-
doc
-
[options]
-
[callback]
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.
Parameters:
-
index
StringIndex name.
-
type
StringDocument type.
-
doc
ObjectDocument data to index.
-
[options]
Object optionalOptions.
-
[consistency="quorum"]
String optionalWrite consistency to use for this indexing operation. Permitted values are "one", "quorum" and "all". See the ElasticSearch docs for details.
-
[create=false]
Boolean optionalOnly create the document if it doesn't already exist.
-
[id]
String optionalDocument id. One will be automatically generated if not specified.
-
[parent]
String optionalParent document id.
-
[percolate]
String optionalPercolation query to check against this document. See the ElasticSearch docs for details.
-
[refresh=false]
Boolean optionalIf
true
, the document will be made searchable immediately after it is indexed. -
[replication="sync"]
String optionalReplication mode for this indexing operation. Maybe be set to "sync" or "async".
-
[routing]
String optionalValue 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 optionalHow 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 optionalDocument 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 optionalVersion type (either "internal" or "external"). See the ElasticSearch docs for details.
-
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch 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
Checks whether the specified index or indices exist.
Parameters:
-
names
String | StringIndex name or array of names to check.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
exists
Booleantrue
if all specified indices exist,false
otherwise.
-
multiGet
-
index
-
type
-
data
-
callback
Multi GET API allows to get multiple documents
Parameters:
-
index
String | Nulloptional Index name.
-
type
String | Nulloptional document type.
-
data
Objecteither docs or ids
-
[docs]
Object optionaldocs to query (can include _index, _type, _id, fields)
-
[ids]
String optionalids to query
-
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
putMapping
-
[names]
-
type
-
[mapping]
-
[callback]
Registers a mapping definition for the specified type within the specified index or indices.
Parameters:
-
[names]
String | String optionalIndex name or array of names to define the mapping within. If not specified, it will be defined in all indices.
-
type
StringDocument type.
-
[mapping]
Object optionalMapping definition. See the ElasticSearch docs for an overview.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
putRiver
-
name
-
config
-
[callback]
Registers a river with the cluster. ElasticSearch docs
Parameters:
-
name
StringA name for this river.
-
config
ObjectThe river configuration.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
refresh
-
[names]
-
[callback]
Refreshes the specified index or indices.
Parameters:
-
[names]
String | String optionalIndex name or array of names to refresh. If not specified, all indices will be refreshed.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
search
-
[options]
-
callback
Searches for documents matching the given query.
Parameters:
-
[options]
Object optionalSearch options. Technically this argument is optional, but you'll almost always want to provide at least a query.
-
[query]
Object | String optionalSearch query. This can be a simple string (in which case a "query_string" search will be performed) or a full query object. See the ElasticSearch Query DSL docs for details.
-
[explain=false]
Boolean optionalIf
true
, results will include an explanation of how the score was computed for each hit. -
[facets]
Object optionalFacets to aggregate by. See the ElasticSearch facets docs for details.
-
[fields]
String | String optionalDocument field name or array of field names to retrieve. By default, all fields are retrieved.
-
[filter]
Object optionalResult filter. See the ElasticSearch filtering docs for details.
-
[from=0]
Number optionalReturn results starting at this offset.
-
[highlight]
Object optionalResult highlighting options. See the ElasticSearch highlighting docs for details.
-
[index]
String | String optionalIndex name or array of index names to search. By default all indices will be searched.
-
[indices_boost]
Object optionalIndex boost options. See the ElasticSearch docs for details.
-
[min_score]
Number optionalIf specified, documents with a score lower than this will be filtered out.
-
[preference]
String optionalControls 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.
-
[routing]
String optionalValue that determines what shard this search will be routed to.
-
[script_fields]
Object optionalScript expressions to evaluate for specific fields. See the ElasticSearch script fields docs for details.
-
[scroll]
String optionalScroll timeout. If specified, nodes that participate in this search will maintain resources for this query until the timeout expires. See the ElasticSearch docs for details.
-
[scroll_id]
String optionalScroll id to use for this request.
-
[search_type]
String optionalSearch operation type to use. May be one of "queryandfetch", "querythenfetch", "dfsqueryandfetch", "dfsquerythenfetch", "count", or "scan". See the ElasticSearch docs for details.
-
[size=10]
Number optionalReturn this many results.
-
[sort]
Object optionalSort options. See the ElasticSearch sort docs for details.
-
[timeout]
String optionalTimeout after which the search will be aborted. Any hits that have been gathered before the timeout is reached will be returned. Default is no timeout.
-
[track_scores=false]
Boolean optionalWhether or not to compute scores when sorting by a field.
-
[type]
String | String optionalType name or array of type names to search. By default all types will be searched.
-
[version=false]
Boolean optionalIf
true
, a version number will be returned for each hit.
-
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
results
ObjectSearch results.
-
res
ObjectFull ElasticSearch response data.
-
Example:
var client = new require('elastical').Client();
client.search({query: 'pie'}, function (err, results) {
if (err) { throw err; }
console.log(results);
});
set
()
Alias for index().
setPercolator
-
index
-
percolator
-
query
-
callback
Registers a percolator for the given index or modifies the existing percolator if one with the name already exists
Parameters:
-
index
String | StringIndex name or array of index names to register the percolator.
-
percolator
StringThe 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 | StringSearch query. Afull query object. See the ElasticSearch Query DSL docs for details.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectFull 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]
Provide statistics on different operations happening on an index. By default, docs, store, and indexing, get, and search stats are returned.
Parameters:
-
[options]
Object optionalStats options. Technically this argument is optional, but you'll almost always want to provide at least an index.
-
[index]
String | String optionalIndex name or array of index names to display stats on. By default global stats will be displayed.
-
[docs=true]
Boolean optionalThe number of docs / deleted docs.
-
[store=true]
Boolean optionalThe size of the index.
-
[indexing=true]
Boolean optionalIndexing statistics.
-
[get=true]
Boolean optionalGet statistics, including missing stats.
-
[search=true]
Boolean optionalSearch statistics.
-
[warmer=false]
Boolean optionalWarmer statistics.
-
[merge=false]
Boolean optionalmerge stats.
-
[flush=false]
Boolean optionalflush stats.
-
[refresh=false]
Boolean optionalrefresh stats.
-
[clear=false]
Boolean optionalClears all the flags (first).
-
-
[]
Object optional[options.types] Comma separated list of types to provide document type level stats.
-
[callback]
Function optionalCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch 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
Update settings for the specified index/indices.
Parameters:
-
index
String | StringIndex name or array of names. If not specified, it will be applied to all indices
-
settings
ObjectSettings. See ElasticSearch docs for details.
-
callback
FunctionCallback function.
-
err
Error | NullError, or
null
on success. -
res
ObjectElasticSearch response data.
-
Properties
_SEARCH_PARAMS
String
protected
final
Search options that must be passed as query parameters instead of in the request body.
baseUrl
String
Base URL for this client, of the form "http://host:port".
port
Number
Port number for this client.