From ac6d007c96475e5db0a6045d9d4e59e37308bb44 Mon Sep 17 00:00:00 2001 From: davidrapin Date: Mon, 17 Feb 2020 20:55:00 +0100 Subject: [PATCH 1/3] #158 fix bug when indexing non-finite numbers --- .../es/util/CustomJestClientFactory.java | 31 +++++++++++++++++++ ...lasticSearchModuleAdvancedMappingTest.java | 2 ++ 2 files changed, 33 insertions(+) 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 5a63463..24bb51d 100644 --- a/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java +++ b/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java @@ -1,9 +1,15 @@ 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; +import java.io.IOException; + /** * Customizations: * - connectionManagerShared: true @@ -12,6 +18,26 @@ */ public class CustomJestClientFactory extends JestClientFactory { + /** + * Serialize non-finite doubles (-Infinity, +Infinity, NaN) as strings. + */ + private 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(); + } + } + @Override protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) { return builder @@ -21,6 +47,11 @@ protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) { @Override public void setHttpClientConfig(HttpClientConfig httpClientConfig) { super.setHttpClientConfig(new HttpClientConfig.Builder(httpClientConfig) + .gson(new GsonBuilder() + .serializeSpecialFloatingPointValues() + .registerTypeAdapter(Double.class, new NonFiniteAsStringAdapter()) + .create() + ) .readTimeout(20000) // 20s .connTimeout(10000) // 10s .build() diff --git a/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java b/src/test/java/com/graphaware/module/es/ElasticSearchModuleAdvancedMappingTest.java index fc86202..108103e 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(); } From 356ba2a0914397bd806f4594f08bf9783df7abeb Mon Sep 17 00:00:00 2001 From: davidrapin Date: Tue, 18 Feb 2020 10:07:16 +0100 Subject: [PATCH 2/3] #158 remove unnecessary "serializeSpecialFloatingPointValues" --- .../com/graphaware/module/es/util/CustomJestClientFactory.java | 1 - 1 file changed, 1 deletion(-) 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 24bb51d..ce4fe51 100644 --- a/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java +++ b/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java @@ -48,7 +48,6 @@ protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) { public void setHttpClientConfig(HttpClientConfig httpClientConfig) { super.setHttpClientConfig(new HttpClientConfig.Builder(httpClientConfig) .gson(new GsonBuilder() - .serializeSpecialFloatingPointValues() .registerTypeAdapter(Double.class, new NonFiniteAsStringAdapter()) .create() ) From 2bd15e3e269915766eccd3c6e62fb309d2844299 Mon Sep 17 00:00:00 2001 From: davidrapin Date: Tue, 18 Feb 2020 10:13:01 +0100 Subject: [PATCH 3/3] #158 moved inner class --- .../es/util/CustomJestClientFactory.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) 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 758b2d8..ffd6323 100644 --- a/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java +++ b/src/main/java/com/graphaware/module/es/util/CustomJestClientFactory.java @@ -32,26 +32,6 @@ public CustomJestClientFactory(int readTimeout, int connectionTimeout) { this.connectionTimeout = connectionTimeout; } - /** - * Serialize non-finite doubles (-Infinity, +Infinity, NaN) as strings. - */ - private 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(); - } - } - @Override protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) { return builder @@ -73,3 +53,23 @@ public void setHttpClientConfig(HttpClientConfig httpClientConfig) { ); } } + +/** + * 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(); + } +}