indexedTerm to return bytes

part of the effort to reduce conversion from string to types
This commit is contained in:
Shay Banon 2012-12-18 10:58:03 -08:00
parent 1cb531f000
commit b9c5f0472c
24 changed files with 75 additions and 81 deletions

View File

@ -25,6 +25,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.codec.postingsformat.PostingsFormatProvider;
import org.elasticsearch.index.field.data.FieldDataType;
@ -106,6 +107,13 @@ public interface FieldMapper<T> {
public Term createIndexNameTerm(String value) {
return new Term(indexName, value);
}
/**
* Creates a new index term based on the provided value.
*/
public Term createIndexNameTerm(BytesRef value) {
return new Term(indexName, value);
}
}
Names names();
@ -172,7 +180,7 @@ public interface FieldMapper<T> {
/**
* Returns the indexed value.
*/
String indexedValue(String value);
BytesRef indexedValue(String value);
/**
* Should the field query {@link #fieldQuery(String, org.elasticsearch.index.query.QueryParseContext)} be used when detecting this
@ -185,6 +193,8 @@ public interface FieldMapper<T> {
*/
Query fieldQuery(String value, @Nullable QueryParseContext context);
Filter fieldFilter(String value, @Nullable QueryParseContext context);
Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions);
Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions);
@ -202,8 +212,6 @@ public interface FieldMapper<T> {
*/
Query queryStringTermQuery(Term term);
Filter fieldFilter(String value, @Nullable QueryParseContext context);
/**
* Constructs a range query based on the mapper.
*/

View File

@ -28,6 +28,8 @@ import org.elasticsearch.common.bytes.HashedBytesArray;
public final class Uid {
public static final char DELIMITER = '#';
public static final byte DELIMITER_BYTE = 0x23;
public static final BytesRef DELIMITER_BYTES = new BytesRef(new byte[]{DELIMITER_BYTE});
private final String type;
@ -98,6 +100,14 @@ public final class Uid {
return new Uid(uid.substring(0, delimiterIndex), uid.substring(delimiterIndex + 1));
}
public static BytesRef createUidAsBytes(String type, String id) {
BytesRef ref = new BytesRef(type.length() + 1 + id.length());
ref.copyChars(type);
ref.copyBytes(DELIMITER_BYTES);
ref.copyChars(id);
return ref;
}
public static String createUid(String type, String id) {
return createUid(new StringBuilder(), type, id);
}
@ -110,7 +120,7 @@ public final class Uid {
public static HashedBytesArray[] splitUidIntoTypeAndId(BytesRef uid) {
int loc = -1;
for (int i = uid.offset; i < uid.length; i++) {
if (uid.bytes[i] == 0x23) { // 0x23 is equal to '#'
if (uid.bytes[i] == DELIMITER_BYTE) { // 0x23 is equal to '#'
loc = i;
break;
}

View File

@ -421,8 +421,8 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
}
@Override
public String indexedValue(String value) {
return value;
public BytesRef indexedValue(String value) {
return new BytesRef(value);
}
@Override
@ -494,16 +494,16 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) {
// LUCENE 4 UPGRADE: Perhaps indexedValue() should return a BytesRef?
return new TermRangeQuery(names.indexName(),
lowerTerm == null ? null : new BytesRef(indexedValue(lowerTerm)),
upperTerm == null ? null : new BytesRef(indexedValue(upperTerm)),
lowerTerm == null ? null : indexedValue(lowerTerm),
upperTerm == null ? null : indexedValue(upperTerm),
includeLower, includeUpper);
}
@Override
public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) {
return new TermRangeFilter(names.indexName(),
lowerTerm == null ? null : new BytesRef(indexedValue(lowerTerm)),
upperTerm == null ? null : new BytesRef(indexedValue(upperTerm)),
lowerTerm == null ? null : indexedValue(lowerTerm),
upperTerm == null ? null : indexedValue(upperTerm),
includeLower, includeUpper);
}

View File

@ -156,11 +156,6 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
return null;
}
@Override
public String indexedValue(String value) {
return value;
}
@Override
protected Field parseCreateField(ParseContext context) throws IOException {
if (!stored()) {

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.search.Filter;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.Lucene;
@ -60,6 +61,11 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
public static final Boolean NULL_VALUE = null;
}
public static class Values {
public final static BytesRef TRUE = new BytesRef("T");
public final static BytesRef FALSE = new BytesRef("F");
}
public static class Builder extends AbstractFieldMapper.Builder<Builder, BooleanFieldMapper> {
private Boolean nullValue = Defaults.NULL_VALUE;
@ -169,17 +175,17 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
if (value == null || value.length() == 0) {
return "F";
return Values.FALSE;
}
if (value.length() == 1 && value.charAt(0) == 'F') {
return "F";
return Values.FALSE;
}
if (Booleans.parseBoolean(value, false)) {
return "T";
return Values.TRUE;
}
return "F";
return Values.FALSE;
}
@Override

View File

@ -146,10 +146,10 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Byte.parseByte(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -220,10 +220,10 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(dateTimeFormatter.parser().parseMillis(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -148,11 +148,11 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
long longValue = NumericUtils.doubleToSortableLong(Double.parseDouble(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(longValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -146,11 +146,11 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
int intValue = NumericUtils.floatToSortableInt(Float.parseFloat(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(intValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -148,10 +148,10 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Integer.parseInt(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -148,10 +148,10 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(Long.parseLong(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -148,10 +148,10 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Short.parseShort(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -229,11 +229,6 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
return value;
}
@Override
public String indexedValue(String value) {
return value;
}
@Override
protected boolean customBoost() {
return true;

View File

@ -145,11 +145,11 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
int intValue = NumericUtils.floatToSortableInt(Float.parseFloat(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(intValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -143,11 +143,6 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
return value;
}
@Override
public String indexedValue(String value) {
return value;
}
@Override
public boolean useFieldQueryWithQueryString() {
return true;

View File

@ -137,11 +137,6 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
return value;
}
@Override
public String indexedValue(String value) {
return value;
}
public Term term(String value) {
return names().createIndexNameTerm(value);
}

View File

@ -26,6 +26,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.Lucene;
@ -192,11 +193,11 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
if (value.indexOf(Uid.DELIMITER) == -1) {
return Uid.createUid(type, value);
return Uid.createUidAsBytes(type, value);
}
return value;
return super.indexedValue(value);
}
@Override

View File

@ -151,11 +151,6 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
return value;
}
@Override
public String indexedValue(String value) {
return value;
}
@Override
public void validate(ParseContext context) throws MapperParsingException {
String routing = context.sourceToParse().routing();

View File

@ -373,11 +373,6 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
throw new UnsupportedOperationException();
}
@Override
public String indexedValue(String value) {
return value;
}
@Override
protected String contentType() {
return CONTENT_TYPE;

View File

@ -121,11 +121,6 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
return value;
}
@Override
public String indexedValue(String value) {
return value;
}
public Term term(String value) {
return names().createIndexNameTerm(value);
}

View File

@ -184,11 +184,6 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
return Uid.createUid(value);
}
@Override
public String indexedValue(String value) {
return value;
}
public Term term(String type, String id) {
return term(Uid.createUid(type, id));
}

View File

@ -186,10 +186,10 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
}
@Override
public String indexedValue(String value) {
public BytesRef indexedValue(String value) {
BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(ipToLong(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
return bytesRef;
}
@Override

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
@ -90,15 +91,19 @@ public class SpanTermQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "No value specified for term query");
}
BytesRef valueBytes = null;
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) {
fieldName = smartNameFieldMappers.mapper().names().indexName();
value = smartNameFieldMappers.mapper().indexedValue(value);
valueBytes = smartNameFieldMappers.mapper().indexedValue(value);
}
}
if (valueBytes == null) {
valueBytes = new BytesRef(value);
}
SpanTermQuery query = new SpanTermQuery(new Term(fieldName, value));
SpanTermQuery query = new SpanTermQuery(new Term(fieldName, valueBytes));
query.setBoost(boost);
return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.query;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
@ -90,13 +91,16 @@ public class WildcardQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "No value specified for prefix query");
}
BytesRef valueBytes;
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) {
fieldName = smartNameFieldMappers.mapper().names().indexName();
value = smartNameFieldMappers.mapper().indexedValue(value);
valueBytes = smartNameFieldMappers.mapper().indexedValue(value);
} else {
valueBytes = new BytesRef(value);
}
WildcardQuery query = new WildcardQuery(new Term(fieldName, value));
WildcardQuery query = new WildcardQuery(new Term(fieldName, valueBytes));
QueryParsers.setRewriteMethod(query, rewriteMethod);
query.setRewriteMethod(QueryParsers.parseRewriteMethod(rewriteMethod));
query.setBoost(boost);