From ac6d007c96475e5db0a6045d9d4e59e37308bb44 Mon Sep 17 00:00:00 2001 From: davidrapin Date: Mon, 17 Feb 2020 20:55:00 +0100 Subject: [PATCH] #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(); }