-
Notifications
You must be signed in to change notification settings - Fork 123
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
[FEATURE] Support for script operations in the update action of the client.helpers.bulk method #210
base: main
Are you sure you want to change the base?
Conversation
Here is the bulkBody of the API request after and before the modification. sample code here: const body = [[
{ update: { _id: '1', _index: 'sample-index'} },
{ script: {
source: "ctx._source.body = params.body;",
lang: 'painless',
params: {
body: "sample body",
}
},
scripted_upsert: true,
},
]];
await client.helpers.bulk({
datasource: body,
onDocument (doc) {
return doc;
}
}); before: {"update":{ "_id": "1", "_index": "sample-index"}},
{"doc":[{"update":{ "_id": "1", "_index": "sample-index"}},{"script":{"source":"ctx._source.body = params.body;","lang":"painless","params":{"body":"sample body"}},"scripted_upsert":true} after: {"update":{ "_id": "1", "_index": "sample-index"}},
{"script":{"source":"ctx._source.body = params.body;","lang":"painless","params":{"body":"sample body"}},"scripted_upsert":true}
// ↑ Even doc operations generate the expected bulkBody. |
Hello @huuyafwww, thanks for opening this! Since this changes a request, I would consider this a major version change which could impact other consumers of the package. To build this in a way that makes it possible to get your desired behavior by making it configurable with the default path being the current request. |
Hello @kavilla , Thanks for the check.
Indeed it is.
Ok. |
This is fine, but if you can determine that the operation is a "doc" or "update" operation by the code below, for example, you know that the developer has entered "doc" or "update" as the return value of onDocument in the first place. const document = Array.isArray(action)
? Object.keys(action[1])[0]
: Object.keys(action)[0] So we assigned payloadBody = serializer.serialize(action[1] || chunk) What do you think about my idea? |
Hi, @kavilla! |
Hello @huuyafwww, will take a look next week if that is okay! Sorry about the delay. |
Hello @kavilla, Thanks again for your thoughtful replies. |
Sorry for the delay, I was incapacitated for the past two weeks! I see what you are getting at and makes sense for me. But since having forked this repo I will need to derive the same level of insight as you. So what you are saying is that by the time the update operation is hit there was essentially no reason why it was checking if it was a doc or not since the value should be correct and that we were previously spreading the completion into the payload body which was the root case of script operations not being supported in update operations. Because basically it was getting incorrectly applied everytime? |
Don't worry about it.
Roger that!
You're right! Also, as you can see, there was no need for a conventional complementary implementation in the first place. In addition, to explain, being able to determine whether the payload body of an update operation is doc or update is no different from receiving that information directly from the return value of onDocument. Therefore, since it is no longer necessary to supplement the payload body as before, we have modified it in this way because we believe that the best implementation is to use the return value given by the developer using this library as-is as the payload body. |
@kavilla I have already patched this pull request change to the business system and am running it in the production environment. |
7753ef9
to
7b3b0df
Compare
Signed-off-by: huuyafwww <[email protected]>
7b3b0df
to
2e47c79
Compare
Since this pull request was created, the contents of the main branch had changed and was no longer working with the latest version, so the main branch was imported and modified. |
Thanks. Needs tests for |
… entry to the changelog Signed-off-by: huuyafwww <[email protected]>
Signed-off-by: huuyafwww <[email protected]>
Sorry for the delay in responding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. Any issues?
thanks. |
@nhtruong would you be willing to take a look, especially with respect to backwards compatibility? |
payloadBody = | ||
typeof chunk === 'string' | ||
? `{"doc":${chunk}}` | ||
: serializer.serialize({ doc: chunk, ...payload }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the reason why this breaks the bulk operation for regular use of this helper method?
(Also sorry the comment mentioning me got lost in the mail)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nhtruong
Yes. The old method only supported doc, so we modified it to support script as well, but that will not work with the previous method of input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid breaking changes for existing users. Unless there are no other ways around this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huuyafwww have you found a work around to avoid breaking current use cases?
Signed-off-by: huuyafwww [email protected]
Description
This is a modification to the client.helpers.bulk method to support script operations in the update action.
Issues Resolved
#209
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.