15
15
*/
16
16
package org .springframework .data .elasticsearch .core ;
17
17
18
+ import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
18
19
import static org .elasticsearch .index .query .QueryBuilders .*;
19
20
import static org .hamcrest .Matchers .*;
20
21
import static org .junit .Assert .*;
21
22
22
23
import java .util .List ;
23
24
25
+ import org .elasticsearch .action .RoutingMissingException ;
26
+ import org .elasticsearch .action .update .UpdateRequest ;
27
+ import org .elasticsearch .action .update .UpdateResponse ;
28
+ import org .elasticsearch .common .xcontent .XContentBuilder ;
24
29
import org .elasticsearch .index .query .QueryBuilder ;
25
30
import org .elasticsearch .index .query .QueryBuilders ;
26
31
import org .junit .After ;
30
35
import org .springframework .beans .factory .annotation .Autowired ;
31
36
import org .springframework .data .elasticsearch .core .query .IndexQuery ;
32
37
import org .springframework .data .elasticsearch .core .query .NativeSearchQuery ;
38
+ import org .springframework .data .elasticsearch .core .query .UpdateQuery ;
33
39
import org .springframework .data .elasticsearch .entities .ParentEntity ;
34
40
import org .springframework .data .elasticsearch .entities .ParentEntity .ChildEntity ;
35
41
import org .springframework .test .context .ContextConfiguration ;
@@ -81,6 +87,55 @@ public void shouldIndexParentChildEntity() {
81
87
assertThat ("parents" , parents , contains (hasProperty ("id" , is (parent1 .getId ()))));
82
88
}
83
89
90
+ @ Test
91
+ public void shouldUpdateChild () throws Exception {
92
+ // index parent and child
93
+ ParentEntity parent = index ("parent" , "Parent" );
94
+ ChildEntity child = index ("child" , parent .getId (), "Child" );
95
+ String newChildName = "New Child Name" ;
96
+
97
+ // update the child, not forgetting to set the parent id as routing parameter
98
+ UpdateRequest updateRequest = new UpdateRequest (ParentEntity .INDEX , ParentEntity .CHILD_TYPE , child .getId ());
99
+ updateRequest .routing (parent .getId ());
100
+ XContentBuilder builder ;
101
+ builder = jsonBuilder ().startObject ().field ("name" , newChildName ).endObject ();
102
+ updateRequest .doc (builder );
103
+ final UpdateResponse response = update (updateRequest );
104
+
105
+ assertThat (response .getShardInfo ().getSuccessful (), is (1 ));
106
+ }
107
+
108
+ @ Test (expected = RoutingMissingException .class )
109
+ public void shouldFailWithRoutingMissingExceptionOnUpdateChildIfNotRoutingSetOnUpdateRequest () throws Exception {
110
+ // index parent and child
111
+ ParentEntity parent = index ("parent" , "Parent" );
112
+ ChildEntity child = index ("child" , parent .getId (), "Child" );
113
+ String newChildName = "New Child Name" ;
114
+
115
+ // update the child, forget routing parameter
116
+ UpdateRequest updateRequest = new UpdateRequest (ParentEntity .INDEX , ParentEntity .CHILD_TYPE , child .getId ());
117
+ XContentBuilder builder ;
118
+ builder = jsonBuilder ().startObject ().field ("name" , newChildName ).endObject ();
119
+ updateRequest .doc (builder );
120
+ update (updateRequest );
121
+ }
122
+
123
+ @ Test (expected = RoutingMissingException .class )
124
+ public void shouldFailWithRoutingMissingExceptionOnUpdateChildIfRoutingOnlySetOnRequestDoc () throws Exception {
125
+ // index parent and child
126
+ ParentEntity parent = index ("parent" , "Parent" );
127
+ ChildEntity child = index ("child" , parent .getId (), "Child" );
128
+ String newChildName = "New Child Name" ;
129
+
130
+ // update the child
131
+ UpdateRequest updateRequest = new UpdateRequest (ParentEntity .INDEX , ParentEntity .CHILD_TYPE , child .getId ());
132
+ XContentBuilder builder ;
133
+ builder = jsonBuilder ().startObject ().field ("name" , newChildName ).endObject ();
134
+ updateRequest .doc (builder );
135
+ updateRequest .doc ().routing (parent .getId ());
136
+ update (updateRequest );
137
+ }
138
+
84
139
private ParentEntity index (String parentId , String name ) {
85
140
ParentEntity parent = new ParentEntity (parentId , name );
86
141
IndexQuery index = new IndexQuery ();
@@ -101,4 +156,13 @@ private ChildEntity index(String childId, String parentId, String name) {
101
156
102
157
return child ;
103
158
}
159
+
160
+ private UpdateResponse update (UpdateRequest updateRequest ) {
161
+ final UpdateQuery update = new UpdateQuery ();
162
+ update .setId (updateRequest .id ());
163
+ update .setType (updateRequest .type ());
164
+ update .setIndexName (updateRequest .index ());
165
+ update .setUpdateRequest (updateRequest );
166
+ return elasticsearchTemplate .update (update );
167
+ }
104
168
}
0 commit comments