Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,103 @@ Index.prototype = {
json : doc
}, callback);
},

/**
Upsert a document in this index.

If a document already exists in this index with the specified _type_ and
_id_, it will be updated. Otherwise, a new document will be created.

[ElasticSearch docs](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html)

@example

var blog = client.getIndex('blog');

blog.upsert('post', {
tags : ['just tagged'],
created: Date.now()
}
,{ id:'post123' }
, function (err, res) {
if (err) { throw err; }
console.log('Updated a blog post');
});

@method upsert
@param {String} type Document type.
@param {Object} doc Document data to index.
@param {Object} [options] Options.
@param {String} [options.consistency="quorum"] Write consistency to use
for this indexing operation. Permitted values are "one", "quorum" and
"all". See the ElasticSearch docs for details.
@param {Boolean} [options.create=false] Only create the document if it
doesn't already exist.
@param {String} [options.id] Document id. Mandatory.
@param {String} [options.parent] Parent document id.
@param {Boolean} [options.refresh=false] If `true`, the document will be
made searchable immediately after it is indexed.
@param {String} [options.replication="sync"] Replication mode for this
indexing operation. Maybe be set to "sync" or "async".
@param {String} [options.routing] Value that determines what shard this
document will be routed to. If not specified, a hash of the document's
id will be used.
@param {String} [options.timeout="1m"] 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).
@param {Number} [options.version] You can use the version parameter to specify that
the document should only be updated if it’s version matches the one specified.
By setting version type to force you can force the new version of the document after
update (use with care! with force there is no guarantee the document didn’t change).
Version types external & external_gte are not supported..
@param {Function} [callback] Callback function.
@param {Error|null} callback.err Error, or `null` on success.
@param {Object} callback.res ElasticSearch response data.
@see Client.index
**/
upsert: function (type, doc, options, callback) {
var query = [],
id, params, url;

if (typeof options === 'function') {
callback = options;
options = {};
}

params = util.merge(options || {});

if (params.id) {
id = params.id;
delete params.id;
}

util.each(params, function (value, name) {
if (value === true || value === false) {
value = value ? '1' : '0';
}

query.push(encode(name) + '=' + encode(value));
});

url = '/' + encode(this.name) + '/' + encode(type);

if (id) {
url += '/' + encode(id) + '/_update';
}

if (query.length) {
url += '?' + query.join('&');
}

this.client._request(url, {
method: 'POST',
json : {
doc : doc,
doc_as_upsert : true
},
}, callback);
},

// http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize.html
optimize: function (options) {
Expand Down