Remove MappedFieldType.value. #17557

This commit removes `MappedFieldType.value` and simplifies
`MappedFieldType.valueforSearch`. `valueforSearch` was used to post-process
values that come for stored fields (eg. to convert a long back to a string
representation of a date in the case of a date field) and also values that
are extracted from the source but only in the case of GET calls: it would
not be called when performing source filtering on search requests.

`valueforSearch` is now only called for stored fields, since values that are
extracted from the source should already be formatted as expected.
This commit is contained in:
Adrien Grand 2016-04-06 11:55:35 +02:00
parent 7ad804fa52
commit 013acf9179
41 changed files with 132 additions and 332 deletions

View File

@ -250,9 +250,6 @@ public final class ShardGetService extends AbstractIndexShardComponent {
} else if (shouldGetFromSource(ignoreErrorsOnGeneratedFields, docMapper, fieldMapper)) {
List<Object> values = searchLookup.source().extractRawValues(field);
if (!values.isEmpty()) {
for (int i = 0; i < values.size(); i++) {
values.set(i, fieldMapper.fieldType().valueForSearch(values.get(i)));
}
value = values;
}

View File

@ -302,12 +302,9 @@ public abstract class MappedFieldType extends FieldType {
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
}
/** Returns the actual value of the field. */
public Object value(Object value) {
return value;
}
/** Returns the value that will be used as a result for search. Can be only of specific types... */
/** Given a value that comes from the stored fields API, convert it to the
* expected type. For instance a date field would store dates as longs and
* format it back to a string in this method. */
public Object valueForSearch(Object value) {
return value;
}

View File

@ -107,7 +107,7 @@ public class BinaryFieldMapper extends FieldMapper {
@Override
public BytesReference value(Object value) {
public BytesReference valueForSearch(Object value) {
if (value == null) {
return null;
}
@ -129,11 +129,6 @@ public class BinaryFieldMapper extends FieldMapper {
return bytes;
}
@Override
public Object valueForSearch(Object value) {
return value(value);
}
@Override
public IndexFieldData.Builder fielddataBuilder() {
failIfNoDocValues();

View File

@ -169,26 +169,18 @@ public class BooleanFieldMapper extends FieldMapper {
}
@Override
public Boolean value(Object value) {
public Boolean valueForSearch(Object value) {
if (value == null) {
return Boolean.FALSE;
return null;
}
String sValue = value.toString();
if (sValue.length() == 0) {
return Boolean.FALSE;
switch(value.toString()) {
case "F":
return false;
case "T":
return true;
default:
throw new IllegalArgumentException("Expected [T] or [F] but got [" + value + "]");
}
if (sValue.length() == 1 && sValue.charAt(0) == 'F') {
return Boolean.FALSE;
}
if (Booleans.parseBoolean(sValue, false)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
@Override
public Object valueForSearch(Object value) {
return value(value);
}
@Override

View File

@ -143,17 +143,11 @@ public class ByteFieldMapper extends NumberFieldMapper {
}
@Override
public Byte value(Object value) {
public Byte valueForSearch(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).byteValue();
}
if (value instanceof BytesRef) {
return ((BytesRef) value).bytes[((BytesRef) value).offset];
}
return Byte.parseByte(value.toString());
return ((Number) value).byteValue();
}
@Override

View File

@ -336,14 +336,6 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp
}
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
}
/**

View File

@ -344,20 +344,6 @@ public class DateFieldMapper extends NumberFieldMapper {
return dateTimeFormatter().parser().parseMillis(value);
}
@Override
public Long value(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).longValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToLong((BytesRef) value);
}
return parseStringValue(value.toString());
}
@Override
public BytesRef indexedValueForSearch(Object value) {
BytesRefBuilder bytesRef = new BytesRefBuilder();
@ -367,11 +353,7 @@ public class DateFieldMapper extends NumberFieldMapper {
@Override
public Object valueForSearch(Object value) {
if (value instanceof String) {
// assume its the string that was indexed, just return it... (for example, with get)
return value;
}
Long val = value(value);
Long val = (Long) value;
if (val == null) {
return null;
}

View File

@ -145,7 +145,7 @@ public class DoubleFieldMapper extends NumberFieldMapper {
}
@Override
public Double value(Object value) {
public Double valueForSearch(Object value) {
if (value == null) {
return null;
}

View File

@ -145,20 +145,6 @@ public class FloatFieldMapper extends NumberFieldMapper {
return (Float)super.nullValue();
}
@Override
public Float value(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToFloat((BytesRef) value);
}
return Float.parseFloat(value.toString());
}
@Override
public BytesRef indexedValueForSearch(Object value) {
int intValue = NumericUtils.floatToSortableInt(parseValue(value));

View File

@ -151,20 +151,6 @@ public class IntegerFieldMapper extends NumberFieldMapper {
return (Integer)super.nullValue();
}
@Override
public Integer value(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).intValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToInt((BytesRef) value);
}
return Integer.parseInt(value.toString());
}
@Override
public BytesRef indexedValueForSearch(Object value) {
BytesRefBuilder bytesRef = new BytesRefBuilder();

View File

@ -157,14 +157,6 @@ public final class KeywordFieldMapper extends FieldMapper implements AllFieldMap
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query nullValueQuery() {
if (nullValue() == null) {

View File

@ -149,20 +149,6 @@ public class LongFieldMapper extends NumberFieldMapper {
return (Long)super.nullValue();
}
@Override
public Long value(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).longValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToLong((BytesRef) value);
}
return Long.parseLong(value.toString());
}
@Override
public BytesRef indexedValueForSearch(Object value) {
BytesRefBuilder bytesRef = new BytesRefBuilder();

View File

@ -161,14 +161,6 @@ public abstract class NumberFieldMapper extends FieldMapper implements AllFieldM
public abstract NumberFieldType clone();
@Override
public abstract Object value(Object value);
@Override
public Object valueForSearch(Object value) {
return value(value);
}
@Override
public abstract Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions);

View File

@ -33,7 +33,6 @@ import org.apache.lucene.util.LegacyNumericUtils;
import org.elasticsearch.Version;
import org.elasticsearch.action.fieldstats.FieldStats;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.Fuzziness;
@ -148,17 +147,11 @@ public class ShortFieldMapper extends NumberFieldMapper {
}
@Override
public Short value(Object value) {
public Short valueForSearch(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).shortValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToShort((BytesRef) value);
}
return Short.parseShort(value.toString());
return ((Number) value).shortValue();
}
@Override

View File

@ -450,14 +450,6 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
this.fielddataMinSegmentSize = fielddataMinSegmentSize;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query nullValueQuery() {
if (nullValue() == null) {

View File

@ -278,14 +278,6 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query nullValueQuery() {
if (nullValue() == null) {

View File

@ -412,11 +412,6 @@ public class GeoShapeFieldMapper extends FieldMapper {
throw new IllegalArgumentException("Unknown prefix tree strategy [" + strategyName + "]");
}
@Override
public String value(Object value) {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
}
}
protected Explicit<Boolean> coerce;

View File

@ -197,14 +197,6 @@ public class AllFieldMapper extends MetadataFieldMapper {
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query queryStringTermQuery(Term term) {
return new AllTermQuery(term);

View File

@ -180,14 +180,6 @@ public class FieldNamesFieldMapper extends MetadataFieldMapper {
return enabled;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query termQuery(Object value, QueryShardContext context) {
if (isEnabled() == false) {

View File

@ -128,14 +128,6 @@ public class IdFieldMapper extends MetadataFieldMapper {
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
if (indexOptions() != IndexOptions.NONE || context == null) {

View File

@ -169,14 +169,6 @@ public class IndexFieldMapper extends MetadataFieldMapper {
}
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public IndexFieldData.Builder fielddataBuilder() {
return new IndexIndexFieldData.Builder();
@ -198,11 +190,6 @@ public class IndexFieldMapper extends MetadataFieldMapper {
return this.enabledState.enabled;
}
public String value(Document document) {
Field field = (Field) document.getField(fieldType().name());
return field == null ? null : (String)fieldType().value(field);
}
@Override
public void preParse(ParseContext context) throws IOException {
// we pre parse it and not in parse, since its not part of the root object

View File

@ -125,14 +125,6 @@ public class RoutingFieldMapper extends MetadataFieldMapper {
public String typeName() {
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
}
private boolean required;
@ -154,11 +146,6 @@ public class RoutingFieldMapper extends MetadataFieldMapper {
return this.required;
}
public String value(Document document) {
Field field = (Field) document.getField(fieldType().name());
return field == null ? null : (String)fieldType().value(field);
}
@Override
public void preParse(ParseContext context) throws IOException {
super.parse(context);

View File

@ -22,14 +22,10 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.settings.Settings;
@ -171,24 +167,6 @@ public class SourceFieldMapper extends MetadataFieldMapper {
public String typeName() {
return CONTENT_TYPE;
}
@Override
public byte[] value(Object value) {
if (value == null) {
return null;
}
BytesReference bValue;
if (value instanceof BytesRef) {
bValue = new BytesArray((BytesRef) value);
} else {
bValue = (BytesReference) value;
}
try {
return CompressorFactory.uncompressIfNeeded(bValue).toBytes();
} catch (IOException e) {
throw new ElasticsearchParseException("failed to decompress source", e);
}
}
}
private final boolean enabled;

View File

@ -151,7 +151,7 @@ public class TTLFieldMapper extends MetadataFieldMapper {
} else {
now = System.currentTimeMillis();
}
long val = value(value);
Long val = (Long) super.valueForSearch(value);
return val - now;
}
}

View File

@ -182,12 +182,9 @@ public class TimestampFieldMapper extends MetadataFieldMapper {
return new TimestampFieldType(this);
}
/**
* Override the default behavior to return a timestamp
*/
@Override
public Object valueForSearch(Object value) {
return value(value);
return value;
}
}

View File

@ -118,14 +118,6 @@ public class TypeFieldMapper extends MetadataFieldMapper {
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
if (indexOptions() == IndexOptions.NONE) {

View File

@ -119,14 +119,6 @@ public class UidFieldMapper extends MetadataFieldMapper {
return CONTENT_TYPE;
}
@Override
public Uid value(Object value) {
if (value == null) {
return null;
}
return Uid.createUid(value.toString());
}
@Override
public IndexFieldData.Builder fielddataBuilder() {
// TODO: add doc values support?

View File

@ -96,15 +96,6 @@ public class VersionFieldMapper extends MetadataFieldMapper {
public String typeName() {
return CONTENT_TYPE;
}
@Override
public Long value(Object value) {
if (value == null || (value instanceof Long)) {
return (Long) value;
} else {
return Long.parseLong(value.toString());
}
}
}
private VersionFieldMapper(Settings indexSettings) {

View File

@ -183,26 +183,12 @@ public class IpFieldMapper extends NumberFieldMapper {
return CONTENT_TYPE;
}
@Override
public Long value(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).longValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToLong((BytesRef) value);
}
return ipToLong(value.toString());
}
/**
* IPs should return as a string.
*/
@Override
public Object valueForSearch(Object value) {
Long val = value(value);
Long val = (Long) value;
if (val == null) {
return null;
}

View File

@ -38,4 +38,13 @@ public class BooleanFieldTypeTests extends FieldTypeTestCase {
assertEquals("false", ft.docValueFormat(null, null).format(0));
assertEquals("true", ft.docValueFormat(null, null).format(1));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
assertEquals(true, ft.valueForSearch("T"));
assertEquals(false, ft.valueForSearch("F"));
expectThrows(IllegalArgumentException.class, () -> ft.valueForSearch(0));
expectThrows(IllegalArgumentException.class, () -> ft.valueForSearch("true"));
expectThrows(IllegalArgumentException.class, () -> ft.valueForSearch("G"));
}
}

View File

@ -32,4 +32,10 @@ public class ByteFieldTypeTests extends FieldTypeTestCase {
public void setupProperties() {
setDummyNullValue((byte)10);
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
// bytes are stored as ints
assertEquals(Byte.valueOf((byte) 3), ft.valueForSearch(Integer.valueOf(3)));
}
}

View File

@ -140,4 +140,11 @@ public class DateFieldTypeTests extends FieldTypeTestCase {
assertEquals(DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-13").getMillis() - 1,
ft.docValueFormat(null, DateTimeZone.UTC).parseLong("2015-10-12||/d", true, null));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
String date = "2015-10-12T12:09:55.000Z";
long instant = DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date).getMillis();
assertEquals(date, ft.valueForSearch(instant));
}
}

View File

@ -43,4 +43,9 @@ public class DoubleFieldTypeTests extends FieldTypeTestCase {
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomDouble(), randomDouble(),
randomBoolean(), randomBoolean(), null, null));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
assertEquals(Double.valueOf(1.2), ft.valueForSearch(1.2));
}
}

View File

@ -29,12 +29,12 @@ import java.io.IOException;
public class FloatFieldTypeTests extends FieldTypeTestCase {
@Override
protected MappedFieldType createDefaultFieldType() {
return new DoubleFieldMapper.DoubleFieldType();
return new FloatFieldMapper.FloatFieldType();
}
@Before
public void setupProperties() {
setDummyNullValue(10.0);
setDummyNullValue(10.0f);
}
public void testIsFieldWithinQuery() throws IOException {
@ -43,4 +43,9 @@ public class FloatFieldTypeTests extends FieldTypeTestCase {
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomFloat(), randomFloat(),
randomBoolean(), randomBoolean(), null, null));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
assertEquals(Float.valueOf(1.2f), ft.valueForSearch(1.2f));
}
}

View File

@ -43,4 +43,9 @@ public class IntegerFieldTypeTests extends FieldTypeTestCase {
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomInt(), randomInt(),
randomBoolean(), randomBoolean(), null, null));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
assertEquals(Integer.valueOf(3), ft.valueForSearch(Integer.valueOf(3)));
}
}

