more mapper simplification, reduce the value methods

This commit is contained in:
Shay Banon 2012-12-28 20:33:10 -08:00
parent e02015c641
commit 01ba287164
36 changed files with 227 additions and 236 deletions

View File

@ -272,7 +272,7 @@ public class TransportMoreLikeThisAction extends TransportAction<MoreLikeThisReq
if (fieldMapper instanceof InternalMapper) {
return true;
}
String value = fieldMapper.valueAsString(convertField(field));
String value = fieldMapper.value(convertField(field)).toString();
if (value == null) {
return false;
}
@ -299,7 +299,7 @@ public class TransportMoreLikeThisAction extends TransportAction<MoreLikeThisReq
}
private void addMoreLikeThis(MoreLikeThisRequest request, BoolQueryBuilder boolBuilder, FieldMapper fieldMapper, Field field) {
addMoreLikeThis(request, boolBuilder, field.name(), fieldMapper.valueAsString(convertField(field)));
addMoreLikeThis(request, boolBuilder, field.name(), fieldMapper.value(convertField(field)).toString());
}
private void addMoreLikeThis(MoreLikeThisRequest request, BoolQueryBuilder boolBuilder, String fieldName, String likeText) {

View File

@ -19,6 +19,8 @@
package org.elasticsearch.common;
import org.apache.lucene.util.BytesRef;
/**
* A set of utilities for numbers.
*/
@ -38,6 +40,10 @@ public final class Numbers {
return (short) (((arr[0] & 0xff) << 8) | (arr[1] & 0xff));
}
public static short bytesToShort(BytesRef bytes) {
return (short) (((bytes.bytes[bytes.offset] & 0xff) << 8) | (bytes.bytes[bytes.offset + 1] & 0xff));
}
/**
* Converts a byte array to an int.
*
@ -48,6 +54,10 @@ public final class Numbers {
return (arr[0] << 24) | ((arr[1] & 0xff) << 16) | ((arr[2] & 0xff) << 8) | (arr[3] & 0xff);
}
public static int bytesToInt(BytesRef bytes) {
return (bytes.bytes[bytes.offset] << 24) | ((bytes.bytes[bytes.offset + 1] & 0xff) << 16) | ((bytes.bytes[bytes.offset + 2] & 0xff) << 8) | (bytes.bytes[bytes.offset + 3] & 0xff);
}
/**
* Converts a byte array to a long.
*
@ -60,6 +70,12 @@ public final class Numbers {
return (((long) high) << 32) | (low & 0x0ffffffffL);
}
public static long bytesToLong(BytesRef bytes) {
int high = (bytes.bytes[bytes.offset + 0] << 24) | ((bytes.bytes[bytes.offset + 1] & 0xff) << 16) | ((bytes.bytes[bytes.offset + 2] & 0xff) << 8) | (bytes.bytes[bytes.offset + 3] & 0xff);
int low = (bytes.bytes[bytes.offset + 4] << 24) | ((bytes.bytes[bytes.offset + 5] & 0xff) << 16) | ((bytes.bytes[bytes.offset + 6] & 0xff) << 8) | (bytes.bytes[bytes.offset + 7] & 0xff);
return (((long) high) << 32) | (low & 0x0ffffffffL);
}
/**
* Converts a byte array to float.
*
@ -70,6 +86,10 @@ public final class Numbers {
return Float.intBitsToFloat(bytesToInt(arr));
}
public static float bytesToFloat(BytesRef bytes) {
return Float.intBitsToFloat(bytesToInt(bytes));
}
/**
* Converts a byte array to double.
*
@ -80,6 +100,10 @@ public final class Numbers {
return Double.longBitsToDouble(bytesToLong(arr));
}
public static double bytesToDouble(BytesRef bytes) {
return Double.longBitsToDouble(bytesToLong(bytes));
}
/**
* Converts an int to a byte array.
*

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.bytes;
import com.google.common.base.Charsets;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Bytes;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.common.io.stream.StreamInput;
@ -157,4 +158,17 @@ public class ByteBufferBytesReference implements BytesReference {
}
return dst.flip().toString();
}
@Override
public BytesRef toBytesRef() {
if (buffer.hasArray()) {
return new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
}
return new BytesRef(toBytes());
}
@Override
public BytesRef copyBytesRef() {
return new BytesRef(toBytes());
}
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.common.bytes;
import com.google.common.base.Charsets;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Bytes;
import org.elasticsearch.common.io.stream.BytesStreamInput;
@ -41,7 +42,11 @@ public class BytesArray implements BytesReference {
private int length;
public BytesArray(String bytes) {
this(bytes.getBytes(Charsets.UTF_8));
BytesRef bytesRef = new BytesRef();
UnicodeUtil.UTF16toUTF8(bytes, 0, bytes.length(), bytesRef);
this.bytes = bytesRef.bytes;
this.offset = bytesRef.offset;
this.length = bytesRef.length;
}
public BytesArray(BytesRef bytesRef) {
@ -147,6 +152,16 @@ public class BytesArray implements BytesReference {
return new String(bytes, offset, length, Charsets.UTF_8);
}
@Override
public BytesRef toBytesRef() {
return new BytesRef(bytes, offset, length);
}
@Override
public BytesRef copyBytesRef() {
return new BytesRef(Arrays.copyOfRange(bytes, offset, offset + length));
}
@Override
public int hashCode() {
return Helper.bytesHashCode(this);

View File

@ -19,12 +19,12 @@
package org.elasticsearch.common.bytes;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.io.stream.StreamInput;
import org.jboss.netty.buffer.ChannelBuffer;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Comparator;
/**
* A reference to bytes.
@ -136,5 +136,13 @@ public interface BytesReference {
*/
String toUtf8();
/**
* Converts to Lucene BytesRef.
*/
BytesRef toBytesRef();
/**
* Converts to a copied Lucene BytesRef.
*/
BytesRef copyBytesRef();
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.bytes;
import com.google.common.base.Charsets;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.transport.netty.ChannelBufferStreamInputFactory;
import org.jboss.netty.buffer.ChannelBuffer;
@ -107,6 +108,23 @@ public class ChannelBufferBytesReference implements BytesReference {
return buffer.toString(Charsets.UTF_8);
}
@Override
public BytesRef toBytesRef() {
if (buffer.hasArray()) {
return new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes());
}
byte[] copy = new byte[buffer.readableBytes()];
buffer.getBytes(buffer.readerIndex(), copy);
return new BytesRef(copy);
}
@Override
public BytesRef copyBytesRef() {
byte[] copy = new byte[buffer.readableBytes()];
buffer.getBytes(buffer.readerIndex(), copy);
return new BytesRef(copy);
}
@Override
public int hashCode() {
return Helper.bytesHashCode(this);

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.bytes;
import com.google.common.base.Charsets;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.io.stream.BytesStreamInput;
@ -123,6 +124,18 @@ public class HashedBytesArray implements BytesReference {
return new String(bytes, Charsets.UTF_8);
}
@Override
public BytesRef toBytesRef() {
return new BytesRef(bytes);
}
@Override
public BytesRef copyBytesRef() {
byte[] copy = new byte[bytes.length];
System.arraycopy(bytes, 0, copy, 0, bytes.length);
return new BytesRef(copy);
}
@Override
public int hashCode() {
return Helper.bytesHashCode(this);

View File

@ -21,6 +21,7 @@ package org.elasticsearch.index.fieldvisitor;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.mapper.*;
@ -85,7 +86,7 @@ public abstract class FieldsVisitor extends StoredFieldVisitor {
if (SourceFieldMapper.NAME.equals(fieldInfo.name)) {
source = new BytesArray(value);
} else {
addValue(fieldInfo.name, new BytesArray(value));
addValue(fieldInfo.name, new BytesRef(value));
}
}

View File

@ -147,18 +147,11 @@ public interface FieldMapper<T> {
*/
T value(Object value);
T valueFromString(String value);
/**
* Returns the value that will be used as a result for search. Can be only of specific types... .
*/
Object valueForSearch(Object value);
/**
* Returns the actual value of the field as string.
*/
String valueAsString(Object value);
/**
* Returns the indexed value.
*/

View File

@ -379,7 +379,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
@Override
public Object valueForSearch(Object value) {
return valueAsString(value);
return value;
}
@Override
@ -387,11 +387,6 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
return new BytesRef(value);
}
@Override
public String valueAsString(Object value) {
return String.valueOf(value);
}
@Override
public Query queryStringTermQuery(Term term) {
return null;

View File

@ -21,10 +21,12 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.io.stream.CachedStreamOutput;
import org.elasticsearch.common.io.stream.StreamOutput;
@ -139,28 +141,28 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
if (value == null) {
return null;
}
BytesReference bytes;
if (value instanceof BytesRef) {
bytes = new BytesArray((BytesRef) value);
} else if (value instanceof BytesReference) {
bytes = (BytesReference) value;
} else if (value instanceof byte[]) {
bytes = new BytesArray((byte[]) value);
} else {
try {
bytes = new BytesArray(Base64.decode(value.toString()));
} catch (IOException e) {
throw new ElasticSearchParseException("failed to convert bytes", e);
}
}
try {
return CompressorFactory.uncompressIfNeeded(new BytesArray((byte[]) value)).toBytes();
return CompressorFactory.uncompressIfNeeded(bytes).toBytes();
} catch (IOException e) {
throw new ElasticSearchParseException("failed to decompress source", e);
}
}
@Override
public byte[] valueFromString(String value) {
// assume its base64 (json)
try {
return Base64.decode(value);
} catch (Exception e) {
return null;
}
}
@Override
public String valueAsString(Object value) {
return null;
}
@Override
protected Field parseCreateField(ParseContext context) throws IOException {
if (!fieldType().stored()) {

View File

@ -166,17 +166,25 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
@Override
public Boolean value(Object value) {
return valueFromString((String) value);
if (value == null) {
return Boolean.FALSE;
}
String sValue = value.toString();
if (sValue.length() == 0) {
return Boolean.FALSE;
}
if (sValue.length() == 1 && sValue.charAt(0) == 'F') {
return Boolean.FALSE;
}
if (Booleans.parseBoolean(sValue, false)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
@Override
public Boolean valueFromString(String value) {
return value.charAt(0) == 'T' ? Boolean.TRUE : Boolean.FALSE;
}
@Override
public String valueAsString(Object value) {
return ((String) value).charAt(0) == 'T' ? "true" : "false";
public Object valueForSearch(Object value) {
return value(value);
}
@Override

View File

@ -32,7 +32,6 @@ import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -141,13 +140,10 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
if (value instanceof Number) {
return ((Number) value).byteValue();
}
BytesReference bytesReference = (BytesReference) value;
return bytesReference.get(bytesReference.arrayOffset());
}
@Override
public Byte valueFromString(String value) {
return Byte.valueOf(value);
if (value instanceof BytesRef) {
return ((BytesRef) value).bytes[((BytesRef) value).offset];
}
return Byte.parseByte(value.toString());
}
@Override

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.joda.DateMathParser;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
@ -197,26 +196,17 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
if (value instanceof Number) {
return ((Number) value).longValue();
}
return Numbers.bytesToLong(((BytesReference) value).array());
}
@Override
public Long valueFromString(String value) {
return parseStringValue(value);
if (value instanceof BytesRef) {
return Numbers.bytesToLong((BytesRef) value);
}
return parseStringValue(value.toString());
}
/**
* Dates should return as a string.
*
* @param value
*/
@Override
public Object valueForSearch(Object value) {
return valueAsString(value);
}
@Override
public String valueAsString(Object value) {
Long val = value(value);
if (val == null) {
return null;

View File

@ -32,7 +32,6 @@ import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -144,12 +143,10 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
return Numbers.bytesToDouble(((BytesReference) value).array());
}
@Override
public Double valueFromString(String value) {
return Double.valueOf(value);
if (value instanceof BytesRef) {
return Numbers.bytesToDouble((BytesRef) value);
}
return Double.parseDouble(value.toString());
}
@Override

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -142,12 +141,10 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
if (value instanceof Number) {
return ((Number) value).floatValue();
}
return Numbers.bytesToFloat(((BytesReference) value).array());
}
@Override
public Float valueFromString(String value) {
return Float.parseFloat(value);
if (value instanceof BytesRef) {
return Numbers.bytesToFloat((BytesRef) value);
}
return Float.parseFloat(value.toString());
}
@Override

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -144,12 +143,10 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
if (value instanceof Number) {
return ((Number) value).intValue();
}
return Numbers.bytesToInt(((BytesReference) value).array());
}
@Override
public Integer valueFromString(String value) {
return Integer.parseInt(value);
if (value instanceof BytesRef) {
return Numbers.bytesToInt((BytesRef) value);
}
return Integer.parseInt(value.toString());
}
@Override

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -144,12 +143,10 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
if (value instanceof Number) {
return ((Number) value).longValue();
}
return Numbers.bytesToLong(((BytesReference) value).array());
}
@Override
public Long valueFromString(String value) {
return Long.valueOf(value);
if (value instanceof BytesRef) {
return Numbers.bytesToLong((BytesRef) value);
}
return Long.parseLong(value.toString());
}
@Override

View File

@ -255,12 +255,6 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
return value(value);
}
@Override
public String valueAsString(Object value) {
Number num = value(value);
return num == null ? null : num.toString();
}
@Override
public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappingException {
super.merge(mergeWith, mergeContext);

View File

@ -33,7 +33,6 @@ import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -144,12 +143,10 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
if (value instanceof Number) {
return ((Number) value).shortValue();
}
return Numbers.bytesToShort(((BytesReference) value).array());
}
@Override
public Short valueFromString(String value) {
return Short.valueOf(value);
if (value instanceof BytesRef) {
return Numbers.bytesToShort((BytesRef) value);
}
return Short.parseShort(value.toString());
}
@Override

View File

@ -226,12 +226,10 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
@Override
public String value(Object value) {
return String.valueOf(value);
}
@Override
public String valueFromString(String value) {
return value;
if (value == null) {
return null;
}
return value.toString();
}
@Override

View File

@ -185,16 +185,6 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
}
@Override
public String valueFromString(String value) {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
}
@Override
public String valueAsString(Object value) {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
}
public SpatialStrategy spatialStrategy() {
return this.spatialStrategy;
}

View File

@ -230,16 +230,6 @@ public class AllFieldMapper extends AbstractFieldMapper<Void> implements Interna
return null;
}
@Override
public Void valueFromString(String value) {
return null;
}
@Override
public String valueAsString(Object value) {
return null;
}
@Override
public Object valueForSearch(Object value) {
return null;

View File

@ -141,12 +141,13 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
if (value == null) {
return null;
}
return Numbers.bytesToFloat((byte[]) value);
}
@Override
public Float valueFromString(String value) {
return Float.parseFloat(value);
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof BytesRef) {
return Numbers.bytesToFloat((BytesRef) value);
}
return Float.parseFloat(value.toString());
}
@Override

