mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 15:22:11 +00:00
DATAES-238 - Fix ElasticsearchTemplate.prepareUpdate(): preserve routing
* need to specify Locale.ENGLISH in String.format() to make tests succeed on non-english locales * that's because the String.format involves floating point numbers which are formatted differently in some locales * ElasticsearchTemplate.prepareUpdate() properly copies routing from given UpdateRequest * add unit tests to ElasticsearchTemplateParentChildTests for update of a child document
This commit is contained in:
parent
907571e550
commit
8bb4ac3653
@ -538,6 +538,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
Assert.notNull(query.getId(), "No Id define for Query");
|
Assert.notNull(query.getId(), "No Id define for Query");
|
||||||
Assert.notNull(query.getUpdateRequest(), "No IndexRequest define for Query");
|
Assert.notNull(query.getUpdateRequest(), "No IndexRequest define for Query");
|
||||||
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(indexName, type, query.getId());
|
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(indexName, type, query.getId());
|
||||||
|
updateRequestBuilder.setRouting(query.getUpdateRequest().routing());
|
||||||
|
|
||||||
if (query.getUpdateRequest().script() == null) {
|
if (query.getUpdateRequest().script() == null) {
|
||||||
// doc
|
// doc
|
||||||
|
@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -82,11 +83,11 @@ public class DefaultEntityMapperTests {
|
|||||||
//then
|
//then
|
||||||
assertThat(jsonResult, containsString(pointTemplate("pointA", point)));
|
assertThat(jsonResult, containsString(pointTemplate("pointA", point)));
|
||||||
assertThat(jsonResult, containsString(pointTemplate("pointB", point)));
|
assertThat(jsonResult, containsString(pointTemplate("pointB", point)));
|
||||||
assertThat(jsonResult, containsString(String.format("\"%s\":\"%s\"", "pointC", pointAsString)));
|
assertThat(jsonResult, containsString(String.format(Locale.ENGLISH, "\"%s\":\"%s\"", "pointC", pointAsString)));
|
||||||
assertThat(jsonResult, containsString(String.format("\"%s\":[%.1f,%.1f]", "pointD", pointAsArray[0], pointAsArray[1])));
|
assertThat(jsonResult, containsString(String.format(Locale.ENGLISH, "\"%s\":[%.1f,%.1f]", "pointD", pointAsArray[0], pointAsArray[1])));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String pointTemplate(String name, Point point) {
|
private String pointTemplate(String name, Point point) {
|
||||||
return String.format("\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
|
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.RoutingMissingException;
|
||||||
|
import org.elasticsearch.action.update.UpdateRequest;
|
||||||
|
import org.elasticsearch.action.update.UpdateResponse;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -30,6 +35,7 @@ import org.junit.runner.RunWith;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
|
||||||
import org.springframework.data.elasticsearch.entities.ParentEntity;
|
import org.springframework.data.elasticsearch.entities.ParentEntity;
|
||||||
import org.springframework.data.elasticsearch.entities.ParentEntity.ChildEntity;
|
import org.springframework.data.elasticsearch.entities.ParentEntity.ChildEntity;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
@ -81,6 +87,55 @@ public class ElasticsearchTemplateParentChildTests {
|
|||||||
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUpdateChild() throws Exception {
|
||||||
|
// index parent and child
|
||||||
|
ParentEntity parent = index("parent", "Parent");
|
||||||
|
ChildEntity child = index("child", parent.getId(), "Child");
|
||||||
|
String newChildName = "New Child Name";
|
||||||
|
|
||||||
|
// update the child, not forgetting to set the parent id as routing parameter
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest(ParentEntity.INDEX, ParentEntity.CHILD_TYPE, child.getId());
|
||||||
|
updateRequest.routing(parent.getId());
|
||||||
|
XContentBuilder builder;
|
||||||
|
builder = jsonBuilder().startObject().field("name", newChildName).endObject();
|
||||||
|
updateRequest.doc(builder);
|
||||||
|
final UpdateResponse response = update(updateRequest);
|
||||||
|
|
||||||
|
assertThat(response.getShardInfo().getSuccessful(), is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RoutingMissingException.class)
|
||||||
|
public void shouldFailWithRoutingMissingExceptionOnUpdateChildIfNotRoutingSetOnUpdateRequest() throws Exception {
|
||||||
|
// index parent and child
|
||||||
|
ParentEntity parent = index("parent", "Parent");
|
||||||
|
ChildEntity child = index("child", parent.getId(), "Child");
|
||||||
|
String newChildName = "New Child Name";
|
||||||
|
|
||||||
|
// update the child, forget routing parameter
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest(ParentEntity.INDEX, ParentEntity.CHILD_TYPE, child.getId());
|
||||||
|
XContentBuilder builder;
|
||||||
|
builder = jsonBuilder().startObject().field("name", newChildName).endObject();
|
||||||
|
updateRequest.doc(builder);
|
||||||
|
update(updateRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RoutingMissingException.class)
|
||||||
|
public void shouldFailWithRoutingMissingExceptionOnUpdateChildIfRoutingOnlySetOnRequestDoc() throws Exception {
|
||||||
|
// index parent and child
|
||||||
|
ParentEntity parent = index("parent", "Parent");
|
||||||
|
ChildEntity child = index("child", parent.getId(), "Child");
|
||||||
|
String newChildName = "New Child Name";
|
||||||
|
|
||||||
|
// update the child
|
||||||
|
UpdateRequest updateRequest = new UpdateRequest(ParentEntity.INDEX, ParentEntity.CHILD_TYPE, child.getId());
|
||||||
|
XContentBuilder builder;
|
||||||
|
builder = jsonBuilder().startObject().field("name", newChildName).endObject();
|
||||||
|
updateRequest.doc(builder);
|
||||||
|
updateRequest.doc().routing(parent.getId());
|
||||||
|
update(updateRequest);
|
||||||
|
}
|
||||||
|
|
||||||
private ParentEntity index(String parentId, String name) {
|
private ParentEntity index(String parentId, String name) {
|
||||||
ParentEntity parent = new ParentEntity(parentId, name);
|
ParentEntity parent = new ParentEntity(parentId, name);
|
||||||
IndexQuery index = new IndexQuery();
|
IndexQuery index = new IndexQuery();
|
||||||
@ -101,4 +156,13 @@ public class ElasticsearchTemplateParentChildTests {
|
|||||||
|
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UpdateResponse update(UpdateRequest updateRequest) {
|
||||||
|
final UpdateQuery update = new UpdateQuery();
|
||||||
|
update.setId(updateRequest.id());
|
||||||
|
update.setType(updateRequest.type());
|
||||||
|
update.setIndexName(updateRequest.index());
|
||||||
|
update.setUpdateRequest(updateRequest);
|
||||||
|
return elasticsearchTemplate.update(update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,8 @@ import org.springframework.data.geo.Point;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/repository-spring-data-geo-support.xml")
|
@ContextConfiguration("classpath:/repository-spring-data-geo-support.xml")
|
||||||
@ -72,7 +74,7 @@ public class SpringDataGeoRepositoryTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String toGeoString(Point point) {
|
private String toGeoString(Point point) {
|
||||||
return String.format("%.1f,%.1f", point.getX(), point.getY());
|
return String.format(Locale.ENGLISH, "%.1f,%.1f", point.getX(), point.getY());
|
||||||
}
|
}
|
||||||
|
|
||||||
private double[] toGeoArray(Point point) {
|
private double[] toGeoArray(Point point) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user