Skip to content

Commit

Permalink
fix for issue #102
Browse files Browse the repository at this point in the history
  • Loading branch information
Deya Eddin authored and VanRoy committed Feb 14, 2019
1 parent cdd1c9e commit a6c4e23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
<maven.compiler.target>1.8</maven.compiler.target>

<!-- Dependencies version -->
<springboot>2.1.0.RELEASE</springboot>
<spring>5.1.2.RELEASE</spring>
<springboot>2.1.2.RELEASE</springboot>
<spring>5.1.4.RELEASE</spring>
<springdataes>3.1.2.RELEASE</springdataes>
<springdatacommons>2.1.2.RELEASE</springdatacommons>
<springdatacommons>2.1.4.RELEASE</springdatacommons>
<jest>6.3.1</jest>
<gson>2.8.0</gson>
<awssigning>0.0.21</awssigning>
Expand All @@ -64,7 +64,7 @@
<aws>1.11.132</aws>
<log4j>2.7</log4j>

<elasticsearch>6.2.2</elasticsearch>
<elasticsearch>6.4.3</elasticsearch>

<!-- Version of maven plugins -->
<version.plugin.maven-compiler-plugin>3.6.1</version.plugin.maven-compiler-plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -201,6 +202,12 @@ public <T> boolean createIndex(Class<T> 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 <T> boolean putMapping(Class<T> clazz) {
if (clazz.isAnnotationPresent(Mapping.class)) {
Expand All @@ -223,12 +230,12 @@ public <T> boolean putMapping(Class<T> 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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6c4e23

Please sign in to comment.