diff --git a/doc/7/controllers/collection/update-mapping/index.md b/doc/7/controllers/collection/update-mapping/index.md index 518288531..343f955ea 100644 --- a/doc/7/controllers/collection/update-mapping/index.md +++ b/doc/7/controllers/collection/update-mapping/index.md @@ -7,7 +7,10 @@ description: Update the collection mapping # updateMapping - + + + +__Use [collection:update](/sdk/js/7/controllers/collection/update/) instead.__ You can define the collection [dynamic mapping policy](/core/2/guides/essentials/database-mappings#dynamic-mapping-policy) by setting the `dynamic` field to the desired value. diff --git a/doc/7/controllers/collection/update/index.md b/doc/7/controllers/collection/update/index.md new file mode 100644 index 000000000..f7edd238a --- /dev/null +++ b/doc/7/controllers/collection/update/index.md @@ -0,0 +1,57 @@ +--- +code: true +type: page +title: update +description: Update the collection mapping +--- + +# update + + + +You can define the collection [dynamic mapping policy](/core/2/guides/essentials/database-mappings#dynamic-mapping-policy) by setting the `dynamic` field to the desired value. + +You can define [collection additional metadata](/core/2/guides/essentials/database-mappings#collection-metadata) within the `_meta` root field. + +
+ +```js +update(index, collection, mapping); +``` + +
+ +| Arguments | Type | Description | +| ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `index` |
string
| Index name | +| `collection` |
string
| Collection name | +| `mapping` |
object
| Describes the collection mapping | + +### mapping + +An object representing the collection data mapping. + +This object must have a root field `properties` that contain the mapping definition: + +```js +const mapping = { + properties: { + field1: { type: 'text' }, + field2: { + properties: { + nestedField: { type: 'keyword' } + } + } + } +}; +``` + +More informations about database mappings [here](/core/2/guides/essentials/database-mappings). + +## Resolves + +Resolve if the collection is successfully updated. + +## Usage + +<<< ./snippets/update.js diff --git a/doc/7/controllers/collection/update/snippets/update.js b/doc/7/controllers/collection/update/snippets/update.js new file mode 100644 index 000000000..16c624f13 --- /dev/null +++ b/doc/7/controllers/collection/update/snippets/update.js @@ -0,0 +1,17 @@ +const mapping = { + dynamic: 'false', + _meta: { + area: 'Panipokhari' + }, + properties: { + plate: { type: 'keyword' } + } +}; + +try { + await kuzzle.collection.update('nyc-open-data', 'yellow-taxi', mapping); + + console.log('Success'); +} catch (error) { + console.error(error.message); +} diff --git a/doc/7/controllers/collection/update/snippets/update.test.yml b/doc/7/controllers/collection/update/snippets/update.test.yml new file mode 100644 index 000000000..db5aed5bd --- /dev/null +++ b/doc/7/controllers/collection/update/snippets/update.test.yml @@ -0,0 +1,10 @@ +name: collection#update +description: Update the collection mapping +hooks: + before: curl -X POST kuzzle:7512/nyc-open-data/_create && curl -X PUT kuzzle:7512/nyc-open-data/yellow-taxi + after: +template: default +expected: Success + +sdk: js +version: 7 diff --git a/src/controllers/Collection.js b/src/controllers/Collection.js index 42ec86320..c3ed130de 100644 --- a/src/controllers/Collection.js +++ b/src/controllers/Collection.js @@ -106,6 +106,17 @@ class CollectionController extends BaseController { .then(response => response.result); } + update(index, collection, body) { + return this.query({ + index, + collection, + body, + action: 'update' + }) + .then(response => response.result); + } + + // @deprecated updateMapping (index, collection, body, options = {}) { return this.query({ index, diff --git a/src/protocols/routes.json b/src/protocols/routes.json index 1a6836f0d..5cae6be41 100644 --- a/src/protocols/routes.json +++ b/src/protocols/routes.json @@ -324,6 +324,10 @@ } }, "collection": { + "update": { + "url": "/:index/:collection/", + "verb": "POST" + }, "updateMapping": { "url": "/:index/:collection/_mapping", "verb": "PUT" diff --git a/test/controllers/collection.test.js b/test/controllers/collection.test.js index 957de1e7e..8d33ba0fa 100644 --- a/test/controllers/collection.test.js +++ b/test/controllers/collection.test.js @@ -328,6 +328,28 @@ describe('Collection Controller', () => { }); }); + describe('update', () => { + it('should call collection/update query with the new mapping and return a Promise which resolves a json object', () => { + kuzzle.query.resolves({ result: { foo: 'bar' } }); + + const body = { foo: 'bar' }; + return kuzzle.collection.update('index', 'collection', body) + .then(res => { + should(kuzzle.query) + .be.calledOnce() + .be.calledWith({ + body, + controller: 'collection', + action: 'update', + index: 'index', + collection: 'collection' + }); + + should(res).match({ foo: 'bar' }); + }); + }); + }); + describe('updateSpecifications', () => { it('should call collection/updateSpecifications query with the new specifications and return a Promise which resolves a json object', () => { kuzzle.query.resolves({ result: { foo: 'bar' } });