Skip to content

Commit d1bef67

Browse files
omniCoder77omniCoder77
authored andcommitted
feat: Add examples for updating documents using ElasticsearchTemplate
Signed-off-by: OmniCoder77 <[email protected]> Signed-off-by: OmniCoder77 <[email protected]>
1 parent f58cc5a commit d1bef67

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919

2020
import java.text.ParseException;
2121
import java.text.SimpleDateFormat;
22+
import java.util.ArrayList;
2223

2324
import org.junit.jupiter.api.Test;
2425
import org.springframework.beans.factory.annotation.Autowired;
2526
import org.springframework.boot.test.context.SpringBootTest;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.data.elasticsearch.client.ClientConfiguration;
2829
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
30+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
2931
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
32+
import org.springframework.data.elasticsearch.core.SearchHit;
3033
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3134
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3235
import org.springframework.data.elasticsearch.core.query.Criteria;
@@ -43,6 +46,7 @@
4346
* @author Artur Konczak
4447
* @author Oliver Gierke
4548
* @author Christoph Strobl
49+
* @author omniCoder77
4650
* @author Prakhar Gupta
4751
* @author Peter-Josef Meisch
4852
*/
@@ -73,6 +77,8 @@ public ClientConfiguration clientConfiguration() {
7377
}
7478

7579
@Autowired ElasticsearchOperations operations;
80+
@Autowired
81+
ElasticsearchTemplate template;
7682

7783
@Test
7884
void textSearch() throws ParseException {
@@ -103,4 +109,29 @@ void geoSpatialSearch() {
103109

104110
assertThat(result).hasSize(2);
105111
}
106-
}
112+
113+
@Test
114+
void shouldUpdateConferenceKeywords() {
115+
var conferenceName = "JDD14 - Cracow";
116+
var newKeyword = "java-ee";
117+
118+
var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName));
119+
SearchHit<Conference> searchHit = template.searchOne(initialQuery, Conference.class);
120+
assertThat(searchHit).isNotNull();
121+
122+
Conference conference = searchHit.getContent();
123+
String conferenceId = searchHit.getId();
124+
125+
int originalKeywordsCount = conference.getKeywords().size();
126+
assertThat(conference.getKeywords()).doesNotContain(newKeyword);
127+
128+
conference.getKeywords().add(newKeyword);
129+
130+
template.save(conference);
131+
132+
Conference updatedConference = template.get(conferenceId, Conference.class);
133+
assertThat(updatedConference).isNotNull();
134+
assertThat(updatedConference.getKeywords()).contains(newKeyword);
135+
assertThat(updatedConference.getKeywords()).hasSize(originalKeywordsCount + 1);
136+
}
137+
}

elasticsearch/reactive/src/test/java/example/springdata/elasticsearch/conference/ReactiveElasticsearchOperationsTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate;
2021
import reactor.test.StepVerifier;
2122

2223
import java.text.ParseException;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Christoph Strobl
4546
* @author Prakhar Gupta
47+
* @author omniCoder77
4648
* @author Peter-Josef Meisch
4749
*/
4850
@SpringBootTest(
@@ -74,6 +76,8 @@ public ClientConfiguration clientConfiguration() {
7476
}
7577

7678
@Autowired ReactiveElasticsearchOperations operations;
79+
@Autowired
80+
ReactiveElasticsearchTemplate template;
7781

7882
@Test
7983
void textSearch() {
@@ -100,4 +104,39 @@ private void verify(SearchHit<Conference> hit, String expectedWord, String expec
100104
fail("o_O", e);
101105
}
102106
}
103-
}
107+
108+
@Test
109+
void shouldUpdateConferenceKeywordsReactive() {
110+
var conferenceName = "JDD14 - Cracow";
111+
var newKeyword = "java-ee";
112+
113+
var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName));
114+
115+
StepVerifier.create(
116+
template
117+
.search(initialQuery, Conference.class)
118+
.next()
119+
.flatMap(searchHit -> {
120+
assert searchHit != null;
121+
Conference conference = searchHit.getContent();
122+
String conferenceId = searchHit.getId();
123+
124+
int originalKeywordsCount = conference.getKeywords().size();
125+
assert !conference.getKeywords().contains(newKeyword);
126+
127+
conference.getKeywords().add(newKeyword);
128+
129+
return template.save(conference)
130+
.then(template.get(conferenceId, Conference.class))
131+
.map(updatedConference -> {
132+
assert updatedConference != null;
133+
assert updatedConference.getKeywords().contains(newKeyword);
134+
assert updatedConference.getKeywords().size() == originalKeywordsCount + 1;
135+
return updatedConference;
136+
});
137+
})
138+
).expectNextCount(1)
139+
.verifyComplete();
140+
}
141+
142+
}

0 commit comments

Comments
 (0)