Fixed bug that mapper_parsing_exception is thrown for numeric field with ignore_malformed=true when inserting "NaN", "Infinity" or "-Infinity" values (#25967)
This commit is contained in:
parent
2b0f4287b3
commit
0848ffd52e
|
@ -399,9 +399,13 @@ public class ScaledFloatFieldMapper extends FieldMapper {
|
|||
|
||||
double doubleValue = numericValue.doubleValue();
|
||||
if (Double.isFinite(doubleValue) == false) {
|
||||
if (ignoreMalformed.value()) {
|
||||
return;
|
||||
} else {
|
||||
// since we encode to a long, we have no way to carry NaNs and infinities
|
||||
throw new IllegalArgumentException("[scaled_float] only supports finite values, but got [" + doubleValue + "]");
|
||||
}
|
||||
}
|
||||
long scaledValue = Math.round(doubleValue * fieldType().getScalingFactor());
|
||||
|
||||
boolean indexed = fieldType().indexOptions() != IndexOptions.NONE;
|
||||
|
|
|
@ -31,7 +31,9 @@ import org.elasticsearch.test.InternalSettingsPlugin;
|
|||
import org.junit.Before;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
|
@ -223,6 +225,15 @@ public class ScaledFloatFieldMapperTests extends ESSingleNodeTestCase {
|
|||
}
|
||||
|
||||
public void testIgnoreMalformed() throws Exception {
|
||||
doTestIgnoreMalformed("a", "For input string: \"a\"");
|
||||
|
||||
List<String> values = Arrays.asList("NaN", "Infinity", "-Infinity");
|
||||
for (String value : values) {
|
||||
doTestIgnoreMalformed(value, "[scaled_float] only supports finite values, but got [" + value + "]");
|
||||
}
|
||||
}
|
||||
|
||||
private void doTestIgnoreMalformed(String value, String exceptionMessageContains) throws Exception {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("properties").startObject("field").field("type", "scaled_float")
|
||||
.field("scaling_factor", 10.0).endObject().endObject()
|
||||
|
@ -234,12 +245,12 @@ public class ScaledFloatFieldMapperTests extends ESSingleNodeTestCase {
|
|||
|
||||
ThrowingRunnable runnable = () -> mapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("field", "a")
|
||||
.field("field", value)
|
||||
.endObject()
|
||||
.bytes(),
|
||||
XContentType.JSON));
|
||||
MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
|
||||
assertThat(e.getCause().getMessage(), containsString("For input string: \"a\""));
|
||||
assertThat(e.getCause().getMessage(), containsString(exceptionMessageContains));
|
||||
|
||||
mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("properties").startObject("field").field("type", "scaled_float")
|
||||
|
@ -250,7 +261,7 @@ public class ScaledFloatFieldMapperTests extends ESSingleNodeTestCase {
|
|||
|
||||
ParsedDocument doc = mapper2.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("field", "a")
|
||||
.field("field", value)
|
||||
.endObject()
|
||||
.bytes(),
|
||||
XContentType.JSON));
|
||||
|
|
Loading…
Reference in New Issue