Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write and mWrite #1302

Merged
merged 17 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
74 changes: 71 additions & 3 deletions lib/api/controllers/bulkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@

const
BaseController = require('./controller'),
{errors: {PartialError}} = require('kuzzle-common-objects'),
{assertHasBody, assertBodyHasAttribute} = require('../../util/requestAssertions');
{ PartialError } = require('kuzzle-common-objects').errors,
Aschen marked this conversation as resolved.
Show resolved Hide resolved
{
assertHasBody,
assertBodyHasAttribute,
assertHasIndexAndCollection,
assertBodyAttributeType,
assertIdStartsNotUnderscore
} = require('../../util/requestAssertions');

/**
* @class BulkController
* @param {Kuzzle} kuzzle
*/
class BulkController extends BaseController {
constructor (kuzzle) {
super(kuzzle, ['import']);
super(kuzzle, [
'import',
'write',
'mWrite'
]);

this.engine = kuzzle.services.list.storageEngine;
}

Expand All @@ -55,6 +66,63 @@ class BulkController extends BaseController {
return response;
});
}

/**
* Write a document without adding metadata or performing data validation.
* @param {Request} request
* @returns {Promise<Object>}
*/
write(request) {
assertHasBody(request);
assertHasIndexAndCollection(request);
assertIdStartsNotUnderscore(request);

const notify = this.ensureBooleanFlag(request, 'input.args.notify');

return this.engine.writeDocument(request)
.then(response => {
this.kuzzle.indexCache.add(request.input.resource.index, request.input.resource.collection);


if (notify && response.created) {
this.kuzzle.notifier.notifyDocumentCreate(request, response);
}
else if (notify) {
Aschen marked this conversation as resolved.
Show resolved Hide resolved
this.kuzzle.notifier.notifyDocumentReplace(request);
}

return response;
});
}

/**
* Write several documents without adding metadata or performing data validation.
*
* @param {Request} request
* @returns {Promise<Object>}
*/
mWrite(request) {
assertHasBody(request);
assertBodyHasAttribute(request, 'documents');
assertBodyAttributeType(request, 'documents', 'array');
assertHasIndexAndCollection(request);

const notify = this.ensureBooleanFlag(request, 'input.args.notify');

return this.engine.mwriteDocuments(request)
.then(response => {
if (response.error.length > 0) {
request.setError(new PartialError('Some document creations failed', response.error));
}

if (notify) {
this.kuzzle.notifier.notifyDocumentMChanges(request, response.result, true);
}

return {hits: response.result, total: response.result.length};
});
}

}

module.exports = BulkController;
3 changes: 3 additions & 0 deletions lib/api/controllers/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ class BaseController {
const booleanValue = flagValue !== undefined ? true : false;

_.set(request, flagPath, booleanValue);

return booleanValue;
}

return false;
}
}

Expand Down
59 changes: 58 additions & 1 deletion lib/services/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,34 @@ class ElasticSearch extends Service {
});
}

/**
* Create a new document to ElasticSearch, or replace it if it already exist.
* This method does not adds additional metadata.
*
* @param {Request} request
* @returns {Promise} resolve an object that contains _id
*/
writeDocument(request) {
const esRequest = initESRequest(request, ['refresh']);

esRequest.id = request.input.resource._id;
esRequest.body = request.input.body;

assertNoRouting(esRequest);
assertWellFormedRefresh(esRequest);

return this.kuzzle.indexCache.exists(esRequest.index, esRequest.type)
.then(exists => {
if (! exists) {
throw new PreconditionError(`Index '${esRequest.index}' and/or collection '${esRequest.type}' does not exist`);
}

return this.client.index(esRequest)
.then(result => this.refreshIndexIfNeeded(esRequest, _.extend(result, {_source: request.input.body})))
.catch(error => Bluebird.reject(this.esWrapper.formatESError(error)));
});
}

/**
* Sends the partial document to elasticsearch
* with the id to update
Expand Down Expand Up @@ -1109,6 +1137,35 @@ class ElasticSearch extends Service {
return this._mexecute(esRequest, extracted.documents, extracted.rejected);
}

/**
* Create or replace multiple documents at once.
* Does not adds additional metadata
*
* @param {Request} request - Kuzzle API request
* @return {Promise}
*/
mwriteDocuments(request) {
const
esRequest = initESRequest(request, ['consistency', 'refresh', 'timeout', 'fields']),
extracted = extractMDocuments(request, {});

esRequest.body = [];

let i; // NOSONAR
Aschen marked this conversation as resolved.
Show resolved Hide resolved
for (i = 0; i < extracted.documents.length; i++) {
Aschen marked this conversation as resolved.
Show resolved Hide resolved
esRequest.body.push({
index: {
_index: request.input.resource.index,
_type: request.input.resource.collection,
_id: extracted.documents[i]._id
}
});
esRequest.body.push(extracted.documents[i]._source);
Aschen marked this conversation as resolved.
Show resolved Hide resolved
}

return this._mexecute(esRequest, extracted.documents, extracted.rejected);
}

/**
* Update multiple documents with one request
* Replacements are rejected if targeted documents do not exist,
Expand Down Expand Up @@ -1284,7 +1341,7 @@ class ElasticSearch extends Service {
}

/**
* Execute an ES request prepared by mcreate, mupdate, mreplace or mdelete
* Execute an ES request prepared by mcreate, mupdate, mreplace, mdelete or mwriteDocuments
* Returns a standardized ES response object, containing the list of
* successfully performed operations, and the rejected ones
*
Expand Down