-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: Add examples for updating documents using ElasticsearchTemplate #700
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,17 @@ | |
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.elasticsearch.client.ClientConfiguration; | ||
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; | ||
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.data.elasticsearch.core.geo.GeoPoint; | ||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; | ||
import org.springframework.data.elasticsearch.core.query.Criteria; | ||
|
@@ -43,6 +46,7 @@ | |
* @author Artur Konczak | ||
* @author Oliver Gierke | ||
* @author Christoph Strobl | ||
* @author omniCoder77 | ||
* @author Prakhar Gupta | ||
* @author Peter-Josef Meisch | ||
*/ | ||
|
@@ -73,6 +77,8 @@ public ClientConfiguration clientConfiguration() { | |
} | ||
|
||
@Autowired ElasticsearchOperations operations; | ||
@Autowired | ||
ElasticsearchTemplate template; | ||
|
||
@Test | ||
void textSearch() throws ParseException { | ||
|
@@ -103,4 +109,29 @@ void geoSpatialSearch() { | |
|
||
assertThat(result).hasSize(2); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldUpdateConferenceKeywords() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use spaces and not tabs. |
||
var conferenceName = "JDD14 - Cracow"; | ||
var newKeyword = "java-ee"; | ||
|
||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
|
||
Conference conference = searchHit.getContent(); | ||
String conferenceId = searchHit.getId(); | ||
|
||
int originalKeywordsCount = conference.getKeywords().size(); | ||
assertThat(conference.getKeywords()).doesNotContain(newKeyword); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.getKeywords().add(newKeyword); | ||
|
||
template.save(conference); | ||
|
||
Conference updatedConference = template.get(conferenceId, Conference.class); | ||
assertThat(updatedConference).isNotNull(); | ||
assertThat(updatedConference.getKeywords()).contains(newKeyword); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
assertThat(updatedConference.getKeywords()).hasSize(originalKeywordsCount + 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.text.ParseException; | ||
|
@@ -43,6 +44,7 @@ | |
* | ||
* @author Christoph Strobl | ||
* @author Prakhar Gupta | ||
* @author omniCoder77 | ||
* @author Peter-Josef Meisch | ||
*/ | ||
@SpringBootTest( | ||
|
@@ -74,6 +76,8 @@ public ClientConfiguration clientConfiguration() { | |
} | ||
|
||
@Autowired ReactiveElasticsearchOperations operations; | ||
@Autowired | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this injecting the Template object? We have already |
||
ReactiveElasticsearchTemplate template; | ||
|
||
@Test | ||
void textSearch() { | ||
|
@@ -100,4 +104,39 @@ private void verify(SearchHit<Conference> hit, String expectedWord, String expec | |
fail("o_O", e); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void shouldUpdateConferenceKeywordsReactive() { | ||
var conferenceName = "JDD14 - Cracow"; | ||
var newKeyword = "java-ee"; | ||
|
||
var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName)); | ||
|
||
StepVerifier.create( | ||
template | ||
.search(initialQuery, Conference.class) | ||
.next() | ||
.flatMap(searchHit -> { | ||
assert searchHit != null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Conference conference = searchHit.getContent(); | ||
String conferenceId = searchHit.getId(); | ||
|
||
int originalKeywordsCount = conference.getKeywords().size(); | ||
assert !conference.getKeywords().contains(newKeyword); | ||
|
||
conference.getKeywords().add(newKeyword); | ||
|
||
return template.save(conference) | ||
.then(template.get(conferenceId, Conference.class)) | ||
.map(updatedConference -> { | ||
assert updatedConference != null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we use AssertJ here? |
||
assert updatedConference.getKeywords().contains(newKeyword); | ||
assert updatedConference.getKeywords().size() == originalKeywordsCount + 1; | ||
return updatedConference; | ||
}); | ||
}) | ||
).expectNextCount(1) | ||
.verifyComplete(); | ||
} | ||
|
||
} |
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.
Why is this injecting the Template object? We have already
ElasticsearchOperations
at hand.