Skip to content
This repository has been archived by the owner on May 6, 2021. It is now read-only.

Fix bug when indexing non-finite numbers #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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 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
Expand Down Expand Up @@ -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<Double> {
@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down