diff --git a/doc/2/controllers/document/upsert/index.md b/doc/2/controllers/document/upsert/index.md new file mode 100644 index 00000000..90ef5d6a --- /dev/null +++ b/doc/2/controllers/document/upsert/index.md @@ -0,0 +1,60 @@ +--- +code: true +type: page +title: upsert +description: Applies partial changes to a document. If the document doesn't already exist, a new document is created. +--- + +# upsert + + + + +Applies partial changes to a document. If the document doesn't already exist, a new document is created. + + +```dart + Future> upsert( + String index, + String collection, + String id, + Map changes, { + bool waitForRefresh = false, + int retryOnConflict, + bool source, + }) +``` + +| Argument | Type | Description | +| ------------ | ----------------- | ----------------------------------------- | +| `index` |
String
| Index name | +| `collection` |
String
| Collection name | +| `id` |
String
| Document ID | +| `changes` |
Map
| Partial content of the document to update | + +### Options + +Additional query options + +| Options | Type
(default) | Description | +| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- | +| `defaults` |
Map

(`{}`) | (optional) fields to add to the document if it gets created | +| `waitForRefresh` |
bool

(`""`) | If set to `true`, waits for the change to be reflected for `search` (up to 1s) | +| `retryOnConflict` |
int

(`10`) | The number of times the database layer should retry in case of version conflict | +| `source` |
bool

(`false`)| If true, returns the updated document inside the response + + +## Returns + +A `Map` with the following properties: + +| Property | Type | Description | +|------------- |--------------------------------------------- |--------------------------------- | +| `_source` |
Map
| Updated document (if `source` option set to true) | +| `_id` |
String
| ID of the udated document | +| `_version` |
int
| Version of the document in the persistent data storage | +| `created` |
bool
| If `true`, a new document was created, otherwise the document existed and was updated | + +## Usage + +<<< ./snippets/upsert.dart diff --git a/doc/2/controllers/document/upsert/snippets/upsert.dart b/doc/2/controllers/document/upsert/snippets/upsert.dart new file mode 100644 index 00000000..8c05a532 --- /dev/null +++ b/doc/2/controllers/document/upsert/snippets/upsert.dart @@ -0,0 +1,6 @@ +final result = await kuzzle + .document + .upsert('nyc-open-data', 'yellow-taxi', 'some-id', { + 'changes': { 'category': 'suv' }, + }, + ); \ No newline at end of file diff --git a/doc/2/controllers/document/upsert/snippets/upsert.test.yml b/doc/2/controllers/document/upsert/snippets/upsert.test.yml new file mode 100644 index 00000000..dc8b71c2 --- /dev/null +++ b/doc/2/controllers/document/upsert/snippets/upsert.test.yml @@ -0,0 +1,10 @@ +name: document#upsert +description: Applies a partial update to an existing document. +hooks: + before: | + curl -XDELETE kuzzle:7512/nyc-open-data + curl -XPOST kuzzle:7512/nyc-open-data/_create + curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi + after: +template: print-result +expected: "{_id: some-id, _version: 1, created: true}" \ No newline at end of file diff --git a/lib/src/controllers/document.dart b/lib/src/controllers/document.dart index 997c689f..4bcd776c 100644 --- a/lib/src/controllers/document.dart +++ b/lib/src/controllers/document.dart @@ -347,7 +347,7 @@ class DocumentController extends KuzzleController { /// /// Optional /// - /// **[refresh]**: if set to wait_for, Kuzzle will not respond + /// **[waitForRefresh]**: if set to wait_for, Kuzzle will not respond /// until the update is indexed \\\ /// **[retryOnConflict]**: conflicts may occur if the same document gets updated multiple times within a short timespan, in a database cluster. You can set the retryOnConflict optional argument (with a retry count), to tell Kuzzle to retry the failing updates the specified amount of times before rejecting the request with an error. \\\ /// **[source]**: if set to true Kuzzle will return the updated document body in the response \\\ @@ -414,6 +414,50 @@ class DocumentController extends KuzzleController { return response.result as Map; } + /// ####Applies a partial update to an existing document. + /// If the document doesn't already exist, a new document is created. + /// + /// **[index]**: index name \\\ + /// **[collection]**: collection name \\\ + /// **[id]**: unique identifier of the document to update \\\ + /// **[changes]**: Partial changes to apply to the document + + /// + /// Optional + /// + /// **[defaults]**: Fields to add to the document if it gets created + /// **[waitForRefresh]**: if set to wait_for, Kuzzle will not respond + /// until the update is indexed \\\ + /// **[retryOnConflict]**: conflicts may occur if the same document gets updated multiple times within a short timespan, in a database cluster. You can set the retryOnConflict optional argument (with a retry count), to tell Kuzzle to retry the failing updates the specified amount of times before rejecting the request with an error. \\\ + /// **[source]**: if set to true Kuzzle will return the updated document body in the response \\\ + Future> upsert( + String index, + String collection, + String id, + Map changes, { + Map defaults, + bool waitForRefresh = false, + int retryOnConflict, + bool source, + }) async { + final response = await kuzzle.query(KuzzleRequest( + controller: name, + action: 'upsert', + index: index, + collection: collection, + uid: id, + body: { + 'changes': changes, + 'defaults': defaults + }, + waitForRefresh: waitForRefresh, + source: source, + retryOnConflict: retryOnConflict, + )); + + return response.result as Map; + } + /// Validates data against existing validation rules. /// /// Documents are always valid if no validation rules are defined diff --git a/lib/src/kuzzle/request.dart b/lib/src/kuzzle/request.dart index ccb7070c..5c5cda75 100644 --- a/lib/src/kuzzle/request.dart +++ b/lib/src/kuzzle/request.dart @@ -200,7 +200,6 @@ class KuzzleRequest { if (source != null) { map['source'] = source; } - if (includeKuzzleMeta != null) { map['includeKuzzleMeta'] = includeKuzzleMeta; }