Do not parse numbers as both strings and numbers when not included in `_all`. #20167

We need to get the string representation of numbers in order to include in
`_all`. However this has a cost and disabling `_all` is rather common so we
should look into skipping it.
This commit is contained in:
Adrien Grand 2016-08-25 18:23:38 +02:00
parent 4460998ff8
commit c5f8e1b64d
2 changed files with 30 additions and 22 deletions

View File

@ -896,6 +896,8 @@ public class NumberFieldMapper extends FieldMapper {
@Override @Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
final boolean includeInAll = context.includeInAll(this.includeInAll, this);
XContentParser parser = context.parser(); XContentParser parser = context.parser();
Object value; Object value;
Number numericValue = null; Number numericValue = null;
@ -908,18 +910,20 @@ public class NumberFieldMapper extends FieldMapper {
&& parser.textLength() == 0) { && parser.textLength() == 0) {
value = null; value = null;
} else { } else {
value = parser.textOrNull(); try {
if (value != null) { numericValue = fieldType().type.parse(parser, coerce.value());
try { } catch (IllegalArgumentException e) {
numericValue = fieldType().type.parse(parser, coerce.value()); if (ignoreMalformed.value()) {
} catch (IllegalArgumentException e) { return;
if (ignoreMalformed.value()) { } else {
return; throw e;
} else {
throw e;
}
} }
} }
if (includeInAll) {
value = parser.textOrNull(); // preserve formatting
} else {
value = numericValue;
}
} }
if (value == null) { if (value == null) {
@ -934,7 +938,7 @@ public class NumberFieldMapper extends FieldMapper {
numericValue = fieldType().type.parse(value); numericValue = fieldType().type.parse(value);
} }
if (context.includeInAll(includeInAll, this)) { if (includeInAll) {
context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost()); context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost());
} }

View File

@ -365,6 +365,8 @@ public class ScaledFloatFieldMapper extends FieldMapper {
@Override @Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
final boolean includeInAll = context.includeInAll(this.includeInAll, this);
XContentParser parser = context.parser(); XContentParser parser = context.parser();
Object value; Object value;
Number numericValue = null; Number numericValue = null;
@ -377,18 +379,20 @@ public class ScaledFloatFieldMapper extends FieldMapper {
&& parser.textLength() == 0) { && parser.textLength() == 0) {
value = null; value = null;
} else { } else {
value = parser.textOrNull(); try {
if (value != null) { numericValue = NumberFieldMapper.NumberType.DOUBLE.parse(parser, coerce.value());
try { } catch (IllegalArgumentException e) {
numericValue = NumberFieldMapper.NumberType.DOUBLE.parse(parser, coerce.value()); if (ignoreMalformed.value()) {
} catch (IllegalArgumentException e) { return;
if (ignoreMalformed.value()) { } else {
return; throw e;
} else {
throw e;
}
} }
} }
if (includeInAll) {
value = parser.textOrNull(); // preserve formatting
} else {
value = numericValue;
}
} }
if (value == null) { if (value == null) {
@ -403,7 +407,7 @@ public class ScaledFloatFieldMapper extends FieldMapper {
numericValue = NumberFieldMapper.NumberType.DOUBLE.parse(value); numericValue = NumberFieldMapper.NumberType.DOUBLE.parse(value);
} }
if (context.includeInAll(includeInAll, this)) { if (includeInAll) {
context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost()); context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost());
} }