feat: Add examples for updating documents using ElasticsearchTemplate#700
feat: Add examples for updating documents using ElasticsearchTemplate#700AshenScribe wants to merge 1 commit into
Conversation
Signed-off-by: OmniCoder77 <rishabh_2024bcse025@nitsri.ac.in> Signed-off-by: OmniCoder77 <rishabhsaraswat17@gmail.com>
mp911de
left a comment
There was a problem hiding this comment.
This pull request illustrates why I don't like AI: It generates the wrong thing just faster. In contrast, a human consistently repeats their mistakes or evolves throughout a coding task by learning from their doings.
One might argue: But wait, this code uses API exposed by the framework, what's wrong with it? Actually, every single aspect of it. Contributions to both files are super-inconsistent, API isn't used as it should be, reading the code causes a maximal level of confusion and it is hard to understand what and why some things are done.
| .search(initialQuery, Conference.class) | ||
| .next() | ||
| .flatMap(searchHit -> { | ||
| assert searchHit != null; |
| } | ||
|
|
||
| @Autowired ReactiveElasticsearchOperations operations; | ||
| @Autowired |
There was a problem hiding this comment.
Why is this injecting the Template object? We have already ReactiveElasticsearchOperations at hand.
| } | ||
|
|
||
| @Autowired ElasticsearchOperations operations; | ||
| @Autowired |
There was a problem hiding this comment.
Why is this injecting the Template object? We have already ElasticsearchOperations at hand.
|
|
||
| var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName)); | ||
| SearchHit<Conference> searchHit = template.searchOne(initialQuery, Conference.class); | ||
| assertThat(searchHit).isNotNull(); |
There was a problem hiding this comment.
Asserting an intermediate state quickly creates the impression that we're testing something that shouldn't be tested. When setting up a state, we assume it is correct and expect the given state, otherwise, the entire arrangement is just waste.
| String conferenceId = searchHit.getId(); | ||
|
|
||
| int originalKeywordsCount = conference.getKeywords().size(); | ||
| assertThat(conference.getKeywords()).doesNotContain(newKeyword); |
There was a problem hiding this comment.
Same as above, the need for asserting initial state hints at a potential problem with creating the state and distracts from the thing we actually want to explain here.
|
|
||
| Conference updatedConference = template.get(conferenceId, Conference.class); | ||
| assertThat(updatedConference).isNotNull(); | ||
| assertThat(updatedConference.getKeywords()).contains(newKeyword); |
There was a problem hiding this comment.
contains and hasSize can be combined.
| return template.save(conference) | ||
| .then(template.get(conferenceId, Conference.class)) | ||
| .map(updatedConference -> { | ||
| assert updatedConference != null; |
| } | ||
|
|
||
| @Test | ||
| void shouldUpdateConferenceKeywords() { |
Closes #698
Goal
This pull request addresses the feature request to add examples for updating an existing document, a core use case that was previously missing from the samples.
As suggested in the issue, this implementation uses
ElasticsearchTemplate(ElasticsearchOperations) to perform the update, filling a specific gap in the existing examples and demonstrating a template-based approach instead of a repository-based one.Implementation Details
Imperative Example:
shouldUpdateConferenceKeywords(), has been added toElasticsearchOperationsTest.java.Conferenceobject using aCriteriaQuery, modifies its keyword list, saves the object back usingtemplate.save(), and verifies that the update was successful by re-fetching the document and asserting on its contents.Reactive Example:
shouldUpdateConferenceKeywordsReactive(), has been added toReactiveElasticsearchOperationsTest.java.ReactiveElasticsearchOperations) andStepVerifierto test the entire non-blocking flow of finding, modifying, saving, and verifying the document.Code Style:
@authortags have been added to the class-level Javadoc of the modified files to conform to the project's existing style.