diff --git a/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java b/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java index d1ea9b7..ffd6323 100644 --- a/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java +++ b/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java @@ -1,5 +1,9 @@ package com.graphaware.module.es.util; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import io.searchbox.client.JestClientFactory; import io.searchbox.client.config.HttpClientConfig; import org.apache.http.impl.client.HttpClientBuilder; @@ -7,6 +11,8 @@ import static com.graphaware.module.es.ElasticSearchConfiguration.DEFAULT_CONNECTION_TIMEOUT; import static com.graphaware.module.es.ElasticSearchConfiguration.DEFAULT_READ_TIMEOUT; +import java.io.IOException; + /** * Customizations: * - connectionManagerShared: true @@ -37,7 +43,33 @@ public void setHttpClientConfig(HttpClientConfig httpClientConfig) { super.setHttpClientConfig(new HttpClientConfig.Builder(httpClientConfig) .readTimeout(readTimeout > 0 ? readTimeout : DEFAULT_READ_TIMEOUT) .connTimeout(connectionTimeout > 0 ? connectionTimeout : DEFAULT_CONNECTION_TIMEOUT) + .gson(new GsonBuilder() + .registerTypeAdapter(Double.class, new NonFiniteAsStringAdapter()) + .create() + ) + .readTimeout(20000) // 20s + .connTimeout(10000) // 10s .build() ); } } + +/** + * Serialize non-finite doubles (-Infinity, +Infinity, NaN) as strings. + */ +class NonFiniteAsStringAdapter extends TypeAdapter { + @Override + public void write(JsonWriter out, Double value) throws IOException { + if (Double.isFinite(value)) { + out.value(value); + } else { + // serialize as "+Infinity", "-Infinity" and "NaN" (string) + out.value(value + ""); + } + } + + @Override + public Double read(JsonReader in) throws IOException { + return in.nextDouble(); + } +} diff --git a/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java b/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java index 0bebf06..5cee917 100644 --- a/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java +++ b/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java @@ -58,6 +58,8 @@ protected void writeSomeStuffWithListToNeo4j() { node.addLabel(Label.label("LabelWithList2")); int[] listOfInteger = {1, 2, 3}; node.setProperty("listOfInteger", listOfInteger); + node.setProperty("infinite-number", Double.NEGATIVE_INFINITY); + node.setProperty("nan-number", Double.NaN); tx.success(); }