View File

@ -43,4 +43,9 @@ public class LongFieldTypeTests extends FieldTypeTestCase {
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomLong(), randomLong(),
randomBoolean(), randomBoolean(), null, null));
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
assertEquals(Long.valueOf(3), ft.valueForSearch(Long.valueOf(3)));
}
}

View File

@ -32,4 +32,10 @@ public class ShortFieldTypeTests extends FieldTypeTestCase {
public void setupProperties() {
setDummyNullValue((short)10);
}
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
// shorts are stored as ints
assertEquals(Short.valueOf((short) 3), ft.valueForSearch(Integer.valueOf(3)));
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.mapper.internal;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.mapper.core.DateFieldTypeTests;
public class TimestampFieldTypeTests extends DateFieldTypeTests {
@ -26,4 +27,12 @@ public class TimestampFieldTypeTests extends DateFieldTypeTests {
protected MappedFieldType createDefaultFieldType() {
return new TimestampFieldMapper.TimestampFieldType();
}
@Override
public void testValueForSearch() {
MappedFieldType ft = createDefaultFieldType();
String date = "2015-10-12T12:09:55.000Z";
long instant = DateFieldMapper.Defaults.DATE_TIME_FORMATTER.parser().parseDateTime(date).getMillis();
assertEquals(instant, ft.valueForSearch(instant));
}
}

View File

@ -19,16 +19,11 @@
package org.elasticsearch.index.mapper.lucene;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -37,9 +32,7 @@ import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.test.ESSingleNodeTestCase;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
@ -54,9 +47,15 @@ public class StoredNumericValuesTests extends ESSingleNodeTestCase {
.startObject()
.startObject("type")
.startObject("properties")
.startObject("field1").field("type", "integer").field("store", true).endObject()
.startObject("field2").field("type", "float").field("store", true).endObject()
.startObject("field3").field("type", "long").field("store", true).endObject()
.startObject("field1").field("type", "byte").field("store", true).endObject()
.startObject("field2").field("type", "short").field("store", true).endObject()
.startObject("field3").field("type", "integer").field("store", true).endObject()
.startObject("field4").field("type", "float").field("store", true).endObject()
.startObject("field5").field("type", "long").field("store", true).endObject()
.startObject("field6").field("type", "double").field("store", true).endObject()
.startObject("field7").field("type", "ip").field("store", true).endObject()
.startObject("field8").field("type", "date").field("store", true).endObject()
.startObject("field9").field("type", "boolean").field("store", true).endObject()
.endObject()
.endObject()
.endObject()
@ -66,55 +65,55 @@ public class StoredNumericValuesTests extends ESSingleNodeTestCase {
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field1", 1)
.field("field2", 1.1)
.startArray("field3").value(1).value(2).value(3).endArray()
.field("field2", 1)
.field("field3", 1)
.field("field4", 1.1)
.startArray("field5").value(1).value(2).value(3).endArray()
.field("field6", 1.1)
.field("field7", "192.168.1.1")
.field("field8", "2016-04-05")
.field("field9", true)
.endObject()
.bytes());
writer.addDocument(doc.rootDoc());
// Indexing a doc in the old way
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setNumericType(FieldType.LegacyNumericType.INT);
Document doc2 = new Document();
doc2.add(new StoredField("field1", new BytesRef(Numbers.intToBytes(1))));
doc2.add(new StoredField("field2", new BytesRef(Numbers.floatToBytes(1.1f))));
doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(1L))));
doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(2L))));
doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(3L))));
writer.addDocument(doc2);
DirectoryReader reader = DirectoryReader.open(writer);
IndexSearcher searcher = new IndexSearcher(reader);
Set<String> fields = new HashSet<>(Arrays.asList("field1", "field2", "field3"));
CustomFieldsVisitor fieldsVisitor = new CustomFieldsVisitor(fields, false);
CustomFieldsVisitor fieldsVisitor = new CustomFieldsVisitor(
Collections.emptySet(), Collections.singletonList("field*"), false);
searcher.doc(0, fieldsVisitor);
fieldsVisitor.postProcess(mapper);
assertThat(fieldsVisitor.fields().size(), equalTo(3));
assertThat(fieldsVisitor.fields().size(), equalTo(9));
assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1L));
assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2L));
assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3L));
assertThat(fieldsVisitor.fields().get("field1").get(0), equalTo((byte) 1));
// Make sure the doc gets loaded as if it was stored in the new way
fieldsVisitor.reset();
searcher.doc(1, fieldsVisitor);
fieldsVisitor.postProcess(mapper);
assertThat(fieldsVisitor.fields().size(), equalTo(3));
assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1L));
assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2L));
assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3L));
assertThat(fieldsVisitor.fields().get("field2").get(0), equalTo((short) 1));
assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field3").get(0), equalTo(1));
assertThat(fieldsVisitor.fields().get("field4").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field4").get(0), equalTo(1.1f));
assertThat(fieldsVisitor.fields().get("field5").size(), equalTo(3));
assertThat(fieldsVisitor.fields().get("field5").get(0), equalTo(1L));
assertThat(fieldsVisitor.fields().get("field5").get(1), equalTo(2L));
assertThat(fieldsVisitor.fields().get("field5").get(2), equalTo(3L));
assertThat(fieldsVisitor.fields().get("field6").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field6").get(0), equalTo(1.1));
assertThat(fieldsVisitor.fields().get("field7").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field7").get(0), equalTo("192.168.1.1"));
assertThat(fieldsVisitor.fields().get("field8").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field8").get(0), equalTo("2016-04-05T00:00:00.000Z"));
assertThat(fieldsVisitor.fields().get("field9").size(), equalTo(1));
assertThat(fieldsVisitor.fields().get("field9").get(0), equalTo(true));
reader.close();
writer.close();

View File

@ -105,12 +105,12 @@ public class BulkTests extends ESIntegTestCase {
.actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(2L));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(2L));
assertThat(((Number) getResponse.getField("field").getValue()).longValue(), equalTo(2L));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(2L));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(3L));
assertThat(((Number) getResponse.getField("field").getValue()).longValue(), equalTo(3L));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("3").setFields("field1").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
@ -143,7 +143,7 @@ public class BulkTests extends ESIntegTestCase {
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("6").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(1L));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(0L));
assertThat(((Number) getResponse.getField("field").getValue()).longValue(), equalTo(0L));
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("7").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(false));
@ -151,7 +151,7 @@ public class BulkTests extends ESIntegTestCase {
getResponse = client().prepareGet().setIndex("test").setType("type1").setId("2").setFields("field").execute().actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(3L));
assertThat(((Long) getResponse.getField("field").getValue()), equalTo(4L));
assertThat(((Number) getResponse.getField("field").getValue()).longValue(), equalTo(4L));
}
public void testBulkVersioning() throws Exception {
@ -276,7 +276,7 @@ public class BulkTests extends ESIntegTestCase {
.actionGet();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getVersion(), equalTo(1L));
assertThat((Long) getResponse.getField("counter").getValue(), equalTo(1L));
assertThat(((Number) getResponse.getField("counter").getValue()).longValue(), equalTo(1L));
}
}

View File

@ -117,11 +117,6 @@ public class AttachmentMapper extends FieldMapper {
public String typeName() {
return CONTENT_TYPE;
}
@Override
public String value(Object value) {
return value == null?null:value.toString();
}
}
public static class Builder extends FieldMapper.Builder<Builder, AttachmentMapper> {