diff --git a/pom.xml b/pom.xml index 27414866..af7d9122 100644 --- a/pom.xml +++ b/pom.xml @@ -47,10 +47,10 @@ 1.8 - 2.1.0.RELEASE - 5.1.2.RELEASE + 2.1.2.RELEASE + 5.1.4.RELEASE 3.1.2.RELEASE - 2.1.2.RELEASE + 2.1.4.RELEASE 6.3.1 2.8.0 0.0.21 @@ -64,7 +64,7 @@ 1.11.132 2.7 - 6.2.2 + 6.4.3 3.6.1 diff --git a/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java b/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java index 3312d6a2..8d3ebc4e 100644 --- a/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java +++ b/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java @@ -70,6 +70,7 @@ import org.springframework.util.Assert; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; @@ -201,6 +202,12 @@ public boolean createIndex(Class clazz, Object settings) { return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings); } + private String xContentBuilderToString(XContentBuilder builder) { + builder.close(); + ByteArrayOutputStream bos = (ByteArrayOutputStream) builder.getOutputStream(); + return bos.toString(); + } + @Override public boolean putMapping(Class clazz) { if (clazz.isAnnotationPresent(Mapping.class)) { @@ -223,12 +230,12 @@ public boolean putMapping(Class clazz) { throw new IllegalArgumentException(String.format("No Id property for %s found", clazz.getSimpleName())); } - mapping = buildMapping( + mapping =xContentBuilderToString( buildMapping( clazz, persistentEntity.getIndexType(), idProperty.getFieldName(), persistentEntity.getParentType() - ).string(); + )); } catch (Exception e) { throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e); @@ -249,9 +256,9 @@ public boolean putMapping(String indexName, String type, Object mapping) { } else if (mapping instanceof Map) { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.map((Map) mapping); - source = builder.string(); + source = xContentBuilderToString( builder ); } else if (mapping instanceof XContentBuilder) { - source = ((XContentBuilder) mapping).string(); + source = xContentBuilderToString(((XContentBuilder) mapping)); } else if (mapping instanceof DocumentMapper) { source = ((DocumentMapper) mapping).mappingSource().toString(); } diff --git a/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java b/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java index 44ce842a..b439fe07 100755 --- a/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java +++ b/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java @@ -593,7 +593,7 @@ public void shouldReturnPageableResultsGivenStringQuery() { String documentId = String.valueOf(ThreadLocalRandom.current().nextLong()).substring(1, 6); SampleEntity sampleEntity1 = SampleEntity.builder().id(documentId).message("some message 1") .version(System.currentTimeMillis()).build(); - + documentId = String.valueOf(ThreadLocalRandom.current().nextLong()).substring(1, 6); SampleEntity sampleEntity2 = SampleEntity.builder().id(documentId).message("some message 2") .version(System.currentTimeMillis()).build(); @@ -1240,6 +1240,8 @@ public void shouldPutXContentBuilderMappingForGivenEntity() throws Exception { // when assertThat(elasticsearchTemplate.putMapping("test-custom-mapping", "mapping", xContentBuilder), is(true)); + //then + elasticsearchTemplate.deleteIndex("test-custom-mapping"); } @Test