View File

@ -140,12 +140,10 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
@Override
public String value(Object value) {
return String.valueOf(value);
}
@Override
public String valueFromString(String value) {
return value;
if (value == null) {
return null;
}
return value.toString();
}
@Override

View File

@ -23,7 +23,6 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -134,16 +133,10 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override
public String value(Object value) {
return String.valueOf(value);
}
@Override
public String valueFromString(String value) {
return value;
}
public Term term(String value) {
return names().createIndexNameTerm(value);
if (value == null) {
return null;
}
return value.toString();
}
@Override

View File

@ -176,25 +176,26 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
@Override
public Uid value(Object value) {
return Uid.createUid(String.valueOf(value));
}
@Override
public Uid valueFromString(String value) {
return Uid.createUid(value);
if (value == null) {
return null;
}
return Uid.createUid(value.toString());
}
@Override
public Object valueForSearch(Object value) {
String fieldValue = String.valueOf(value);
if (fieldValue == null) {
if (value == null) {
return null;
}
int index = fieldValue.indexOf(Uid.DELIMITER);
if (index == -1) {
return fieldValue;
String sValue = value.toString();
if (sValue == null) {
return null;
}
return fieldValue.substring(index + 1);
int index = sValue.indexOf(Uid.DELIMITER);
if (index == -1) {
return sValue;
}
return sValue.substring(index + 1);
}
@Override

View File

@ -148,12 +148,10 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
@Override
public String value(Object value) {
return String.valueOf(value);
}
@Override
public String valueFromString(String value) {
return value;
if (value == null) {
return null;
}
return value.toString();
}
@Override

View File

@ -20,13 +20,14 @@
package org.elasticsearch.index.mapper.internal;
import com.google.common.base.Objects;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchParseException;
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.CompressedStreamInput;
@ -346,38 +347,24 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
return new StoredField(names().indexName(), source.array(), source.arrayOffset(), source.length());
}
public byte[] value(Document document) {
Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field);
}
public byte[] nativeValue(Field field) {
return field.binaryValue().bytes;
}
@Override
public byte[] value(Object value) {
BytesReference val = (BytesReference) value;
if (val == null) {
if (value == null) {
return null;
}
BytesReference bValue;
if (value instanceof BytesRef) {
bValue = new BytesArray((BytesRef) value);
} else {
bValue = (BytesReference) value;
}
try {
return CompressorFactory.uncompressIfNeeded(val).toBytes();
return CompressorFactory.uncompressIfNeeded(bValue).toBytes();
} catch (IOException e) {
throw new ElasticSearchParseException("failed to decompress source", e);
}
}
@Override
public byte[] valueFromString(String value) {
return null;
}
@Override
public String valueAsString(Object value) {
throw new UnsupportedOperationException();
}
@Override
protected String contentType() {
return CONTENT_TYPE;

View File

@ -162,23 +162,12 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
/**
* Override the default behavior to return a timestamp
*
* @param value
*/
@Override
public Object valueForSearch(Object value) {
return value(value);
}
@Override
public String valueAsString(Object value) {
Long val = value(value);
if (val == null) {
return null;
}
return val.toString();
}
@Override
public void validate(ParseContext context) throws MapperParsingException {
}

View File

@ -112,12 +112,10 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
@Override
public String value(Object value) {
return String.valueOf(value);
}
@Override
public String valueFromString(String value) {
return value;
if (value == null) {
return null;
}
return value.toString();
}
public Term term(String value) {

View File

@ -190,12 +190,10 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
@Override
public Uid value(Object value) {
return Uid.createUid(String.valueOf(value));
}
@Override
public Uid valueFromString(String value) {
return Uid.createUid(value);
if (value == null) {
return null;
}
return Uid.createUid(value.toString());
}
public Term term(String type, String id) {

View File

@ -163,26 +163,20 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
if (value == null) {
return null;
}
return Numbers.bytesToLong((byte[]) value);
}
@Override
public Long valueFromString(String value) {
return ipToLong(value);
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.
*
* @param value
*/
@Override
public Object valueForSearch(Object value) {
return valueAsString(value);
}
@Override
public String valueAsString(Object value) {
Long val = value(value);
if (val == null) {
return null;

View File

@ -120,8 +120,8 @@ public class HistogramFacetProcessor extends AbstractComponent implements FacetP
if (mapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for key_field [" + keyField + "]");
}
long from = ((Number) mapper.valueFromString(sFrom)).longValue();
long to = ((Number) mapper.valueFromString(sTo)).longValue();
long from = ((Number) mapper.value(sFrom)).longValue();
long to = ((Number) mapper.value(sTo)).longValue();
if (valueField != null) {
return new BoundedValueHistogramFacetCollector(facetName, keyField, valueField, interval, from, to, comparatorType, context);

View File

@ -127,10 +127,10 @@ public class RangeFacetProcessor extends AbstractComponent implements FacetProce
}
for (RangeFacet.Entry entry : rangeEntries) {
if (entry.fromAsString != null) {
entry.from = ((Number) mapper.valueFromString(entry.fromAsString)).doubleValue();
entry.from = ((Number) mapper.value(entry.fromAsString)).doubleValue();
}
if (entry.toAsString != null) {
entry.to = ((Number) mapper.valueFromString(entry.toAsString)).doubleValue();
entry.to = ((Number) mapper.value(entry.toAsString)).doubleValue();
}
}
}

View File

@ -342,7 +342,7 @@ public class SearchFieldsTests extends AbstractNodesTests {
assertThat(searchResponse.hits().getAt(0).fields().get("double_field").value(), equalTo((Object) 6.0d));
String dateTime = Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC));
assertThat(searchResponse.hits().getAt(0).fields().get("date_field").value(), equalTo((Object) dateTime));
assertThat(searchResponse.hits().getAt(0).fields().get("boolean_field").value(), equalTo((Object) "true"));
assertThat(searchResponse.hits().getAt(0).fields().get("boolean_field").value(), equalTo((Object) Boolean.TRUE));
assertThat(searchResponse.hits().getAt(0).fields().get("binary_field").value().toString(), equalTo(Base64.encodeBytes("testing text".getBytes("UTF8"))));
}