Skip to content

Commit 79d75f8

Browse files
MassimilianoPoggisothawo
authored andcommitted
DATAES-693 - Support for source fetching in update operations.
Original PR: #346
1 parent eba23ee commit 79d75f8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
* @author Peter-Josef Meisch
146146
* @author Mathias Teier
147147
* @author Gyula Attila Csorogi
148+
* @author Massimiliano Poggi
148149
*/
149150
public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate
150151
implements EsClient<RestHighLevelClient>, ApplicationContextAware {
@@ -685,7 +686,8 @@ private UpdateRequest prepareUpdate(UpdateQuery query) {
685686
.setRefreshPolicy(queryUpdateRequest.getRefreshPolicy()) //
686687
.waitForActiveShards(queryUpdateRequest.waitForActiveShards()) //
687688
.scriptedUpsert(queryUpdateRequest.scriptedUpsert()) //
688-
.docAsUpsert(queryUpdateRequest.docAsUpsert());
689+
.docAsUpsert(queryUpdateRequest.docAsUpsert()) //
690+
.fetchSource(queryUpdateRequest.fetchSource());
689691

690692
if (query.DoUpsert()) {
691693
updateRequest.docAsUpsert(true);

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplateTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.action.index.IndexRequest;
3232
import org.elasticsearch.action.update.UpdateRequest;
3333
import org.elasticsearch.common.xcontent.XContentType;
34+
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
3435
import org.junit.jupiter.api.Test;
3536
import org.springframework.data.annotation.Id;
3637
import org.springframework.data.elasticsearch.annotations.Document;
@@ -55,6 +56,7 @@
5556
* @author Sascha Woo
5657
* @author Don Wellington
5758
* @author Peter-Josef Meisch
59+
* @author Massimiliano Poggi
5860
*/
5961
@SpringIntegrationTest
6062
@ContextConfiguration(classes = { ElasticsearchRestTemplateConfiguration.class })
@@ -100,6 +102,31 @@ public void shouldUseUpsertOnUpdate() throws IOException {
100102
assertThat(request.upsertRequest()).isNotNull();
101103
}
102104

105+
@Test // DATAES-693
106+
public void shouldReturnSourceWhenRequested() throws IOException {
107+
// given
108+
Map<String, Object> doc = new HashMap<>();
109+
doc.put("id", "1");
110+
doc.put("message", "test");
111+
112+
UpdateRequest updateRequest = new UpdateRequest()
113+
.doc(doc)
114+
.fetchSource(FetchSourceContext.FETCH_SOURCE);
115+
116+
UpdateQuery updateQuery = new UpdateQueryBuilder() //
117+
.withClass(SampleEntity.class) //
118+
.withId("1") //
119+
.withUpdateRequest(updateRequest).build();
120+
121+
// when
122+
UpdateRequest request = (UpdateRequest) ReflectionTestUtils //
123+
.invokeMethod(elasticsearchTemplate, "prepareUpdate", updateQuery);
124+
125+
// then
126+
assertThat(request).isNotNull();
127+
assertThat(request.fetchSource()).isEqualTo(FetchSourceContext.FETCH_SOURCE);
128+
}
129+
103130
@Data
104131
@Builder
105132
@Document(indexName = "test-index-sample-core-rest-template", type = "test-type", shards = 1, replicas = 0,

0 commit comments

Comments
 (0)