-
Notifications
You must be signed in to change notification settings - Fork 17
Add bulk:updateByQuery and document:mUpsert #633
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
44283da
Add bulk:updateByQuery and document:mUpsert
Yoann-Abbes e9b92a0
indent
Yoann-Abbes 66faa4d
Update doc/7/controllers/bulk/update-by-query/snippets/update-by-quer…
Yoann-Abbes 823013f
requested changes
Yoann-Abbes 0358589
trailing pace
Yoann-Abbes 3ea36dc
typo
scottinet bb4f393
Update doc/7/controllers/document/m-upsert/snippets/m-upsert.js
Yoann-Abbes cf908b9
@aschen requested changes
Yoann-Abbes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| --- | ||
| code: true | ||
| type: page | ||
| title: updateByQuery | ||
| description: Updates documents matching query | ||
| --- | ||
|
|
||
| # updateByQuery | ||
|
|
||
| <SinceBadge version="Kuzzle 2.11.0"/> | ||
| <SinceBadge version="auto-version"/> | ||
|
|
||
| Updates documents matching the provided search query. | ||
|
|
||
| Kuzzle uses the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) syntax. | ||
|
|
||
| An empty or null query will match all documents in the collection. | ||
|
|
||
| <br/> | ||
|
|
||
| ```js | ||
| updateByQuery(index, collection, searchQuery, changes, [options]) | ||
| ``` | ||
|
|
||
| | Argument | Type | Description | | ||
| |---------------|-------------------|-------------------------------------------| | ||
| | `index` | <pre>string</pre> | Index name | | ||
| | `collection` | <pre>string</pre> | Collection name | | ||
| | `searchQuery` | <pre>object</pre> | Query to match | | ||
|
||
| | `changes` | <pre>object</pre> | Partial changes to apply to the documents | | ||
| | `options` | <pre>object</pre> | Optional parameters | | ||
|
|
||
| --- | ||
|
|
||
| ### options | ||
|
|
||
| Additional query options. | ||
|
|
||
| | Options | Type<br/>(default) | Description | | ||
| |-----------|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------| | ||
| | `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) | | ||
|
|
||
| ## Resolves | ||
|
|
||
| Returns the number of updated documents. | ||
|
|
||
| ## Usage | ||
|
|
||
| <<< ./snippets/update-by-query.js | ||
17 changes: 17 additions & 0 deletions
17
doc/7/controllers/bulk/update-by-query/snippets/update-by-query.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| try { | ||
| const result = await kuzzle.bulk.updateByQuery( | ||
| 'nyc-open-data', | ||
| 'yellow-taxi', | ||
| { | ||
| match: { | ||
| capacity: 4 | ||
| } | ||
| }, | ||
| { capacity: 42 }); | ||
| console.log(result); | ||
| /** | ||
| * 2 | ||
| */ | ||
| } catch (error) { | ||
| console.log(error.message); | ||
| } |
18 changes: 18 additions & 0 deletions
18
doc/7/controllers/bulk/update-by-query/snippets/update-by-query.test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| name: bulk#updateByQuery | ||
| description: Update documents matching query | ||
| 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 | ||
| for i in 1 2 ; do | ||
| curl --fail -H "Content-type: application/json" -d '{"capacity": 4}' kuzzle:7512/nyc-open-data/yellow-taxi/document_$i/_create | ||
| done | ||
| for i in 1 2 3 4 5; do | ||
| curl --fail -H "Content-type: application/json" -d '{"capacity": 7}' kuzzle:7512/nyc-open-data/yellow-taxi/_create | ||
| done | ||
| curl -XPOST kuzzle:7512/nyc-open-data/yellow-taxi/_refresh | ||
| after: | ||
| - curl -XDELETE kuzzle:7512/nyc-open-data | ||
| template: default | ||
| expected: 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| code: true | ||
| type: page | ||
| title: mUpsert | ||
| description: Update documents | ||
| --- | ||
|
|
||
| # mUpsert | ||
scottinet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| <SinceBadge version="Kuzzle 2.11.0"/> | ||
| <SinceBadge version="auto-version"/> | ||
|
|
||
| Applies partial changes to multiple documents. If a document doesn't already exist, a new document is created. | ||
|
|
||
| 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. | ||
|
|
||
| <br/> | ||
|
|
||
| ```js | ||
| mUpsert(index, collection, documents, [options]); | ||
| ``` | ||
|
|
||
| | Argument | Type | Description | | ||
| |--------------|---------------------|------------------------------| | ||
| | `index` | <pre>string</pre> | Index name | | ||
| | `collection` | <pre>string</pre> | Collection name | | ||
| | `documents` | <pre>object[]</pre> | Array of documents to update | | ||
scottinet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | `options` | <pre>object</pre> | Query options | | ||
|
|
||
|
|
||
| ### documents | ||
|
|
||
| `documents` is an array of object which each object represents a document. Fields `_id` and `changes` is always mandatory while `default` is optional. | ||
scottinet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Example: | ||
|
|
||
| ```js | ||
| [ | ||
| { | ||
| "_id": "<documentId>", | ||
| "changes": { | ||
| // document partial changes | ||
| }, | ||
| "default": { | ||
| // optional: document fields to add to the "update" part if the document | ||
| // is created | ||
| } | ||
| }, | ||
| { | ||
| "_id": "<anotherDocumentId>", | ||
| "changes": { | ||
| // document partial changes | ||
| }, | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| Additional query options | ||
|
|
||
| | Options | Type<br/>(default) | Description | | ||
| |-------------------|----------------------------------|------------------------------------------------------------------------------------------| | ||
| | `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again | | ||
| | `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) | | ||
| | `retryOnConflict` | <pre>int</pre><br/>(`0`) | The number of times the database layer should retry in case of version conflict | | ||
| | `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> | | ||
|
|
||
| ## Resolves | ||
|
|
||
| Returns an object containing 2 arrays: `successes` and `errors` | ||
|
|
||
| Each updated document is an object of the `successes` array with the following properties: | ||
|
|
||
| | Name | Type | Description | | ||
| |------------|-------------------|--------------------------------------------------------| | ||
| | `_id` | <pre>string</pre> | Document ID | | ||
| | `status` | <pre>number</pre> | HTTP error status | | ||
| | `created` | <pre>boolean</pre>| `true` if the document has been created | | ||
| | `_version` | <pre>number</pre> | Version of the document in the persistent data storage | | ||
| | `_source` | <pre>object</pre> | Document content | | ||
|
|
||
| Each errored document is an object of the `errors` array with the following properties: | ||
|
|
||
| | Name | Type | Description | | ||
| |------------|-------------------|-------------------------------| | ||
| | `document` | <pre>object</pre> | Document that cause the error | | ||
| | `status` | <pre>number</pre> | HTTP error status | | ||
| | `reason` | <pre>string</pre> | Human readable reason | | ||
|
|
||
| ## Usage | ||
|
|
||
| <<< ./snippets/m-upsert.js | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| const doc1 = { capacity: 4 }; | ||
| const doc2 = { capacity: 7 }; | ||
|
|
||
| try { | ||
| await kuzzle.document.create('nyc-open-data', 'yellow-taxi', doc1, 'some-id'); | ||
| await kuzzle.document.create('nyc-open-data', 'yellow-taxi', doc2, 'some-other-id'); | ||
Yoann-Abbes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const documents = [ | ||
| { | ||
| _id: 'some-id', | ||
| changes: { category: 'sedan' } | ||
| }, | ||
| { | ||
| _id: 'some-other-id', | ||
| changes: { category: 'limousine' }, | ||
| default: { capacity: 8 } | ||
| } | ||
| ]; | ||
|
|
||
| const response = await kuzzle.document.mUpsert( | ||
| 'nyc-open-data', | ||
| 'yellow-taxi', | ||
| documents | ||
| ); | ||
|
|
||
| console.log(response); | ||
| /* | ||
| { successes: | ||
| [ { _id: 'some-id', | ||
| _source: { _kuzzle_info: [Object], category: 'sedan' }, | ||
| _version: 2, | ||
| created: false, | ||
| status: 200 }, | ||
| { _id: 'some-other-id', | ||
| _source: { _kuzzle_info: [Object], category: 'limousine', capacity: 8 }, | ||
| _version: 1, | ||
| created: true, | ||
| status: 200 } ], | ||
| errors: [] } | ||
| */ | ||
| console.log('Success'); | ||
| } catch (error) { | ||
| console.error(error.message); | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
doc/7/controllers/document/m-upsert/snippets/m-upsert.test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| name: document#mUpsert | ||
| description: Update documents | ||
| 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: default | ||
| expected: Success |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.