Skip to content

Commit 096c2e1

Browse files
committed
DATAES-16 : Add Partial Update / Upsert support
1 parent 2d6750a commit 096c2e1

File tree

5 files changed

+245
-1
lines changed

5 files changed

+245
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import org.elasticsearch.action.update.UpdateResponse;
1819
import org.springframework.data.domain.Page;
1920
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2021
import org.springframework.data.elasticsearch.core.query.*;
@@ -158,6 +159,14 @@ public interface ElasticsearchOperations {
158159
*/
159160
String index(IndexQuery query);
160161

162+
/**
163+
* Partial update of the document
164+
*
165+
* @param updateQuery
166+
* @return
167+
*/
168+
UpdateResponse update(UpdateQuery updateQuery);
169+
161170
/**
162171
* Bulk index all objects. Will do save or update
163172
*

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
3030
import org.elasticsearch.action.search.SearchRequestBuilder;
3131
import org.elasticsearch.action.search.SearchResponse;
32+
import org.elasticsearch.action.update.UpdateRequestBuilder;
33+
import org.elasticsearch.action.update.UpdateResponse;
3234
import org.elasticsearch.client.Client;
3335
import org.elasticsearch.client.Requests;
3436
import org.elasticsearch.common.collect.MapBuilder;
@@ -99,7 +101,6 @@ public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearch
99101

100102
@Override
101103
public <T> boolean createIndex(Class<T> clazz) {
102-
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
103104
return createIndexIfNotCreated(clazz);
104105
}
105106

@@ -207,6 +208,25 @@ public String index(IndexQuery query) {
207208
return prepareIndex(query).execute().actionGet().getId();
208209
}
209210

211+
@Override
212+
public UpdateResponse update(UpdateQuery query) {
213+
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName();
214+
String type = isNotBlank(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType();
215+
Assert.notNull(indexName, "No index defined for Query");
216+
Assert.notNull(type, "No type define for Query");
217+
Assert.notNull(query.getId(), "No Id define for Query");
218+
Assert.notNull(query.getIndexRequest(), "No IndexRequest define for Query");
219+
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(indexName, type, query.getId());
220+
if(query.DoUpsert()){
221+
updateRequestBuilder.setDocAsUpsert(true)
222+
.setUpsertRequest(query.getIndexRequest()).setDoc(query.getIndexRequest());
223+
} else {
224+
updateRequestBuilder.setDoc(query.getIndexRequest());
225+
}
226+
return updateRequestBuilder.execute().actionGet();
227+
}
228+
229+
210230
@Override
211231
public void bulkIndex(List<IndexQuery> queries) {
212232
BulkRequestBuilder bulkRequest = client.prepareBulk();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.query;
17+
18+
import org.elasticsearch.action.index.IndexRequest;
19+
20+
/**
21+
* @author Rizwan Idrees
22+
* @author Mohsin Husen
23+
*/
24+
public class UpdateQuery {
25+
26+
private String id;
27+
private IndexRequest indexRequest;
28+
private String indexName;
29+
private String type;
30+
private Class clazz;
31+
private boolean doUpsert;
32+
33+
public String getId() {
34+
return id;
35+
}
36+
37+
public void setId(String id) {
38+
this.id = id;
39+
}
40+
41+
public IndexRequest getIndexRequest() {
42+
return indexRequest;
43+
}
44+
45+
public void setIndexRequest(IndexRequest indexRequest) {
46+
this.indexRequest = indexRequest;
47+
}
48+
49+
public String getIndexName() {
50+
return indexName;
51+
}
52+
53+
public void setIndexName(String indexName) {
54+
this.indexName = indexName;
55+
}
56+
57+
public String getType() {
58+
return type;
59+
}
60+
61+
public void setType(String type) {
62+
this.type = type;
63+
}
64+
65+
public Class getClazz() {
66+
return clazz;
67+
}
68+
69+
public void setClazz(Class clazz) {
70+
this.clazz = clazz;
71+
}
72+
73+
public boolean DoUpsert() {
74+
return doUpsert;
75+
}
76+
77+
public void setDoUpsert(boolean doUpsert) {
78+
this.doUpsert = doUpsert;
79+
}
80+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.query;
17+
18+
import org.elasticsearch.action.index.IndexRequest;
19+
20+
/**
21+
* @author Rizwan Idrees
22+
* @author Mohsin Husen
23+
*/
24+
public class UpdateQueryBuilder {
25+
26+
private String id;
27+
private IndexRequest indexRequest;
28+
private String indexName;
29+
private String type;
30+
private Class clazz;
31+
private boolean doUpsert;
32+
33+
public UpdateQueryBuilder withId(String id){
34+
this.id = id;
35+
return this;
36+
}
37+
38+
public UpdateQueryBuilder withIndexRequest(IndexRequest indexRequest){
39+
this.indexRequest = indexRequest;
40+
return this;
41+
}
42+
43+
public UpdateQueryBuilder withIndexName(String indexName){
44+
this.indexName = indexName;
45+
return this;
46+
}
47+
48+
public UpdateQueryBuilder withType(String type){
49+
this.type = type;
50+
return this;
51+
}
52+
53+
public UpdateQueryBuilder withClass(Class clazz){
54+
this.clazz = clazz;
55+
return this;
56+
}
57+
58+
public UpdateQueryBuilder withDoUpsert(boolean doUpsert){
59+
this.doUpsert = doUpsert;
60+
return this;
61+
}
62+
63+
public UpdateQuery build(){
64+
UpdateQuery updateQuery = new UpdateQuery();
65+
updateQuery.setId(id);
66+
updateQuery.setIndexName(indexName);
67+
updateQuery.setType(type);
68+
updateQuery.setClazz(clazz);
69+
updateQuery.setIndexRequest(indexRequest);
70+
updateQuery.setDoUpsert(doUpsert);
71+
return updateQuery;
72+
}
73+
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import org.elasticsearch.action.index.IndexRequest;
1819
import org.elasticsearch.action.search.SearchResponse;
20+
import org.elasticsearch.index.engine.DocumentMissingException;
1921
import org.elasticsearch.search.SearchHit;
2022
import org.elasticsearch.search.sort.FieldSortBuilder;
2123
import org.elasticsearch.search.sort.SortOrder;
@@ -716,4 +718,64 @@ public void shouldDeleteIndexForGivenEntity() {
716718
assertThat(elasticsearchTemplate.indexExists(clazz), is(false));
717719
}
718720

721+
@Test
722+
public void shouldDoPartialUpdateForExistingDocument() {
723+
//given
724+
String documentId = randomNumeric(5);
725+
String messageBeforeUpdate = "some test message";
726+
String messageAfterUpdate = "test message";
727+
728+
SampleEntity sampleEntity = new SampleEntity();
729+
sampleEntity.setId(documentId);
730+
sampleEntity.setMessage(messageBeforeUpdate);
731+
sampleEntity.setVersion(System.currentTimeMillis());
732+
733+
IndexQuery indexQuery = new IndexQuery();
734+
indexQuery.setId(documentId);
735+
indexQuery.setObject(sampleEntity);
736+
737+
elasticsearchTemplate.index(indexQuery);
738+
elasticsearchTemplate.refresh(SampleEntity.class, true);
739+
740+
IndexRequest indexRequest = new IndexRequest();
741+
indexRequest.source("message", messageAfterUpdate);
742+
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(documentId)
743+
.withClass(SampleEntity.class).withIndexRequest(indexRequest).build();
744+
// when
745+
elasticsearchTemplate.update(updateQuery);
746+
//then
747+
GetQuery getQuery = new GetQuery();
748+
getQuery.setId(documentId);
749+
SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
750+
assertThat(indexedEntity.getMessage(), is(messageAfterUpdate));
751+
}
752+
753+
@Test(expected = DocumentMissingException.class)
754+
public void shouldThrowExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate(){
755+
// when
756+
IndexRequest indexRequest = new IndexRequest();
757+
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(randomNumeric(5))
758+
.withClass(SampleEntity.class).withIndexRequest(indexRequest).build();
759+
elasticsearchTemplate.update(updateQuery);
760+
}
761+
762+
@Test
763+
public void shouldDoUpsertIfDocumentDoesNotExist(){
764+
//given
765+
String documentId = randomNumeric(5);
766+
String message = "test message";
767+
IndexRequest indexRequest = new IndexRequest();
768+
indexRequest.source("message", message);
769+
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(documentId)
770+
.withDoUpsert(true).withClass(SampleEntity.class)
771+
.withIndexRequest(indexRequest).build();
772+
//when
773+
elasticsearchTemplate.update(updateQuery);
774+
//then
775+
GetQuery getQuery = new GetQuery();
776+
getQuery.setId(documentId);
777+
SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
778+
assertThat(indexedEntity.getMessage(), is(message));
779+
}
780+
719781
}

0 commit comments

Comments
 (0)