lucene 4: Converted most Mappers over to FieldType API

This commit is contained in:
Chris Male 2012-10-27 15:19:27 +13:00 committed by Shay Banon
parent e75301b781
commit 549900a082
32 changed files with 973 additions and 648 deletions

View File

@ -5,7 +5,6 @@ import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Rectangle; import com.spatial4j.core.shape.Rectangle;
import com.spatial4j.core.shape.Shape; import com.spatial4j.core.shape.Shape;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.common.geo.GeoShapeConstants; import org.elasticsearch.common.geo.GeoShapeConstants;
@ -56,12 +55,13 @@ public abstract class SpatialStrategy {
* @param shape Shape to convert ints its indexable format * @param shape Shape to convert ints its indexable format
* @return Fieldable for indexing the Shape * @return Fieldable for indexing the Shape
*/ */
public Fieldable createField(Shape shape) { public Field createField(Shape shape) {
int detailLevel = prefixTree.getLevelForDistance( int detailLevel = prefixTree.getLevelForDistance(
calcDistanceFromErrPct(shape, distanceErrorPct, GeoShapeConstants.SPATIAL_CONTEXT)); calcDistanceFromErrPct(shape, distanceErrorPct, GeoShapeConstants.SPATIAL_CONTEXT));
List<Node> nodes = prefixTree.getNodes(shape, detailLevel, true); List<Node> nodes = prefixTree.getNodes(shape, detailLevel, true);
NodeTokenStream tokenStream = nodeTokenStream.get(); NodeTokenStream tokenStream = nodeTokenStream.get();
tokenStream.setNodes(nodes); tokenStream.setNodes(nodes);
// LUCENE 4 Upgrade: We should pass in the FieldType and use it here
return new Field(fieldName.indexName(), tokenStream); return new Field(fieldName.indexName(), tokenStream);
} }

View File

@ -36,7 +36,7 @@ import java.io.Reader;
/** /**
* *
*/ */
public class UidField extends AbstractField { public class UidField extends Field {
public static class DocIdAndVersion { public static class DocIdAndVersion {
public final int docId; public final int docId;

View File

@ -24,7 +24,6 @@ import com.google.common.collect.Maps;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -114,13 +113,13 @@ public class DocumentMapper implements ToXContent {
* Called before a field is added to the document. Return <tt>true</tt> to include * Called before a field is added to the document. Return <tt>true</tt> to include
* it in the document. * it in the document.
*/ */
boolean beforeFieldAdded(FieldMapper fieldMapper, Fieldable fieldable, ParseContext parseContent); boolean beforeFieldAdded(FieldMapper fieldMapper, Field fieldable, ParseContext parseContent);
} }
public static class ParseListenerAdapter implements ParseListener { public static class ParseListenerAdapter implements ParseListener {
@Override @Override
public boolean beforeFieldAdded(FieldMapper fieldMapper, Fieldable fieldable, Object parseContext) { public boolean beforeFieldAdded(FieldMapper fieldMapper, Field fieldable, Object parseContext) {
return true; return true;
} }
} }

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.mapper;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
@ -120,17 +119,21 @@ public interface FieldMapper<T> {
Names names(); Names names();
Field.Index index(); // LUCENE 4 UPGRADE Consider replacing these all with fieldType() and letting consumer pick and choose
boolean indexed(); boolean indexed();
boolean analyzed(); boolean analyzed();
Field.Store store();
boolean stored(); boolean stored();
Field.TermVector termVector(); boolean storeTermVectors();
boolean storeTermVectorOffsets();
boolean storeTermVectorPositions();
boolean storeTermVectorPayloads();
float boost(); float boost();
@ -156,19 +159,19 @@ public interface FieldMapper<T> {
/** /**
* Returns the value that will be used as a result for search. Can be only of specific types... . * Returns the value that will be used as a result for search. Can be only of specific types... .
*/ */
Object valueForSearch(Fieldable field); Object valueForSearch(Field field);
/** /**
* Returns the actual value of the field. * Returns the actual value of the field.
*/ */
T value(Fieldable field); T value(Field field);
T valueFromString(String value); T valueFromString(String value);
/** /**
* Returns the actual value of the field as string. * Returns the actual value of the field as string.
*/ */
String valueAsString(Fieldable field); String valueAsString(Field field);
/** /**
* Returns the indexed value. * Returns the indexed value.

View File

@ -21,11 +21,11 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
@ -44,33 +44,60 @@ import java.io.IOException;
public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper { public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
public static class Defaults { public static class Defaults {
public static final Field.Index INDEX = Field.Index.ANALYZED; public static final FieldType FIELD_TYPE = new FieldType();
public static final Field.Store STORE = Field.Store.NO;
public static final Field.TermVector TERM_VECTOR = Field.TermVector.NO; static {
FIELD_TYPE.setIndexed(true);
FIELD_TYPE.setTokenized(true);
FIELD_TYPE.setStored(false);
FIELD_TYPE.setStoreTermVectors(false);
FIELD_TYPE.setOmitNorms(false);
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
FIELD_TYPE.freeze();
}
public static final float BOOST = 1.0f; public static final float BOOST = 1.0f;
public static final boolean OMIT_NORMS = false;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
} }
public abstract static class OpenBuilder<T extends Builder, Y extends AbstractFieldMapper> extends AbstractFieldMapper.Builder<T, Y> { public abstract static class OpenBuilder<T extends Builder, Y extends AbstractFieldMapper> extends AbstractFieldMapper.Builder<T, Y> {
protected OpenBuilder(String name) { protected OpenBuilder(String name, FieldType fieldType) {
super(name); super(name, fieldType);
} }
@Override @Override
public T index(Field.Index index) { public T index(boolean index) {
return super.index(index); return super.index(index);
} }
@Override @Override
public T store(Field.Store store) { public T store(boolean store) {
return super.store(store); return super.store(store);
} }
@Override @Override
public T termVector(Field.TermVector termVector) { protected T storeTermVectors(boolean termVectors) {
return super.termVector(termVector); return super.storeTermVectors(termVectors);
}
@Override
protected T storeTermVectorOffsets(boolean termVectorOffsets) {
return super.storeTermVectorOffsets(termVectorOffsets);
}
@Override
protected T storeTermVectorPositions(boolean termVectorPositions) {
return super.storeTermVectorPositions(termVectorPositions);
}
@Override
protected T storeTermVectorPayloads(boolean termVectorPayloads) {
return super.storeTermVectorPayloads(termVectorPayloads);
}
@Override
protected T tokenized(boolean tokenized) {
return super.tokenized(tokenized);
} }
@Override @Override
@ -106,35 +133,55 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
public abstract static class Builder<T extends Builder, Y extends AbstractFieldMapper> extends Mapper.Builder<T, Y> { public abstract static class Builder<T extends Builder, Y extends AbstractFieldMapper> extends Mapper.Builder<T, Y> {
protected Field.Index index = Defaults.INDEX; protected final FieldType fieldType;
protected Field.Store store = Defaults.STORE;
protected Field.TermVector termVector = Defaults.TERM_VECTOR;
protected float boost = Defaults.BOOST; protected float boost = Defaults.BOOST;
protected boolean omitNorms = Defaults.OMIT_NORMS;
protected boolean omitNormsSet = false; protected boolean omitNormsSet = false;
protected String indexName; protected String indexName;
protected NamedAnalyzer indexAnalyzer; protected NamedAnalyzer indexAnalyzer;
protected NamedAnalyzer searchAnalyzer; protected NamedAnalyzer searchAnalyzer;
protected Boolean includeInAll; protected Boolean includeInAll;
protected IndexOptions indexOptions = Defaults.INDEX_OPTIONS;
protected boolean indexOptionsSet = false; protected boolean indexOptionsSet = false;
protected Builder(String name) { protected Builder(String name, FieldType fieldType) {
super(name); super(name);
this.fieldType = fieldType;
} }
protected T index(Field.Index index) { protected T index(boolean index) {
this.index = index; this.fieldType.setIndexed(index);
return builder; return builder;
} }
protected T store(Field.Store store) { protected T store(boolean store) {
this.store = store; this.fieldType.setStored(store);
return builder; return builder;
} }
protected T termVector(Field.TermVector termVector) { protected T storeTermVectors(boolean termVectors) {
this.termVector = termVector; this.fieldType.setStoreTermVectors(termVectors);
return builder;
}
protected T storeTermVectorOffsets(boolean termVectorOffsets) {
this.fieldType.setStoreTermVectors(termVectorOffsets);
this.fieldType.setStoreTermVectorOffsets(termVectorOffsets);
return builder;
}
protected T storeTermVectorPositions(boolean termVectorPositions) {
this.fieldType.setStoreTermVectors(termVectorPositions);
this.fieldType.setStoreTermVectorPositions(termVectorPositions);
return builder;
}
protected T storeTermVectorPayloads(boolean termVectorPayloads) {
this.fieldType.setStoreTermVectors(termVectorPayloads);
this.fieldType.setStoreTermVectorPayloads(termVectorPayloads);
return builder;
}
protected T tokenized(boolean tokenized) {
this.fieldType.setTokenized(tokenized);
return builder; return builder;
} }
@ -144,13 +191,13 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} }
protected T omitNorms(boolean omitNorms) { protected T omitNorms(boolean omitNorms) {
this.omitNorms = omitNorms; this.fieldType.setOmitNorms(omitNorms);
this.omitNormsSet = true; this.omitNormsSet = true;
return builder; return builder;
} }
protected T indexOptions(IndexOptions indexOptions) { protected T indexOptions(IndexOptions indexOptions) {
this.indexOptions = indexOptions; this.fieldType.setIndexOptions(indexOptions);
this.indexOptionsSet = true; this.indexOptionsSet = true;
return builder; return builder;
} }
@ -191,40 +238,28 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
protected final Names names; protected final Names names;
protected final Field.Index index;
protected final Field.Store store;
protected final Field.TermVector termVector;
protected float boost; protected float boost;
protected final boolean omitNorms; protected final FieldType fieldType;
protected final FieldInfo.IndexOptions indexOptions;
protected final NamedAnalyzer indexAnalyzer; protected final NamedAnalyzer indexAnalyzer;
protected final NamedAnalyzer searchAnalyzer; protected final NamedAnalyzer searchAnalyzer;
protected AbstractFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector, protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
float boost, boolean omitNorms, IndexOptions indexOptions, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
this.names = names; this.names = names;
this.index = index;
this.store = store;
this.termVector = termVector;
this.boost = boost; this.boost = boost;
this.omitNorms = omitNorms; this.fieldType = fieldType;
this.indexOptions = indexOptions; this.fieldType.freeze();
// automatically set to keyword analyzer if its indexed and not analyzed // automatically set to keyword analyzer if its indexed and not analyzed
if (indexAnalyzer == null && !index.isAnalyzed() && index.isIndexed()) { if (indexAnalyzer == null && !this.fieldType.tokenized() && this.fieldType.indexed()) {
this.indexAnalyzer = Lucene.KEYWORD_ANALYZER; this.indexAnalyzer = Lucene.KEYWORD_ANALYZER;
} else { } else {
this.indexAnalyzer = indexAnalyzer; this.indexAnalyzer = indexAnalyzer;
} }
// automatically set to keyword analyzer if its indexed and not analyzed // automatically set to keyword analyzer if its indexed and not analyzed
if (searchAnalyzer == null && !index.isAnalyzed() && index.isIndexed()) { if (searchAnalyzer == null && !this.fieldType.tokenized() && this.fieldType.indexed()) {
this.searchAnalyzer = Lucene.KEYWORD_ANALYZER; this.searchAnalyzer = Lucene.KEYWORD_ANALYZER;
} else { } else {
this.searchAnalyzer = searchAnalyzer; this.searchAnalyzer = searchAnalyzer;
@ -241,34 +276,39 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
return this.names; return this.names;
} }
@Override
public Field.Index index() {
return this.index;
}
@Override
public Field.Store store() {
return this.store;
}
@Override @Override
public boolean stored() { public boolean stored() {
return store == Field.Store.YES; return fieldType.stored();
} }
@Override @Override
public boolean indexed() { public boolean indexed() {
return index != Field.Index.NO; return fieldType.indexed();
} }
@Override @Override
public boolean analyzed() { public boolean analyzed() {
return index == Field.Index.ANALYZED; return fieldType.tokenized();
} }
@Override @Override
public Field.TermVector termVector() { public boolean storeTermVectors() {
return this.termVector; return fieldType.storeTermVectors();
}
@Override
public boolean storeTermVectorOffsets() {
return fieldType.storeTermVectorOffsets();
}
@Override
public boolean storeTermVectorPositions() {
return fieldType.storeTermVectorPositions();
}
@Override
public boolean storeTermVectorPayloads() {
return fieldType.storeTermVectorPayloads();
} }
@Override @Override
@ -278,12 +318,12 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
@Override @Override
public boolean omitNorms() { public boolean omitNorms() {
return this.omitNorms; return fieldType.omitNorms();
} }
@Override @Override
public IndexOptions indexOptions() { public IndexOptions indexOptions() {
return this.indexOptions; return fieldType.indexOptions();
} }
@Override @Override
@ -304,12 +344,10 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
@Override @Override
public void parse(ParseContext context) throws IOException { public void parse(ParseContext context) throws IOException {
try { try {
Fieldable field = parseCreateField(context); Field field = parseCreateField(context);
if (field == null) { if (field == null) {
return; return;
} }
field.setOmitNorms(omitNorms);
field.setIndexOptions(indexOptions);
if (!customBoost()) { if (!customBoost()) {
field.setBoost(boost); field.setBoost(boost);
} }
@ -321,7 +359,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} }
} }
protected abstract Fieldable parseCreateField(ParseContext context) throws IOException; protected abstract Field parseCreateField(ParseContext context) throws IOException;
/** /**
* Derived classes can override it to specify that boost value is set by derived classes. * Derived classes can override it to specify that boost value is set by derived classes.
@ -341,7 +379,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} }
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return valueAsString(field); return valueAsString(field);
} }
@ -396,17 +434,18 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
@Override @Override
public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { 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(), return new TermRangeQuery(names.indexName(),
lowerTerm == null ? null : indexedValue(lowerTerm), lowerTerm == null ? null : new BytesRef(indexedValue(lowerTerm)),
upperTerm == null ? null : indexedValue(upperTerm), upperTerm == null ? null : new BytesRef(indexedValue(upperTerm)),
includeLower, includeUpper); includeLower, includeUpper);
} }
@Override @Override
public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) {
return new TermRangeFilter(names.indexName(), return new TermRangeFilter(names.indexName(),
lowerTerm == null ? null : indexedValue(lowerTerm), lowerTerm == null ? null : new BytesRef(indexedValue(lowerTerm)),
upperTerm == null ? null : indexedValue(upperTerm), upperTerm == null ? null : new BytesRef(indexedValue(upperTerm)),
includeLower, includeUpper); includeLower, includeUpper);
} }
@ -427,14 +466,26 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
return; return;
} }
AbstractFieldMapper fieldMergeWith = (AbstractFieldMapper) mergeWith; AbstractFieldMapper fieldMergeWith = (AbstractFieldMapper) mergeWith;
if (!this.index.equals(fieldMergeWith.index)) { if (this.indexed() != fieldMergeWith.indexed() || this.analyzed() != fieldMergeWith.analyzed()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different index values"); mergeContext.addConflict("mapper [" + names.fullName() + "] has different index values");
} }
if (!this.store.equals(fieldMergeWith.store)) { if (this.stored() != fieldMergeWith.stored()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different store values"); mergeContext.addConflict("mapper [" + names.fullName() + "] has different store values");
} }
if (!this.termVector.equals(fieldMergeWith.termVector)) { if (this.analyzed() != fieldMergeWith.analyzed()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different term_vector values"); mergeContext.addConflict("mapper [" + names.fullName() + "] has different tokenize values");
}
if (this.storeTermVectors() != fieldMergeWith.storeTermVectors()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different store_term_vector values");
}
if (this.storeTermVectorOffsets() != fieldMergeWith.storeTermVectorOffsets()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different store_term_vector_offsets values");
}
if (this.storeTermVectorPositions() != fieldMergeWith.storeTermVectorPositions()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different store_term_vector_positions values");
}
if (this.storeTermVectorPayloads() != fieldMergeWith.storeTermVectorPayloads()) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different store_term_vector_payloads values");
} }
if (this.indexAnalyzer == null) { if (this.indexAnalyzer == null) {
if (fieldMergeWith.indexAnalyzer != null) { if (fieldMergeWith.indexAnalyzer != null) {
@ -486,6 +537,16 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} }
} }
protected static String indexTokenizeOptionToString(boolean indexed, boolean tokenized) {
if (!indexed) {
return "no";
} else if (tokenized) {
return "analyzed";
} else {
return "not_analyzed";
}
}
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
builder.field("type", contentType()); builder.field("type", contentType());
if (!names.name().equals(names.indexNameClean())) { if (!names.name().equals(names.indexNameClean())) {

View File

@ -20,8 +20,8 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Base64; import org.elasticsearch.common.Base64;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -50,7 +50,12 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final long COMPRESS_THRESHOLD = -1; public static final long COMPRESS_THRESHOLD = -1;
public static final Field.Store STORE = Field.Store.YES; public static final FieldType BINARY_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
static {
BINARY_FIELD_TYPE.setStored(false);
BINARY_FIELD_TYPE.freeze();
}
} }
public static class Builder extends AbstractFieldMapper.Builder<Builder, BinaryFieldMapper> { public static class Builder extends AbstractFieldMapper.Builder<Builder, BinaryFieldMapper> {
@ -60,8 +65,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
private long compressThreshold = Defaults.COMPRESS_THRESHOLD; private long compressThreshold = Defaults.COMPRESS_THRESHOLD;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.BINARY_FIELD_TYPE));
store = Defaults.STORE;
builder = this; builder = this;
} }
@ -82,7 +86,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
@Override @Override
public BinaryFieldMapper build(BuilderContext context) { public BinaryFieldMapper build(BuilderContext context) {
return new BinaryFieldMapper(buildNames(context), store, compress, compressThreshold); return new BinaryFieldMapper(buildNames(context), fieldType, compress, compressThreshold);
} }
} }
@ -114,22 +118,22 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
private long compressThreshold; private long compressThreshold;
protected BinaryFieldMapper(Names names, Field.Store store, Boolean compress, long compressThreshold) { protected BinaryFieldMapper(Names names, FieldType fieldType, Boolean compress, long compressThreshold) {
super(names, Field.Index.NO, store, Field.TermVector.NO, 1.0f, true, IndexOptions.DOCS_ONLY, null, null); super(names, 1.0f, fieldType, null, null);
this.compress = compress; this.compress = compress;
this.compressThreshold = compressThreshold; this.compressThreshold = compressThreshold;
} }
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return value(field); return value(field);
} }
@Override @Override
public byte[] value(Fieldable field) { public byte[] value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return value; return null;
} }
try { try {
return CompressorFactory.uncompressIfNeeded(new BytesArray(value)).toBytes(); return CompressorFactory.uncompressIfNeeded(new BytesArray(value)).toBytes();
@ -149,7 +153,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return null; return null;
} }
@ -184,7 +188,7 @@ public class BinaryFieldMapper extends AbstractFieldMapper<byte[]> {
if (value == null) { if (value == null) {
return null; return null;
} }
return new Field(names.indexName(), value); return new Field(names.indexName(), value, fieldType);
} }
@Override @Override

View File

@ -20,8 +20,7 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -49,7 +48,12 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
public static final String CONTENT_TYPE = "boolean"; public static final String CONTENT_TYPE = "boolean";
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final boolean OMIT_NORMS = true; public static final FieldType BOOLEAN_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
static {
BOOLEAN_FIELD_TYPE.setOmitNorms(true);
BOOLEAN_FIELD_TYPE.freeze();
}
public static final Boolean NULL_VALUE = null; public static final Boolean NULL_VALUE = null;
} }
@ -58,8 +62,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
private Boolean nullValue = Defaults.NULL_VALUE; private Boolean nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.BOOLEAN_FIELD_TYPE));
this.omitNorms = Defaults.OMIT_NORMS;
this.builder = this; this.builder = this;
} }
@ -69,18 +72,33 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
} }
@Override @Override
public Builder index(Field.Index index) { public Builder index(boolean index) {
return super.index(index); return super.index(index);
} }
@Override @Override
public Builder store(Field.Store store) { public Builder store(boolean store) {
return super.store(store); return super.store(store);
} }
@Override @Override
public Builder termVector(Field.TermVector termVector) { protected Builder storeTermVectors(boolean termVectors) {
return super.termVector(termVector); return super.storeTermVectors(termVectors);
}
@Override
protected Builder storeTermVectorOffsets(boolean termVectorOffsets) {
return super.storeTermVectorOffsets(termVectorOffsets);
}
@Override
protected Builder storeTermVectorPositions(boolean termVectorPositions) {
return super.storeTermVectorPositions(termVectorPositions);
}
@Override
protected Builder storeTermVectorPayloads(boolean termVectorPayloads) {
return super.storeTermVectorPayloads(termVectorPayloads);
} }
@Override @Override
@ -96,8 +114,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
@Override @Override
public BooleanFieldMapper build(BuilderContext context) { public BooleanFieldMapper build(BuilderContext context) {
return new BooleanFieldMapper(buildNames(context), index, store, return new BooleanFieldMapper(buildNames(context), boost, fieldType, nullValue);
termVector, boost, omitNorms, indexOptions, nullValue);
} }
} }
@ -119,9 +136,8 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
private Boolean nullValue; private Boolean nullValue;
protected BooleanFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector, protected BooleanFieldMapper(Names names, float boost, FieldType fieldType, Boolean nullValue) {
float boost, boolean omitNorms, IndexOptions indexOptions, Boolean nullValue) { super(names, boost, fieldType, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
super(names, index, store, termVector, boost, omitNorms, indexOptions, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
this.nullValue = nullValue; this.nullValue = nullValue;
} }
@ -131,7 +147,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
} }
@Override @Override
public Boolean value(Fieldable field) { public Boolean value(Field field) {
return field.stringValue().charAt(0) == 'T' ? Boolean.TRUE : Boolean.FALSE; return field.stringValue().charAt(0) == 'T' ? Boolean.TRUE : Boolean.FALSE;
} }
@ -141,7 +157,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return field.stringValue().charAt(0) == 'T' ? "true" : "false"; return field.stringValue().charAt(0) == 'T' ? "true" : "false";
} }
@ -184,7 +200,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
if (value == null) { if (value == null) {
return null; return null;
} }
return new Field(names.indexName(), value, store, index, termVector); return new Field(names.indexName(), value, fieldType);
} }
@Override @Override
@ -195,20 +211,30 @@ public class BooleanFieldMapper extends AbstractFieldMapper<Boolean> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.BOOLEAN_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.BOOLEAN_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.BOOLEAN_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.BOOLEAN_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.BOOLEAN_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.BOOLEAN_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.BOOLEAN_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.BOOLEAN_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.BOOLEAN_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (nullValue != null) { if (nullValue != null) {
builder.field("null_value", nullValue); builder.field("null_value", nullValue);

View File

@ -19,14 +19,15 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -56,6 +57,11 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
public static final String CONTENT_TYPE = "byte"; public static final String CONTENT_TYPE = "byte";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType BYTE_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
BYTE_FIELD_TYPE.freeze();
}
public static final Byte NULL_VALUE = null; public static final Byte NULL_VALUE = null;
} }
@ -64,7 +70,7 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
protected Byte nullValue = Defaults.NULL_VALUE; protected Byte nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.BYTE_FIELD_TYPE));
builder = this; builder = this;
} }
@ -75,8 +81,9 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
@Override @Override
public ByteFieldMapper build(BuilderContext context) { public ByteFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
ByteFieldMapper fieldMapper = new ByteFieldMapper(buildNames(context), ByteFieldMapper fieldMapper = new ByteFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, ignoreMalformed(context)); precisionStep, fuzzyFactor, boost, fieldType, nullValue, ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
} }
@ -102,10 +109,9 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
private String nullValueAsString; private String nullValueAsString;
protected ByteFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, protected ByteFieldMapper(Names names, int precisionStep, String fuzzyFactor, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
Byte nullValue, Explicit<Boolean> ignoreMalformed) { Byte nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_byte/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_byte/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)),
new NamedAnalyzer("_byte/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_byte/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -118,12 +124,12 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
} }
@Override @Override
public Byte value(Fieldable field) { public Byte value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return value[0]; return value.bytes[value.offset];
} }
@Override @Override
@ -133,7 +139,9 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.intToPrefixCoded(Byte.parseByte(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Byte.parseByte(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -216,7 +224,7 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
byte value; byte value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -282,7 +290,7 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
} }
} }
} }
CustomByteNumericField field = new CustomByteNumericField(this, value); CustomByteNumericField field = new CustomByteNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -312,20 +320,30 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.BYTE_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.BYTE_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.BYTE_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.BYTE_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.BYTE_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.BYTE_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.BYTE_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.BYTE_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.BYTE_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -347,15 +365,15 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomByteNumericField(NumberFieldMapper mapper, byte number) { public CustomByteNumericField(NumberFieldMapper mapper, byte number, FieldType fieldType) {
super(mapper, mapper.stored() ? new byte[]{number} : null); super(mapper, mapper.stored() ? new byte[]{number} : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStream(Analyzer analyzer) {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setIntValue(number); return mapper.popCachedStream().setIntValue(number);
} }
return null; return null;

View File

@ -20,12 +20,12 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -63,6 +63,12 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime"); public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime");
public static final FieldType DATE_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
DATE_FIELD_TYPE.freeze();
}
public static final String NULL_VALUE = null; public static final String NULL_VALUE = null;
public static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS; public static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
@ -78,7 +84,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
protected FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER; protected FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.DATE_FIELD_TYPE));
builder = this; builder = this;
} }
@ -103,8 +109,9 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
if (context.indexSettings() != null) { if (context.indexSettings() != null) {
parseUpperInclusive = context.indexSettings().getAsBoolean("index.mapping.date.parse_upper_inclusive", Defaults.PARSE_UPPER_INCLUSIVE); parseUpperInclusive = context.indexSettings().getAsBoolean("index.mapping.date.parse_upper_inclusive", Defaults.PARSE_UPPER_INCLUSIVE);
} }
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
DateFieldMapper fieldMapper = new DateFieldMapper(buildNames(context), dateTimeFormatter, DateFieldMapper fieldMapper = new DateFieldMapper(buildNames(context), dateTimeFormatter,
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, precisionStep, fuzzyFactor, boost, fieldType, nullValue,
timeUnit, parseUpperInclusive, ignoreMalformed(context)); timeUnit, parseUpperInclusive, ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -142,10 +149,9 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
protected final TimeUnit timeUnit; protected final TimeUnit timeUnit;
protected DateFieldMapper(Names names, FormatDateTimeFormatter dateTimeFormatter, int precisionStep, String fuzzyFactor, protected DateFieldMapper(Names names, FormatDateTimeFormatter dateTimeFormatter, int precisionStep, String fuzzyFactor,
Field.Index index, Field.Store store, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
String nullValue, TimeUnit timeUnit, boolean parseUpperInclusive, Explicit<Boolean> ignoreMalformed) { String nullValue, TimeUnit timeUnit, boolean parseUpperInclusive, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_date/" + precisionStep, ignoreMalformed, new NamedAnalyzer("_date/" + precisionStep,
new NumericDateAnalyzer(precisionStep, dateTimeFormatter.parser())), new NumericDateAnalyzer(precisionStep, dateTimeFormatter.parser())),
new NamedAnalyzer("_date/max", new NumericDateAnalyzer(Integer.MAX_VALUE, dateTimeFormatter.parser()))); new NamedAnalyzer("_date/max", new NumericDateAnalyzer(Integer.MAX_VALUE, dateTimeFormatter.parser())));
@ -174,12 +180,12 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Long value(Fieldable field) { public Long value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToLong(value); return Numbers.bytesToLong(value.bytes);
} }
@Override @Override
@ -188,15 +194,15 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
/** /**
* Dates should return as a string, delegates to {@link #valueAsString(org.apache.lucene.document.Fieldable)}. * Dates should return as a string.
*/ */
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return valueAsString(field); return valueAsString(field);
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
Long value = value(field); Long value = value(field);
if (value == null) { if (value == null) {
return null; return null;
@ -206,7 +212,9 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.longToPrefixCoded(dateTimeFormatter.parser().parseMillis(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(dateTimeFormatter.parser().parseMillis(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -297,7 +305,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
String dateAsString = null; String dateAsString = null;
Long value = null; Long value = null;
float boost = this.boost; float boost = this.boost;
@ -343,7 +351,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
if (value != null) { if (value != null) {
LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, timeUnit.toMillis(value)); LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, timeUnit.toMillis(value), fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -356,7 +364,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
value = parseStringValue(dateAsString); value = parseStringValue(dateAsString);
LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, value); LongFieldMapper.CustomLongNumericField field = new LongFieldMapper.CustomLongNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -385,20 +393,30 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.DATE_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.DATE_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.DATE_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.DATE_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.DATE_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.DATE_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.DATE_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.DATE_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.DATE_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);

View File

@ -21,12 +21,12 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -56,6 +56,11 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
public static final String CONTENT_TYPE = "double"; public static final String CONTENT_TYPE = "double";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType DOUBLE_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
DOUBLE_FIELD_TYPE.freeze();
}
public static final Double NULL_VALUE = null; public static final Double NULL_VALUE = null;
} }
@ -64,7 +69,7 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
protected Double nullValue = Defaults.NULL_VALUE; protected Double nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.DOUBLE_FIELD_TYPE));
builder = this; builder = this;
} }
@ -75,8 +80,9 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
@Override @Override
public DoubleFieldMapper build(BuilderContext context) { public DoubleFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
DoubleFieldMapper fieldMapper = new DoubleFieldMapper(buildNames(context), DoubleFieldMapper fieldMapper = new DoubleFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, precisionStep, fuzzyFactor, boost, fieldType, nullValue,
ignoreMalformed(context)); ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -105,10 +111,9 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
private String nullValueAsString; private String nullValueAsString;
protected DoubleFieldMapper(Names names, int precisionStep, String fuzzyFactor, protected DoubleFieldMapper(Names names, int precisionStep, String fuzzyFactor,
Field.Index index, Field.Store store, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
Double nullValue, Explicit<Boolean> ignoreMalformed) { Double nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_double/" + precisionStep, new NumericDoubleAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_double/" + precisionStep, new NumericDoubleAnalyzer(precisionStep)),
new NamedAnalyzer("_double/max", new NumericDoubleAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_double/max", new NumericDoubleAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -121,12 +126,12 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
} }
@Override @Override
public Double value(Fieldable field) { public Double value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToDouble(value); return Numbers.bytesToDouble(value.bytes);
} }
@Override @Override
@ -136,7 +141,10 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.doubleToPrefixCoded(Double.parseDouble(value)); long longValue = NumericUtils.doubleToSortableLong(Double.parseDouble(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(longValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -218,7 +226,7 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
double value; double value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -285,7 +293,7 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
} }
} }
CustomDoubleNumericField field = new CustomDoubleNumericField(this, value); CustomDoubleNumericField field = new CustomDoubleNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -315,20 +323,30 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.DOUBLE_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.DOUBLE_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.DOUBLE_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.DOUBLE_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.DOUBLE_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.DOUBLE_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.DOUBLE_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.DOUBLE_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.DOUBLE_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -350,15 +368,15 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomDoubleNumericField(NumberFieldMapper mapper, double number) { public CustomDoubleNumericField(NumberFieldMapper mapper, double number, FieldType fieldType) {
super(mapper, mapper.stored() ? Numbers.doubleToBytes(number) : null); super(mapper, mapper.stored() ? Numbers.doubleToBytes(number) : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStreamValue() {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setDoubleValue(number); return mapper.popCachedStream().setDoubleValue(number);
} }
return null; return null;

View File

@ -21,12 +21,13 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -57,6 +58,12 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
public static final String CONTENT_TYPE = "float"; public static final String CONTENT_TYPE = "float";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType FLOAT_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
FLOAT_FIELD_TYPE.freeze();
}
public static final Float NULL_VALUE = null; public static final Float NULL_VALUE = null;
} }
@ -65,7 +72,7 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
protected Float nullValue = Defaults.NULL_VALUE; protected Float nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.FLOAT_FIELD_TYPE));
builder = this; builder = this;
} }
@ -76,8 +83,9 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
@Override @Override
public FloatFieldMapper build(BuilderContext context) { public FloatFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
FloatFieldMapper fieldMapper = new FloatFieldMapper(buildNames(context), FloatFieldMapper fieldMapper = new FloatFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, precisionStep, fuzzyFactor, boost, fieldType, nullValue,
ignoreMalformed(context)); ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -104,10 +112,9 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
private String nullValueAsString; private String nullValueAsString;
protected FloatFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, protected FloatFieldMapper(Names names, int precisionStep, String fuzzyFactor, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
Float nullValue, Explicit<Boolean> ignoreMalformed) { Float nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_float/" + precisionStep, new NumericFloatAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_float/" + precisionStep, new NumericFloatAnalyzer(precisionStep)),
new NamedAnalyzer("_float/max", new NumericFloatAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_float/max", new NumericFloatAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -120,12 +127,12 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
} }
@Override @Override
public Float value(Fieldable field) { public Float value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToFloat(value); return Numbers.bytesToFloat(value.bytes);
} }
@Override @Override
@ -135,7 +142,10 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.floatToPrefixCoded(Float.parseFloat(value)); int intValue = NumericUtils.floatToSortableInt(Float.parseFloat(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(intValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -213,7 +223,7 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
float value; float value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -280,7 +290,7 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
} }
} }
CustomFloatNumericField field = new CustomFloatNumericField(this, value); CustomFloatNumericField field = new CustomFloatNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -311,20 +321,30 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.FLOAT_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.FLOAT_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.FLOAT_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.FLOAT_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.FLOAT_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.FLOAT_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.FLOAT_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.FLOAT_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.FLOAT_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -346,15 +366,15 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomFloatNumericField(NumberFieldMapper mapper, float number) { public CustomFloatNumericField(NumberFieldMapper mapper, float number, FieldType fieldType) {
super(mapper, mapper.stored() ? Numbers.floatToBytes(number) : null); super(mapper, mapper.stored() ? Numbers.floatToBytes(number) : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStreamValue() {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setFloatValue(number); return mapper.popCachedStream().setFloatValue(number);
} }
return null; return null;

View File

@ -21,12 +21,12 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -57,6 +57,12 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
public static final String CONTENT_TYPE = "integer"; public static final String CONTENT_TYPE = "integer";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType INTEGER_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
INTEGER_FIELD_TYPE.freeze();
}
public static final Integer NULL_VALUE = null; public static final Integer NULL_VALUE = null;
} }
@ -65,7 +71,7 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
protected Integer nullValue = Defaults.NULL_VALUE; protected Integer nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.INTEGER_FIELD_TYPE));
builder = this; builder = this;
} }
@ -76,8 +82,9 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
@Override @Override
public IntegerFieldMapper build(BuilderContext context) { public IntegerFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
IntegerFieldMapper fieldMapper = new IntegerFieldMapper(buildNames(context), IntegerFieldMapper fieldMapper = new IntegerFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, precisionStep, fuzzyFactor, boost, fieldType,
nullValue, ignoreMalformed(context)); nullValue, ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -104,10 +111,10 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
private String nullValueAsString; private String nullValueAsString;
protected IntegerFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, protected IntegerFieldMapper(Names names, int precisionStep, String fuzzyFactor,
float boost, boolean omitNorms, IndexOptions indexOptions, float boost, FieldType fieldType,
Integer nullValue, Explicit<Boolean> ignoreMalformed) { Integer nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_int/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_int/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)),
new NamedAnalyzer("_int/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_int/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -120,12 +127,12 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
} }
@Override @Override
public Integer value(Fieldable field) { public Integer value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToInt(value); return Numbers.bytesToInt(value.bytes);
} }
@Override @Override
@ -135,7 +142,9 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.intToPrefixCoded(Integer.parseInt(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Integer.parseInt(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -218,7 +227,7 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
int value; int value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -285,7 +294,7 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
} }
} }
CustomIntegerNumericField field = new CustomIntegerNumericField(this, value); CustomIntegerNumericField field = new CustomIntegerNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -315,20 +324,30 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.INTEGER_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.INTEGER_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.INTEGER_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.INTEGER_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.INTEGER_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.INTEGER_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.INTEGER_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.INTEGER_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.INTEGER_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -350,15 +369,15 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomIntegerNumericField(NumberFieldMapper mapper, int number) { public CustomIntegerNumericField(NumberFieldMapper mapper, int number, FieldType fieldType) {
super(mapper, mapper.stored() ? Numbers.intToBytes(number) : null); super(mapper, mapper.stored() ? Numbers.intToBytes(number) : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStreamValue() {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setIntValue(number); return mapper.popCachedStream().setIntValue(number);
} }
return null; return null;

View File

@ -21,12 +21,13 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -57,6 +58,12 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
public static final String CONTENT_TYPE = "long"; public static final String CONTENT_TYPE = "long";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType LONG_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
LONG_FIELD_TYPE.freeze();
}
public static final Long NULL_VALUE = null; public static final Long NULL_VALUE = null;
} }
@ -65,7 +72,7 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
protected Long nullValue = Defaults.NULL_VALUE; protected Long nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.LONG_FIELD_TYPE));
builder = this; builder = this;
} }
@ -76,8 +83,9 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
public LongFieldMapper build(BuilderContext context) { public LongFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
LongFieldMapper fieldMapper = new LongFieldMapper(buildNames(context), LongFieldMapper fieldMapper = new LongFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, precisionStep, fuzzyFactor, boost, fieldType, nullValue,
ignoreMalformed(context)); ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -104,10 +112,10 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
private String nullValueAsString; private String nullValueAsString;
protected LongFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, protected LongFieldMapper(Names names, int precisionStep, String fuzzyFactor,
float boost, boolean omitNorms, IndexOptions indexOptions, float boost, FieldType fieldType,
Long nullValue, Explicit<Boolean> ignoreMalformed) { Long nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_long/" + precisionStep, new NumericLongAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_long/" + precisionStep, new NumericLongAnalyzer(precisionStep)),
new NamedAnalyzer("_long/max", new NumericLongAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_long/max", new NumericLongAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -120,12 +128,12 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Long value(Fieldable field) { public Long value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToLong(value); return Numbers.bytesToLong(value.bytes);
} }
@Override @Override
@ -135,7 +143,9 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.longToPrefixCoded(Long.parseLong(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(Long.parseLong(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -218,7 +228,7 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
long value; long value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -284,7 +294,7 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
} }
} }
} }
CustomLongNumericField field = new CustomLongNumericField(this, value); CustomLongNumericField field = new CustomLongNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -314,20 +324,30 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.LONG_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.LONG_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.LONG_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.LONG_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.LONG_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.LONG_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.LONG_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.LONG_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.LONG_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -349,15 +369,15 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomLongNumericField(NumberFieldMapper mapper, long number) { public CustomLongNumericField(NumberFieldMapper mapper, long number, FieldType fieldType) {
super(mapper, mapper.stored() ? Numbers.longToBytes(number) : null); super(mapper, mapper.stored() ? Numbers.longToBytes(number) : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStreamValue() {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setLongValue(number); return mapper.popCachedStream().setLongValue(number);
} }
return null; return null;

View File

@ -20,10 +20,8 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.NumericTokenStream; import org.apache.lucene.analysis.NumericTokenStream;
import org.apache.lucene.document.AbstractField;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -48,9 +46,17 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final int PRECISION_STEP = NumericUtils.PRECISION_STEP_DEFAULT; public static final int PRECISION_STEP = NumericUtils.PRECISION_STEP_DEFAULT;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final boolean OMIT_NORMS = true; public static final FieldType NUMBER_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY;
static {
NUMBER_FIELD_TYPE.setTokenized(false);
NUMBER_FIELD_TYPE.setOmitNorms(true);
NUMBER_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
NUMBER_FIELD_TYPE.setStoreTermVectors(false);
NUMBER_FIELD_TYPE.freeze();
}
public static final String FUZZY_FACTOR = null; public static final String FUZZY_FACTOR = null;
public static final Explicit<Boolean> IGNORE_MALFORMED = new Explicit<Boolean>(false, false); public static final Explicit<Boolean> IGNORE_MALFORMED = new Explicit<Boolean>(false, false);
} }
@ -63,15 +69,12 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
private Boolean ignoreMalformed; private Boolean ignoreMalformed;
public Builder(String name) { public Builder(String name, FieldType fieldType) {
super(name); super(name, fieldType);
this.index = Defaults.INDEX;
this.omitNorms = Defaults.OMIT_NORMS;
this.indexOptions = Defaults.INDEX_OPTIONS;
} }
@Override @Override
public T store(Field.Store store) { public T store(boolean store) {
return super.store(store); return super.store(store);
} }
@ -134,10 +137,10 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
}; };
protected NumberFieldMapper(Names names, int precisionStep, @Nullable String fuzzyFactor, protected NumberFieldMapper(Names names, int precisionStep, @Nullable String fuzzyFactor,
Field.Index index, Field.Store store, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
Explicit<Boolean> ignoreMalformed, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) { Explicit<Boolean> ignoreMalformed, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
super(names, index, store, Field.TermVector.NO, boost, boost != 1.0f || omitNorms, indexOptions, indexAnalyzer, searchAnalyzer); // LUCENE 4 UPGRADE: Since we can't do anything before the super call, we have to push the boost check down to subclasses
super(names, boost, fieldType, indexAnalyzer, searchAnalyzer);
if (precisionStep <= 0 || precisionStep >= maxPrecisionStep()) { if (precisionStep <= 0 || precisionStep >= maxPrecisionStep()) {
this.precisionStep = Integer.MAX_VALUE; this.precisionStep = Integer.MAX_VALUE;
} else { } else {
@ -176,7 +179,7 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
} }
@Override @Override
protected Fieldable parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
RuntimeException e; RuntimeException e;
try { try {
return innerParseCreateField(context); return innerParseCreateField(context);
@ -193,7 +196,7 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
} }
} }
protected abstract Fieldable innerParseCreateField(ParseContext context) throws IOException; protected abstract Field innerParseCreateField(ParseContext context) throws IOException;
/** /**
* Use the field query created here when matching on numbers. * Use the field query created here when matching on numbers.
@ -242,12 +245,12 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
* Override the default behavior (to return the string, and return the actual Number instance). * Override the default behavior (to return the string, and return the actual Number instance).
*/ */
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return value(field); return value(field);
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
Number num = value(field); Number num = value(field);
return num == null ? null : num.toString(); return num == null ? null : num.toString();
} }
@ -283,28 +286,13 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
} }
// used to we can use a numeric field in a document that is then parsed twice! // used to we can use a numeric field in a document that is then parsed twice!
public abstract static class CustomNumericField extends AbstractField { public abstract static class CustomNumericField extends Field {
protected final NumberFieldMapper mapper; protected final NumberFieldMapper mapper;
public CustomNumericField(NumberFieldMapper mapper, byte[] value) { public CustomNumericField(NumberFieldMapper mapper, byte[] value, FieldType fieldType) {
super(mapper.names().indexName(), value, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.name = mapper.names().indexName();
fieldsData = value;
isIndexed = mapper.indexed();
isTokenized = mapper.indexed();
indexOptions = FieldInfo.IndexOptions.DOCS_ONLY;
omitNorms = mapper.omitNorms();
if (value != null) {
isStored = true;
isBinary = true;
binaryLength = value.length;
binaryOffset = 0;
}
setStoreTermVector(Field.TermVector.NO);
} }
@Override @Override

View File

@ -21,12 +21,13 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -57,6 +58,11 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
public static final String CONTENT_TYPE = "short"; public static final String CONTENT_TYPE = "short";
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final FieldType SHORT_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
SHORT_FIELD_TYPE.freeze();
}
public static final Short NULL_VALUE = null; public static final Short NULL_VALUE = null;
} }
@ -65,7 +71,7 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
protected Short nullValue = Defaults.NULL_VALUE; protected Short nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.SHORT_FIELD_TYPE));
builder = this; builder = this;
} }
@ -76,8 +82,9 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
@Override @Override
public ShortFieldMapper build(BuilderContext context) { public ShortFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
ShortFieldMapper fieldMapper = new ShortFieldMapper(buildNames(context), ShortFieldMapper fieldMapper = new ShortFieldMapper(buildNames(context),
precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, nullValue, precisionStep, fuzzyFactor, boost, fieldType, nullValue,
ignoreMalformed(context)); ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -104,10 +111,10 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
private String nullValueAsString; private String nullValueAsString;
protected ShortFieldMapper(Names names, int precisionStep, String fuzzyFactor, Field.Index index, Field.Store store, protected ShortFieldMapper(Names names, int precisionStep, String fuzzyFactor,
float boost, boolean omitNorms, IndexOptions indexOptions, float boost, FieldType fieldType,
Short nullValue, Explicit<Boolean> ignoreMalformed) { Short nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, fuzzyFactor, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, fuzzyFactor, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_short/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_short/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)),
new NamedAnalyzer("_short/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_short/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -120,12 +127,12 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
} }
@Override @Override
public Short value(Fieldable field) { public Short value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToShort(value); return Numbers.bytesToShort(value.bytes);
} }
@Override @Override
@ -135,7 +142,9 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.intToPrefixCoded(Short.parseShort(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(Short.parseShort(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -218,7 +227,7 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
short value; short value;
float boost = this.boost; float boost = this.boost;
if (context.externalValueSet()) { if (context.externalValueSet()) {
@ -284,7 +293,7 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
} }
} }
} }
CustomShortNumericField field = new CustomShortNumericField(this, value); CustomShortNumericField field = new CustomShortNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -314,20 +323,30 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.SHORT_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.SHORT_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.SHORT_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.SHORT_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.SHORT_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.SHORT_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.SHORT_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.SHORT_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.SHORT_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);
@ -349,15 +368,15 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
private final NumberFieldMapper mapper; private final NumberFieldMapper mapper;
public CustomShortNumericField(NumberFieldMapper mapper, short number) { public CustomShortNumericField(NumberFieldMapper mapper, short number, FieldType fieldType) {
super(mapper, mapper.stored() ? Numbers.shortToBytes(number) : null); super(mapper, mapper.stored() ? Numbers.shortToBytes(number) : null, fieldType);
this.mapper = mapper; this.mapper = mapper;
this.number = number; this.number = number;
} }
@Override @Override
public TokenStream tokenStreamValue() { public TokenStream tokenStreamValue() {
if (isIndexed) { if (fieldType().indexed()) {
return mapper.popCachedStream().setIntValue(number); return mapper.popCachedStream().setIntValue(number);
} }
return null; return null;

View File

@ -21,7 +21,7 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -47,6 +47,12 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
public static final String CONTENT_TYPE = "string"; public static final String CONTENT_TYPE = "string";
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final FieldType STRING_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
STRING_FIELD_TYPE.freeze();
}
// NOTE, when adding defaults here, make sure you add them in the builder // NOTE, when adding defaults here, make sure you add them in the builder
public static final String NULL_VALUE = null; public static final String NULL_VALUE = null;
public static final int POSITION_OFFSET_GAP = 0; public static final int POSITION_OFFSET_GAP = 0;
@ -64,7 +70,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
protected int ignoreAbove = Defaults.IGNORE_ABOVE; protected int ignoreAbove = Defaults.IGNORE_ABOVE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.STRING_FIELD_TYPE));
builder = this; builder = this;
} }
@ -113,16 +119,16 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
// if the field is not analyzed, then by default, we should omit norms and have docs only // if the field is not analyzed, then by default, we should omit norms and have docs only
// index options, as probably what the user really wants // index options, as probably what the user really wants
// if they are set explicitly, we will use those values // if they are set explicitly, we will use those values
if (index == Field.Index.NOT_ANALYZED) { if (fieldType.indexed() && fieldType.tokenized()) {
if (!omitNormsSet) { if (!omitNormsSet) {
omitNorms = true; fieldType.setOmitNorms(true);
} }
if (!indexOptionsSet) { if (!indexOptionsSet) {
indexOptions = IndexOptions.DOCS_ONLY; fieldType.setIndexOptions(IndexOptions.DOCS_ONLY);
} }
} }
StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context), StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
index, store, termVector, boost, omitNorms, indexOptions, nullValue, boost, fieldType, nullValue,
indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, ignoreAbove); indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, ignoreAbove);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -176,18 +182,16 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
private int ignoreAbove; private int ignoreAbove;
protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector, protected StringFieldMapper(Names names, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) { String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
this(names, index, store, termVector, boost, omitNorms, indexOptions, nullValue, indexAnalyzer, this(names, boost, fieldType, nullValue, indexAnalyzer,
searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP, Defaults.IGNORE_ABOVE); searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP, Defaults.IGNORE_ABOVE);
} }
protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector, protected StringFieldMapper(Names names, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer,
NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap, int ignoreAbove) { NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap, int ignoreAbove) {
super(names, index, store, termVector, boost, omitNorms, indexOptions, indexAnalyzer, searchAnalyzer); super(names, boost, fieldType, indexAnalyzer, searchAnalyzer);
this.nullValue = nullValue; this.nullValue = nullValue;
this.positionOffsetGap = positionOffsetGap; this.positionOffsetGap = positionOffsetGap;
this.searchQuotedAnalyzer = searchQuotedAnalyzer != null ? searchQuotedAnalyzer : this.searchAnalyzer; this.searchQuotedAnalyzer = searchQuotedAnalyzer != null ? searchQuotedAnalyzer : this.searchAnalyzer;
@ -209,7 +213,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
return field.stringValue(); return field.stringValue();
} }
@ -219,7 +223,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return value(field); return value(field);
} }
@ -291,7 +295,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
context.ignoredValue(names.indexName(), value); context.ignoredValue(names.indexName(), value);
return null; return null;
} }
Field field = new Field(names.indexName(), false, value, store, index, termVector); Field field = new Field(names.indexName(), value, fieldType);
field.setBoost(boost); field.setBoost(boost);
return field; return field;
} }
@ -317,20 +321,30 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.STRING_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.STRING_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.STRING_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.STRING_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.STRING_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.STRING_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.STRING_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.STRING_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.STRING_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (nullValue != null) { if (nullValue != null) {
builder.field("null_value", nullValue); builder.field("null_value", nullValue);

View File

@ -19,8 +19,6 @@
package org.elasticsearch.index.mapper.core; package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -68,11 +66,21 @@ public class TypeParsers {
} else if (propName.equals("store")) { } else if (propName.equals("store")) {
builder.store(parseStore(name, propNode.toString())); builder.store(parseStore(name, propNode.toString()));
} else if (propName.equals("index")) { } else if (propName.equals("index")) {
builder.index(parseIndex(name, propNode.toString())); parseIndex(name, propNode.toString(), builder);
} else if (propName.equals("tokenized")) {
builder.tokenized(nodeBooleanValue(propNode));
} else if (propName.equals("term_vector")) { } else if (propName.equals("term_vector")) {
builder.termVector(parseTermVector(name, propNode.toString())); parseTermVector(name, propNode.toString(), builder);
} else if (propName.equals("boost")) { } else if (propName.equals("boost")) {
builder.boost(nodeFloatValue(propNode)); builder.boost(nodeFloatValue(propNode));
} else if (propName.equals("store_term_vectors")) {
builder.storeTermVectors(nodeBooleanValue(propNode));
} else if (propName.equals("store_term_vector_offsets")) {
builder.storeTermVectorOffsets(nodeBooleanValue(propNode));
} else if (propName.equals("store_term_vector_positions")) {
builder.storeTermVectorPositions(nodeBooleanValue(propNode));
} else if (propName.equals("store_term_vector_payloads")) {
builder.storeTermVectorPayloads(nodeBooleanValue(propNode));
} else if (propName.equals("omit_norms")) { } else if (propName.equals("omit_norms")) {
builder.omitNorms(nodeBooleanValue(propNode)); builder.omitNorms(nodeBooleanValue(propNode));
} else if (propName.equals("omit_term_freq_and_positions")) { } else if (propName.equals("omit_term_freq_and_positions")) {
@ -122,48 +130,46 @@ public class TypeParsers {
return Joda.forPattern(node.toString()); return Joda.forPattern(node.toString());
} }
public static Field.TermVector parseTermVector(String fieldName, String termVector) throws MapperParsingException { public static void parseTermVector(String fieldName, String termVector, AbstractFieldMapper.Builder builder) throws MapperParsingException {
termVector = Strings.toUnderscoreCase(termVector); termVector = Strings.toUnderscoreCase(termVector);
if ("no".equals(termVector)) { if ("no".equals(termVector)) {
return Field.TermVector.NO; builder.storeTermVectors(false);
} else if ("yes".equals(termVector)) { } else if ("yes".equals(termVector)) {
return Field.TermVector.YES; builder.storeTermVectors(true);
} else if ("with_offsets".equals(termVector)) { } else if ("with_offsets".equals(termVector)) {
return Field.TermVector.WITH_OFFSETS; builder.storeTermVectorOffsets(true);
} else if ("with_positions".equals(termVector)) { } else if ("with_positions".equals(termVector)) {
return Field.TermVector.WITH_POSITIONS; builder.storeTermVectorPositions(true);
} else if ("with_positions_offsets".equals(termVector)) { } else if ("with_positions_offsets".equals(termVector)) {
return Field.TermVector.WITH_POSITIONS_OFFSETS; builder.storeTermVectorPositions(true);
builder.storeTermVectorOffsets(true);
} else { } else {
throw new MapperParsingException("Wrong value for termVector [" + termVector + "] for field [" + fieldName + "]"); throw new MapperParsingException("Wrong value for termVector [" + termVector + "] for field [" + fieldName + "]");
} }
} }
public static Field.Index parseIndex(String fieldName, String index) throws MapperParsingException { public static void parseIndex(String fieldName, String index, AbstractFieldMapper.Builder builder) throws MapperParsingException {
index = Strings.toUnderscoreCase(index); index = Strings.toUnderscoreCase(index);
if ("no".equals(index)) { if ("no".equals(index)) {
return Field.Index.NO; builder.index(false);
} else if ("not_analyzed".equals(index)) { } else if ("not_analyzed".equals(index)) {
return Field.Index.NOT_ANALYZED; builder.index(true);
builder.tokenized(false);
} else if ("analyzed".equals(index)) { } else if ("analyzed".equals(index)) {
return Field.Index.ANALYZED; builder.index(true);
builder.tokenized(true);
} else { } else {
throw new MapperParsingException("Wrong value for index [" + index + "] for field [" + fieldName + "]"); throw new MapperParsingException("Wrong value for index [" + index + "] for field [" + fieldName + "]");
} }
} }
public static Field.Store parseStore(String fieldName, String store) throws MapperParsingException { public static boolean parseStore(String fieldName, String store) throws MapperParsingException {
if ("no".equals(store)) { if ("no".equals(store)) {
return Field.Store.NO; return false;
} else if ("yes".equals(store)) { } else if ("yes".equals(store)) {
return Field.Store.YES; return true;
} else { } else {
boolean value = nodeBooleanValue(store); return nodeBooleanValue(store);
if (value) {
return Field.Store.YES;
} else {
return Field.Store.NO;
}
} }
} }

View File

@ -1,7 +1,7 @@
package org.elasticsearch.index.mapper.geo; package org.elasticsearch.index.mapper.geo;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfo;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -55,6 +55,18 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
public static final int GEOHASH_LEVELS = GeohashPrefixTree.getMaxLevelsPossible(); public static final int GEOHASH_LEVELS = GeohashPrefixTree.getMaxLevelsPossible();
public static final int QUADTREE_LEVELS = QuadPrefixTree.DEFAULT_MAX_LEVELS; public static final int QUADTREE_LEVELS = QuadPrefixTree.DEFAULT_MAX_LEVELS;
public static final double DISTANCE_ERROR_PCT = 0.025d; public static final double DISTANCE_ERROR_PCT = 0.025d;
public static final FieldType GEO_SHAPE_FIELD_TYPE = new FieldType();
static {
GEO_SHAPE_FIELD_TYPE.setIndexed(true);
GEO_SHAPE_FIELD_TYPE.setTokenized(false);
GEO_SHAPE_FIELD_TYPE.setStored(false);
GEO_SHAPE_FIELD_TYPE.setStoreTermVectors(false);
GEO_SHAPE_FIELD_TYPE.setOmitNorms(true);
GEO_SHAPE_FIELD_TYPE.setIndexOptions(FieldInfo.IndexOptions.DOCS_ONLY);
GEO_SHAPE_FIELD_TYPE.freeze();
}
} }
public static class Builder extends AbstractFieldMapper.Builder<Builder, GeoShapeFieldMapper> { public static class Builder extends AbstractFieldMapper.Builder<Builder, GeoShapeFieldMapper> {
@ -66,7 +78,7 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
private SpatialPrefixTree prefixTree; private SpatialPrefixTree prefixTree;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.GEO_SHAPE_FIELD_TYPE));
} }
public Builder tree(String tree) { public Builder tree(String tree) {
@ -96,7 +108,7 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
throw new ElasticSearchIllegalArgumentException("Unknown prefix tree type [" + tree + "]"); throw new ElasticSearchIllegalArgumentException("Unknown prefix tree type [" + tree + "]");
} }
return new GeoShapeFieldMapper(buildNames(context), prefixTree, distanceErrorPct); return new GeoShapeFieldMapper(buildNames(context), prefixTree, distanceErrorPct, fieldType);
} }
} }
@ -123,13 +135,13 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
private final SpatialStrategy spatialStrategy; private final SpatialStrategy spatialStrategy;
public GeoShapeFieldMapper(FieldMapper.Names names, SpatialPrefixTree prefixTree, double distanceErrorPct) { public GeoShapeFieldMapper(FieldMapper.Names names, SpatialPrefixTree prefixTree, double distanceErrorPct, FieldType fieldType) {
super(names, Field.Index.NOT_ANALYZED, Field.Store.NO, Field.TermVector.NO, 1, true, FieldInfo.IndexOptions.DOCS_ONLY, null, null); super(names, 1, fieldType, null, null);
this.spatialStrategy = new TermQueryPrefixTreeStrategy(names, prefixTree, distanceErrorPct); this.spatialStrategy = new TermQueryPrefixTreeStrategy(names, prefixTree, distanceErrorPct);
} }
@Override @Override
protected Fieldable parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
return spatialStrategy.createField(GeoJSONShapeParser.parse(context.parser())); return spatialStrategy.createField(GeoJSONShapeParser.parse(context.parser()));
} }
@ -162,7 +174,7 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values"); throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
} }
@ -172,7 +184,7 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values"); throw new UnsupportedOperationException("GeoShape fields cannot be converted to String values");
} }

View File

@ -21,7 +21,6 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -195,7 +194,7 @@ public class AllFieldMapper extends AbstractFieldMapper<Void> implements Interna
} }
@Override @Override
protected Fieldable parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
if (!enabled) { if (!enabled) {
return null; return null;
} }
@ -229,7 +228,7 @@ public class AllFieldMapper extends AbstractFieldMapper<Void> implements Interna
} }
@Override @Override
public Void value(Fieldable field) { public Void value(Field field) {
return null; return null;
} }
@ -239,12 +238,12 @@ public class AllFieldMapper extends AbstractFieldMapper<Void> implements Interna
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return null; return null;
} }
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return null; return null;
} }

View File

@ -20,12 +20,12 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Numbers;
@ -59,8 +59,13 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final String NAME = "_boost"; public static final String NAME = "_boost";
public static final Float NULL_VALUE = null; public static final Float NULL_VALUE = null;
public static final Field.Index INDEX = Field.Index.NO;
public static final Field.Store STORE = Field.Store.NO; public static final FieldType BOOST_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
BOOST_FIELD_TYPE.setIndexed(false);
BOOST_FIELD_TYPE.setStored(false);
}
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, BoostFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, BoostFieldMapper> {
@ -68,10 +73,8 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
protected Float nullValue = Defaults.NULL_VALUE; protected Float nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.BOOST_FIELD_TYPE));
builder = this; builder = this;
index = Defaults.INDEX;
store = Defaults.STORE;
} }
public Builder nullValue(float nullValue) { public Builder nullValue(float nullValue) {
@ -82,7 +85,7 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
@Override @Override
public BoostFieldMapper build(BuilderContext context) { public BoostFieldMapper build(BuilderContext context) {
return new BoostFieldMapper(name, buildIndexName(context), return new BoostFieldMapper(name, buildIndexName(context),
precisionStep, index, store, boost, omitNorms, indexOptions, nullValue); precisionStep, boost, fieldType, nullValue);
} }
} }
@ -110,14 +113,12 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
} }
protected BoostFieldMapper(String name, String indexName) { protected BoostFieldMapper(String name, String indexName) {
this(name, indexName, Defaults.PRECISION_STEP, Defaults.INDEX, Defaults.STORE, this(name, indexName, Defaults.PRECISION_STEP, Defaults.BOOST, new FieldType(Defaults.BOOST_FIELD_TYPE), Defaults.NULL_VALUE);
Defaults.BOOST, Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.NULL_VALUE);
} }
protected BoostFieldMapper(String name, String indexName, int precisionStep, Field.Index index, Field.Store store, protected BoostFieldMapper(String name, String indexName, int precisionStep,
float boost, boolean omitNorms, IndexOptions indexOptions, float boost, FieldType fieldType, Float nullValue) {
Float nullValue) { super(new Names(name, indexName, indexName, name), precisionStep, null, boost, fieldType,
super(new Names(name, indexName, indexName, name), precisionStep, null, index, store, boost, omitNorms, indexOptions,
Defaults.IGNORE_MALFORMED, new NamedAnalyzer("_float/" + precisionStep, new NumericFloatAnalyzer(precisionStep)), Defaults.IGNORE_MALFORMED, new NamedAnalyzer("_float/" + precisionStep, new NumericFloatAnalyzer(precisionStep)),
new NamedAnalyzer("_float/max", new NumericFloatAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_float/max", new NumericFloatAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -129,12 +130,12 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
} }
@Override @Override
public Float value(Fieldable field) { public Float value(Field field) {
byte[] value = field.getBinaryValue(); BytesRef value = field.binaryValue();
if (value == null) { if (value == null) {
return null; return null;
} }
return Numbers.bytesToFloat(value); return Numbers.bytesToFloat(value.bytes);
} }
@Override @Override
@ -144,7 +145,10 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.floatToPrefixCoded(Float.parseFloat(value)); int intValue = NumericUtils.floatToSortableInt(Float.parseFloat(value));
BytesRef bytesRef = new BytesRef();
NumericUtils.intToPrefixCoded(intValue, precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -230,13 +234,13 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
final float value = parseFloatValue(context); final float value = parseFloatValue(context);
if (Float.isNaN(value)) { if (Float.isNaN(value)) {
return null; return null;
} }
context.doc().setBoost(value); context.doc().setBoost(value);
return new FloatFieldMapper.CustomFloatNumericField(this, value); return new FloatFieldMapper.CustomFloatNumericField(this, value, fieldType);
} }
private float parseFloatValue(ParseContext context) throws IOException { private float parseFloatValue(ParseContext context) throws IOException {

View File

@ -23,8 +23,9 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -56,10 +57,17 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = IdFieldMapper.NAME; public static final String NAME = IdFieldMapper.NAME;
public static final String INDEX_NAME = IdFieldMapper.NAME; public static final String INDEX_NAME = IdFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NO;
public static final Field.Store STORE = Field.Store.NO; public static final FieldType ID_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final boolean OMIT_NORMS = true;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY; static {
ID_FIELD_TYPE.setIndexed(false);
ID_FIELD_TYPE.setStored(false);
ID_FIELD_TYPE.setOmitNorms(true);
ID_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
ID_FIELD_TYPE.freeze();
}
public static final String PATH = null; public static final String PATH = null;
} }
@ -68,12 +76,8 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
private String path = Defaults.PATH; private String path = Defaults.PATH;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.ID_FIELD_TYPE));
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
store = Defaults.STORE;
index = Defaults.INDEX;
omitNorms = Defaults.OMIT_NORMS;
indexOptions = Defaults.INDEX_OPTIONS;
} }
public Builder path(String path) { public Builder path(String path) {
@ -83,7 +87,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
@Override @Override
public IdFieldMapper build(BuilderContext context) { public IdFieldMapper build(BuilderContext context) {
return new IdFieldMapper(name, indexName, index, store, termVector, boost, omitNorms, indexOptions, path); return new IdFieldMapper(name, indexName, boost, fieldType, path);
} }
} }
@ -106,21 +110,19 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
private final String path; private final String path;
public IdFieldMapper() { public IdFieldMapper() {
this(Defaults.NAME, Defaults.INDEX_NAME, Defaults.INDEX); this(Defaults.NAME, Defaults.INDEX_NAME, new FieldType(Defaults.ID_FIELD_TYPE));
} }
public IdFieldMapper(Field.Index index) { public IdFieldMapper(FieldType fieldType) {
this(Defaults.NAME, Defaults.INDEX_NAME, index); this(Defaults.NAME, Defaults.INDEX_NAME, fieldType);
} }
protected IdFieldMapper(String name, String indexName, Field.Index index) { protected IdFieldMapper(String name, String indexName, FieldType fieldType) {
this(name, indexName, index, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.BOOST, this(name, indexName, Defaults.BOOST, fieldType, Defaults.PATH);
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.PATH);
} }
protected IdFieldMapper(String name, String indexName, Field.Index index, Field.Store store, Field.TermVector termVector, protected IdFieldMapper(String name, String indexName, float boost, FieldType fieldType, String path) {
float boost, boolean omitNorms, IndexOptions indexOptions, String path) { super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER,
super(new Names(name, indexName, indexName, name), index, store, termVector, boost, omitNorms, indexOptions, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER);
this.path = path; this.path = path;
} }
@ -130,12 +132,12 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
} }
public String value(Document document) { public String value(Document document) {
Fieldable field = document.getFieldable(names.indexName()); Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field); return field == null ? null : value(field);
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
return field.stringValue(); return field.stringValue();
} }
@ -145,7 +147,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return value(field); return value(field);
} }
@ -184,14 +186,14 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
} }
Collection<String> queryTypes = context.queryTypes(); Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) { if (queryTypes.size() == 1) {
PrefixQuery prefixQuery = new PrefixQuery(UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(Iterables.getFirst(queryTypes, null), value))); PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)));
if (method != null) { if (method != null) {
prefixQuery.setRewriteMethod(method); prefixQuery.setRewriteMethod(method);
} }
} }
BooleanQuery query = new BooleanQuery(); BooleanQuery query = new BooleanQuery();
for (String queryType : queryTypes) { for (String queryType : queryTypes) {
PrefixQuery prefixQuery = new PrefixQuery(UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(queryType, value))); PrefixQuery prefixQuery = new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value)));
if (method != null) { if (method != null) {
prefixQuery.setRewriteMethod(method); prefixQuery.setRewriteMethod(method);
} }
@ -207,11 +209,11 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
} }
Collection<String> queryTypes = context.queryTypes(); Collection<String> queryTypes = context.queryTypes();
if (queryTypes.size() == 1) { if (queryTypes.size() == 1) {
return new PrefixFilter(UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(Iterables.getFirst(queryTypes, null), value))); return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUid(Iterables.getFirst(queryTypes, null), value)));
} }
XBooleanFilter filter = new XBooleanFilter(); XBooleanFilter filter = new XBooleanFilter();
for (String queryType : queryTypes) { for (String queryType : queryTypes) {
filter.addShould(new PrefixFilter(UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(queryType, value)))); filter.addShould(new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.createUid(queryType, value))));
} }
return filter; return filter;
} }
@ -256,16 +258,16 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
throw new MapperParsingException("Provided id [" + context.id() + "] does not match the content one [" + id + "]"); throw new MapperParsingException("Provided id [" + context.id() + "] does not match the content one [" + id + "]");
} }
context.id(id); context.id(id);
if (index == Field.Index.NO && store == Field.Store.NO) { if (!fieldType.indexed() && !fieldType.stored()) {
return null; return null;
} }
return new Field(names.indexName(), false, context.id(), store, index, termVector); return new Field(names.indexName(), context.id(), fieldType);
} else { } else {
// we are in the pre/post parse phase // we are in the pre/post parse phase
if (index == Field.Index.NO && store == Field.Store.NO) { if (!fieldType.indexed() && !fieldType.stored()) {
return null; return null;
} }
return new Field(names.indexName(), false, context.id(), store, index, termVector); return new Field(names.indexName(), context.id(), fieldType);
} }
} }
@ -277,15 +279,16 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all are defaults, no sense to write it at all // if all are defaults, no sense to write it at all
if (store == Defaults.STORE && index == Defaults.INDEX && path == Defaults.PATH) { if (fieldType.stored() == Defaults.ID_FIELD_TYPE.stored() &&
fieldType.indexed() == Defaults.ID_FIELD_TYPE.indexed() && path == Defaults.PATH) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (store != Defaults.STORE) { if (fieldType.stored() != Defaults.ID_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", fieldType.stored());
} }
if (index != Defaults.INDEX) { if (fieldType.indexed() != Defaults.ID_FIELD_TYPE.indexed()) {
builder.field("index", index.name().toLowerCase()); builder.field("index", fieldType.indexed());
} }
if (path != Defaults.PATH) { if (path != Defaults.PATH) {
builder.field("path", path); builder.field("path", path);

View File

@ -21,7 +21,7 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -48,10 +48,18 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = IndexFieldMapper.NAME; public static final String NAME = IndexFieldMapper.NAME;
public static final String INDEX_NAME = IndexFieldMapper.NAME; public static final String INDEX_NAME = IndexFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final Field.Store STORE = Field.Store.NO; public static final FieldType INDEX_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final boolean OMIT_NORMS = true;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY; static {
INDEX_FIELD_TYPE.setIndexed(true);
INDEX_FIELD_TYPE.setTokenized(false);
INDEX_FIELD_TYPE.setStored(false);
INDEX_FIELD_TYPE.setOmitNorms(true);
INDEX_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
INDEX_FIELD_TYPE.freeze();
}
public static final boolean ENABLED = false; public static final boolean ENABLED = false;
} }
@ -60,12 +68,8 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
private boolean enabled = Defaults.ENABLED; private boolean enabled = Defaults.ENABLED;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.INDEX_FIELD_TYPE));
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
index = Defaults.INDEX;
store = Defaults.STORE;
omitNorms = Defaults.OMIT_NORMS;
indexOptions = Defaults.INDEX_OPTIONS;
} }
public Builder enabled(boolean enabled) { public Builder enabled(boolean enabled) {
@ -75,7 +79,7 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override @Override
public IndexFieldMapper build(BuilderContext context) { public IndexFieldMapper build(BuilderContext context) {
return new IndexFieldMapper(name, indexName, store, termVector, boost, omitNorms, indexOptions, enabled); return new IndexFieldMapper(name, indexName, boost, fieldType, enabled);
} }
} }
@ -103,13 +107,11 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
} }
protected IndexFieldMapper(String name, String indexName) { protected IndexFieldMapper(String name, String indexName) {
this(name, indexName, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.BOOST, this(name, indexName, Defaults.BOOST, new FieldType(Defaults.INDEX_FIELD_TYPE), Defaults.ENABLED);
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.ENABLED);
} }
public IndexFieldMapper(String name, String indexName, Field.Store store, Field.TermVector termVector, public IndexFieldMapper(String name, String indexName, float boost, FieldType fieldType, boolean enabled) {
float boost, boolean omitNorms, IndexOptions indexOptions, boolean enabled) { super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER,
super(new Names(name, indexName, indexName, name), Defaults.INDEX, store, termVector, boost, omitNorms, indexOptions, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER);
this.enabled = enabled; this.enabled = enabled;
} }
@ -119,12 +121,12 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
} }
public String value(Document document) { public String value(Document document) {
Fieldable field = document.getFieldable(names.indexName()); Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field); return field == null ? null : value(field);
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
return field.stringValue(); return field.stringValue();
} }
@ -134,7 +136,7 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return value(field); return value(field);
} }
@ -176,7 +178,7 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
if (!enabled) { if (!enabled) {
return null; return null;
} }
return new Field(names.indexName(), context.index(), store, index); return new Field(names.indexName(), context.index(), fieldType);
} }
@Override @Override
@ -187,12 +189,12 @@ public class IndexFieldMapper extends AbstractFieldMapper<String> implements Int
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all defaults, no need to write it at all // if all defaults, no need to write it at all
if (store == Defaults.STORE && enabled == Defaults.ENABLED) { if (stored() == Defaults.INDEX_FIELD_TYPE.stored() && enabled == Defaults.ENABLED) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (store != Defaults.STORE) { if (stored() != Defaults.INDEX_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (enabled != Defaults.ENABLED) { if (enabled != Defaults.ENABLED) {
builder.field("enabled", enabled); builder.field("enabled", enabled);

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.ConstantScoreQuery;
@ -49,9 +49,17 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = ParentFieldMapper.NAME; public static final String NAME = ParentFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final boolean OMIT_NORMS = true; public static final FieldType PARENT_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY;
static {
PARENT_FIELD_TYPE.setIndexed(true);
PARENT_FIELD_TYPE.setTokenized(false);
PARENT_FIELD_TYPE.setStored(true);
PARENT_FIELD_TYPE.setOmitNorms(true);
PARENT_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
PARENT_FIELD_TYPE.freeze();
}
} }
public static class Builder extends Mapper.Builder<Builder, ParentFieldMapper> { public static class Builder extends Mapper.Builder<Builder, ParentFieldMapper> {
@ -97,8 +105,8 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
private final String type; private final String type;
protected ParentFieldMapper(String name, String indexName, String type) { protected ParentFieldMapper(String name, String indexName, String type) {
super(new Names(name, indexName, indexName, name), Defaults.INDEX, Field.Store.YES, Defaults.TERM_VECTOR, Defaults.BOOST, super(new Names(name, indexName, indexName, name), Defaults.BOOST, new FieldType(Defaults.PARENT_FIELD_TYPE),
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
this.type = type; this.type = type;
} }
@ -130,7 +138,7 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
// we are in the parsing of _parent phase // we are in the parsing of _parent phase
String parentId = context.parser().text(); String parentId = context.parser().text();
context.sourceToParse().parent(parentId); context.sourceToParse().parent(parentId);
return new Field(names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), store, index); return new Field(names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType);
} }
// otherwise, we are running it post processing of the xcontent // otherwise, we are running it post processing of the xcontent
String parsedParentId = context.doc().get(Defaults.NAME); String parsedParentId = context.doc().get(Defaults.NAME);
@ -141,7 +149,7 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
throw new MapperParsingException("No parent id provided, not within the document, and not externally"); throw new MapperParsingException("No parent id provided, not within the document, and not externally");
} }
// we did not add it in the parsing phase, add it now // we did not add it in the parsing phase, add it now
return new Field(names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), store, index); return new Field(names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType);
} else if (parentId != null && !parsedParentId.equals(Uid.createUid(context.stringBuilder(), type, parentId))) { } else if (parentId != null && !parsedParentId.equals(Uid.createUid(context.stringBuilder(), type, parentId))) {
throw new MapperParsingException("Parent id mismatch, document value is [" + Uid.createUid(parsedParentId).id() + "], while external value is [" + parentId + "]"); throw new MapperParsingException("Parent id mismatch, document value is [" + Uid.createUid(parsedParentId).id() + "], while external value is [" + parentId + "]");
} }
@ -151,7 +159,7 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
} }
@Override @Override
public Uid value(Fieldable field) { public Uid value(Field field) {
return Uid.createUid(field.stringValue()); return Uid.createUid(field.stringValue());
} }
@ -161,12 +169,12 @@ public class ParentFieldMapper extends AbstractFieldMapper<Uid> implements Inter
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return field.stringValue(); return field.stringValue();
} }
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
String fieldValue = field.stringValue(); String fieldValue = field.stringValue();
if (fieldValue == null) { if (fieldValue == null) {
return null; return null;

View File

@ -21,7 +21,7 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
@ -47,10 +47,18 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = "_routing"; public static final String NAME = "_routing";
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final Field.Store STORE = Field.Store.YES; public static final FieldType ROUTING_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final boolean OMIT_NORMS = true;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY; static {
ROUTING_FIELD_TYPE.setIndexed(true);
ROUTING_FIELD_TYPE.setTokenized(false);
ROUTING_FIELD_TYPE.setStored(true);
ROUTING_FIELD_TYPE.setOmitNorms(true);
ROUTING_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
ROUTING_FIELD_TYPE.freeze();
}
public static final boolean REQUIRED = false; public static final boolean REQUIRED = false;
public static final String PATH = null; public static final String PATH = null;
} }
@ -62,9 +70,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
private String path = Defaults.PATH; private String path = Defaults.PATH;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.ROUTING_FIELD_TYPE));
store = Defaults.STORE;
index = Defaults.INDEX;
} }
public Builder required(boolean required) { public Builder required(boolean required) {
@ -79,7 +85,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
@Override @Override
public RoutingFieldMapper build(BuilderContext context) { public RoutingFieldMapper build(BuilderContext context) {
return new RoutingFieldMapper(store, index, required, path); return new RoutingFieldMapper(fieldType, required, path);
} }
} }
@ -107,11 +113,11 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
private final String path; private final String path;
public RoutingFieldMapper() { public RoutingFieldMapper() {
this(Defaults.STORE, Defaults.INDEX, Defaults.REQUIRED, Defaults.PATH); this(new FieldType(Defaults.ROUTING_FIELD_TYPE), Defaults.REQUIRED, Defaults.PATH);
} }
protected RoutingFieldMapper(Field.Store store, Field.Index index, boolean required, String path) { protected RoutingFieldMapper(FieldType fieldType, boolean required, String path) {
super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), index, store, Defaults.TERM_VECTOR, 1.0f, Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Lucene.KEYWORD_ANALYZER, super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), 1.0f, fieldType, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER);
this.required = required; this.required = required;
this.path = path; this.path = path;
@ -130,12 +136,12 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
} }
public String value(Document document) { public String value(Document document) {
Fieldable field = document.getFieldable(names.indexName()); Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field); return field == null ? null : value(field);
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
return field.stringValue(); return field.stringValue();
} }
@ -145,7 +151,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return value(field); return value(field);
} }
@ -160,7 +166,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
if (path != null && routing != null) { if (path != null && routing != null) {
// we have a path, check if we can validate we have the same routing value as the one in the doc... // we have a path, check if we can validate we have the same routing value as the one in the doc...
String value = null; String value = null;
Fieldable field = context.doc().getFieldable(path); Field field = (Field) context.doc().getField(path);
if (field != null) { if (field != null) {
value = field.stringValue(); value = field.stringValue();
if (value == null) { if (value == null) {
@ -209,7 +215,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
context.ignoredValue(names.indexName(), routing); context.ignoredValue(names.indexName(), routing);
return null; return null;
} }
return new Field(names.indexName(), routing, store, index); return new Field(names.indexName(), routing, fieldType);
} }
} }
return null; return null;
@ -224,15 +230,16 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all are defaults, no sense to write it at all // if all are defaults, no sense to write it at all
if (index == Defaults.INDEX && store == Defaults.STORE && required == Defaults.REQUIRED && path == Defaults.PATH) { if (indexed() == Defaults.ROUTING_FIELD_TYPE.indexed() &&
stored() == Defaults.ROUTING_FIELD_TYPE.stored() && required == Defaults.REQUIRED && path == Defaults.PATH) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (index != Defaults.INDEX) { if (indexed() != Defaults.ROUTING_FIELD_TYPE.indexed()) {
builder.field("index", index.name().toLowerCase()); builder.field("index", indexed());
} }
if (store != Defaults.STORE) { if (stored() != Defaults.ROUTING_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (required != Defaults.REQUIRED) { if (required != Defaults.REQUIRED) {
builder.field("required", required); builder.field("required", required);

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.*; import org.elasticsearch.index.mapper.*;
@ -40,16 +40,23 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
public static class Defaults extends IntegerFieldMapper.Defaults { public static class Defaults extends IntegerFieldMapper.Defaults {
public static final String NAME = CONTENT_TYPE; public static final String NAME = CONTENT_TYPE;
public static final boolean ENABLED = false; public static final boolean ENABLED = false;
public static final FieldType SIZE_FIELD_TYPE = new FieldType(IntegerFieldMapper.Defaults.INTEGER_FIELD_TYPE);
static {
SIZE_FIELD_TYPE.freeze();
}
} }
public static class Builder extends Mapper.Builder<Builder, IntegerFieldMapper> { public static class Builder extends Mapper.Builder<Builder, IntegerFieldMapper> {
protected boolean enabled = Defaults.ENABLED; protected boolean enabled = Defaults.ENABLED;
protected Field.Store store = Defaults.STORE; protected final FieldType fieldType;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME);
fieldType = new FieldType(Defaults.SIZE_FIELD_TYPE);
builder = this; builder = this;
} }
@ -58,14 +65,14 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
return builder; return builder;
} }
public Builder store(Field.Store store) { public Builder store(boolean store) {
this.store = store; this.fieldType.setStored(store);
return builder; return builder;
} }
@Override @Override
public SizeFieldMapper build(BuilderContext context) { public SizeFieldMapper build(BuilderContext context) {
return new SizeFieldMapper(enabled, store); return new SizeFieldMapper(enabled, fieldType);
} }
} }
@ -89,12 +96,12 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
private final boolean enabled; private final boolean enabled;
public SizeFieldMapper() { public SizeFieldMapper() {
this(Defaults.ENABLED, Defaults.STORE); this(Defaults.ENABLED, new FieldType(Defaults.SIZE_FIELD_TYPE));
} }
public SizeFieldMapper(boolean enabled, Field.Store store) { public SizeFieldMapper(boolean enabled, FieldType fieldType) {
super(new Names(Defaults.NAME), Defaults.PRECISION_STEP, Defaults.FUZZY_FACTOR, Defaults.INDEX, store, super(new Names(Defaults.NAME), Defaults.PRECISION_STEP, Defaults.FUZZY_FACTOR,
Defaults.BOOST, Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.NULL_VALUE, Defaults.BOOST, fieldType, Defaults.NULL_VALUE,
Defaults.IGNORE_MALFORMED); Defaults.IGNORE_MALFORMED);
this.enabled = enabled; this.enabled = enabled;
} }
@ -133,28 +140,28 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
if (!enabled) { if (!enabled) {
return null; return null;
} }
if (context.flyweight()) { if (context.flyweight()) {
return null; return null;
} }
return new CustomIntegerNumericField(this, context.source().length()); return new CustomIntegerNumericField(this, context.source().length(), fieldType);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// all are defaults, no need to write it at all // all are defaults, no need to write it at all
if (enabled == Defaults.ENABLED && store == Defaults.STORE) { if (enabled == Defaults.ENABLED && stored() == Defaults.SIZE_FIELD_TYPE.stored()) {
return builder; return builder;
} }
builder.startObject(contentType()); builder.startObject(contentType());
if (enabled != Defaults.ENABLED) { if (enabled != Defaults.ENABLED) {
builder.field("enabled", enabled); builder.field("enabled", enabled);
} }
if (store != Defaults.STORE) { if (stored() != Defaults.SIZE_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
builder.endObject(); builder.endObject();
return builder; return builder;

View File

@ -22,7 +22,8 @@ package org.elasticsearch.index.mapper.internal;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -67,10 +68,17 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
public static final boolean ENABLED = true; public static final boolean ENABLED = true;
public static final long COMPRESS_THRESHOLD = -1; public static final long COMPRESS_THRESHOLD = -1;
public static final String FORMAT = null; // default format is to use the one provided public static final String FORMAT = null; // default format is to use the one provided
public static final Field.Index INDEX = Field.Index.NO;
public static final Field.Store STORE = Field.Store.YES; public static final FieldType SOURCE_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final boolean OMIT_NORMS = true;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY; static {
SOURCE_FIELD_TYPE.setIndexed(false);
SOURCE_FIELD_TYPE.setStored(true);
SOURCE_FIELD_TYPE.setOmitNorms(true);
SOURCE_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
SOURCE_FIELD_TYPE.freeze();
}
public static final String[] INCLUDES = Strings.EMPTY_ARRAY; public static final String[] INCLUDES = Strings.EMPTY_ARRAY;
public static final String[] EXCLUDES = Strings.EMPTY_ARRAY; public static final String[] EXCLUDES = Strings.EMPTY_ARRAY;
} }
@ -190,8 +198,7 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
} }
protected SourceFieldMapper(String name, boolean enabled, String format, Boolean compress, long compressThreshold, String[] includes, String[] excludes) { protected SourceFieldMapper(String name, boolean enabled, String format, Boolean compress, long compressThreshold, String[] includes, String[] excludes) {
super(new Names(name, name, name, name), Defaults.INDEX, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.BOOST, super(new Names(name, name, name, name), Defaults.BOOST, new FieldType(Defaults.SOURCE_FIELD_TYPE), Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
this.enabled = enabled; this.enabled = enabled;
this.compress = compress; this.compress = compress;
this.compressThreshold = compressThreshold; this.compressThreshold = compressThreshold;
@ -237,7 +244,7 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
if (!enabled) { if (!enabled) {
return null; return null;
} }
if (store == Field.Store.NO) { if (!stored()) {
return null; return null;
} }
if (context.flyweight()) { if (context.flyweight()) {
@ -335,21 +342,21 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
} }
} }
assert source.hasArray(); assert source.hasArray();
return new Field(names().indexName(), source.array(), source.arrayOffset(), source.length()); return new StoredField(names().indexName(), source.array(), source.arrayOffset(), source.length());
} }
public byte[] value(Document document) { public byte[] value(Document document) {
Fieldable field = document.getFieldable(names.indexName()); Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field); return field == null ? null : value(field);
} }
public byte[] nativeValue(Fieldable field) { public byte[] nativeValue(Field field) {
return field.getBinaryValue(); return field.binaryValue().bytes;
} }
@Override @Override
public byte[] value(Fieldable field) { public byte[] value(Field field) {
byte[] value = field.getBinaryValue(); byte[] value = field.binaryValue().bytes;
if (value == null) { if (value == null) {
return value; return value;
} }
@ -366,7 +373,7 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
@ -47,8 +47,16 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
public static class Defaults extends LongFieldMapper.Defaults { public static class Defaults extends LongFieldMapper.Defaults {
public static final String NAME = TTLFieldMapper.CONTENT_TYPE; public static final String NAME = TTLFieldMapper.CONTENT_TYPE;
public static final Field.Store STORE = Field.Store.YES;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED; public static final FieldType TTL_FIELD_TYPE = new FieldType(LongFieldMapper.Defaults.LONG_FIELD_TYPE);
static {
TTL_FIELD_TYPE.setStored(true);
TTL_FIELD_TYPE.setIndexed(true);
TTL_FIELD_TYPE.setTokenized(false);
TTL_FIELD_TYPE.freeze();
}
public static final boolean ENABLED = false; public static final boolean ENABLED = false;
public static final long DEFAULT = -1; public static final long DEFAULT = -1;
} }
@ -59,9 +67,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
private long defaultTTL = Defaults.DEFAULT; private long defaultTTL = Defaults.DEFAULT;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.TTL_FIELD_TYPE));
store = Defaults.STORE;
index = Defaults.INDEX;
} }
public Builder enabled(boolean enabled) { public Builder enabled(boolean enabled) {
@ -76,7 +82,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
@Override @Override
public TTLFieldMapper build(BuilderContext context) { public TTLFieldMapper build(BuilderContext context) {
return new TTLFieldMapper(store, index, enabled, defaultTTL, ignoreMalformed(context)); return new TTLFieldMapper(fieldType, enabled, defaultTTL, ignoreMalformed(context));
} }
} }
@ -105,12 +111,12 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
private long defaultTTL; private long defaultTTL;
public TTLFieldMapper() { public TTLFieldMapper() {
this(Defaults.STORE, Defaults.INDEX, Defaults.ENABLED, Defaults.DEFAULT, Defaults.IGNORE_MALFORMED); this(new FieldType(Defaults.TTL_FIELD_TYPE), Defaults.ENABLED, Defaults.DEFAULT, Defaults.IGNORE_MALFORMED);
} }
protected TTLFieldMapper(Field.Store store, Field.Index index, boolean enabled, long defaultTTL, Explicit<Boolean> ignoreMalformed) { protected TTLFieldMapper(FieldType fieldType, boolean enabled, long defaultTTL, Explicit<Boolean> ignoreMalformed) {
super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), Defaults.PRECISION_STEP, super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), Defaults.PRECISION_STEP,
Defaults.FUZZY_FACTOR, index, store, Defaults.BOOST, Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.FUZZY_FACTOR, Defaults.BOOST, fieldType,
Defaults.NULL_VALUE, ignoreMalformed); Defaults.NULL_VALUE, ignoreMalformed);
this.enabled = enabled; this.enabled = enabled;
this.defaultTTL = defaultTTL; this.defaultTTL = defaultTTL;
@ -126,7 +132,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
// Overrides valueForSearch to display live value of remaining ttl // Overrides valueForSearch to display live value of remaining ttl
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
long now; long now;
SearchContext searchContext = SearchContext.current(); SearchContext searchContext = SearchContext.current();
if (searchContext != null) { if (searchContext != null) {
@ -178,7 +184,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException, AlreadyExpiredException { protected Field innerParseCreateField(ParseContext context) throws IOException, AlreadyExpiredException {
if (enabled) { if (enabled) {
long ttl = context.sourceToParse().ttl(); long ttl = context.sourceToParse().ttl();
if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value
@ -194,7 +200,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R
throw new AlreadyExpiredException(context.index(), context.type(), context.id(), timestamp, ttl, now); throw new AlreadyExpiredException(context.index(), context.type(), context.id(), timestamp, ttl, now);
} }
// the expiration timestamp (timestamp + ttl) is set as field // the expiration timestamp (timestamp + ttl) is set as field
return new CustomLongNumericField(this, expire); return new CustomLongNumericField(this, expire, fieldType);
} }
} }
return null; return null;

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.FormatDateTimeFormatter;
@ -50,8 +50,16 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
public static class Defaults extends DateFieldMapper.Defaults { public static class Defaults extends DateFieldMapper.Defaults {
public static final String NAME = "_timestamp"; public static final String NAME = "_timestamp";
public static final Field.Store STORE = Field.Store.NO;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED; public static final FieldType TIMESTAMP_FIELD_TYPE = new FieldType(DateFieldMapper.Defaults.DATE_FIELD_TYPE);
static {
TIMESTAMP_FIELD_TYPE.setStored(false);
TIMESTAMP_FIELD_TYPE.setIndexed(true);
TIMESTAMP_FIELD_TYPE.setTokenized(false);
TIMESTAMP_FIELD_TYPE.freeze();
}
public static final boolean ENABLED = false; public static final boolean ENABLED = false;
public static final String PATH = null; public static final String PATH = null;
public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern(DEFAULT_DATE_TIME_FORMAT); public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern(DEFAULT_DATE_TIME_FORMAT);
@ -64,9 +72,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
private FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER; private FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER;
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.TIMESTAMP_FIELD_TYPE));
store = Defaults.STORE;
index = Defaults.INDEX;
} }
public Builder enabled(boolean enabled) { public Builder enabled(boolean enabled) {
@ -90,7 +96,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
if (context.indexSettings() != null) { if (context.indexSettings() != null) {
parseUpperInclusive = context.indexSettings().getAsBoolean("index.mapping.date.parse_upper_inclusive", Defaults.PARSE_UPPER_INCLUSIVE); parseUpperInclusive = context.indexSettings().getAsBoolean("index.mapping.date.parse_upper_inclusive", Defaults.PARSE_UPPER_INCLUSIVE);
} }
return new TimestampFieldMapper(store, index, enabled, path, dateTimeFormatter, parseUpperInclusive, ignoreMalformed(context)); return new TimestampFieldMapper(fieldType, enabled, path, dateTimeFormatter, parseUpperInclusive, ignoreMalformed(context));
} }
} }
@ -120,13 +126,13 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
private final String path; private final String path;
public TimestampFieldMapper() { public TimestampFieldMapper() {
this(Defaults.STORE, Defaults.INDEX, Defaults.ENABLED, Defaults.PATH, Defaults.DATE_TIME_FORMATTER, Defaults.PARSE_UPPER_INCLUSIVE, Defaults.IGNORE_MALFORMED); this(new FieldType(Defaults.TIMESTAMP_FIELD_TYPE), Defaults.ENABLED, Defaults.PATH, Defaults.DATE_TIME_FORMATTER, Defaults.PARSE_UPPER_INCLUSIVE, Defaults.IGNORE_MALFORMED);
} }
protected TimestampFieldMapper(Field.Store store, Field.Index index, boolean enabled, String path, protected TimestampFieldMapper(FieldType fieldType, boolean enabled, String path,
FormatDateTimeFormatter dateTimeFormatter, boolean parseUpperInclusive, Explicit<Boolean> ignoreMalformed) { FormatDateTimeFormatter dateTimeFormatter, boolean parseUpperInclusive, Explicit<Boolean> ignoreMalformed) {
super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter, super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), dateTimeFormatter,
Defaults.PRECISION_STEP, Defaults.FUZZY_FACTOR, index, store, Defaults.BOOST, Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Defaults.PRECISION_STEP, Defaults.FUZZY_FACTOR, Defaults.BOOST, fieldType,
Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/, Defaults.NULL_VALUE, TimeUnit.MILLISECONDS /*always milliseconds*/,
parseUpperInclusive, ignoreMalformed); parseUpperInclusive, ignoreMalformed);
this.enabled = enabled; this.enabled = enabled;
@ -149,12 +155,12 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
* Override the default behavior to return a timestamp * Override the default behavior to return a timestamp
*/ */
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return value(field); return value(field);
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
Long value = value(field); Long value = value(field);
if (value == null) { if (value == null) {
return null; return null;
@ -186,14 +192,14 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
if (enabled) { if (enabled) {
long timestamp = context.sourceToParse().timestamp(); long timestamp = context.sourceToParse().timestamp();
if (!indexed() && !stored()) { if (!indexed() && !stored()) {
context.ignoredValue(names.indexName(), String.valueOf(timestamp)); context.ignoredValue(names.indexName(), String.valueOf(timestamp));
return null; return null;
} }
return new LongFieldMapper.CustomLongNumericField(this, timestamp); return new LongFieldMapper.CustomLongNumericField(this, timestamp, fieldType);
} }
return null; return null;
} }
@ -206,16 +212,17 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all are defaults, no sense to write it at all // if all are defaults, no sense to write it at all
if (index == Defaults.INDEX && store == Defaults.STORE && enabled == Defaults.ENABLED && path == Defaults.PATH if (indexed() == Defaults.TIMESTAMP_FIELD_TYPE.indexed() &&
stored() == Defaults.TIMESTAMP_FIELD_TYPE.stored() && enabled == Defaults.ENABLED && path == Defaults.PATH
&& dateTimeFormatter.format().equals(Defaults.DATE_TIME_FORMATTER.format())) { && dateTimeFormatter.format().equals(Defaults.DATE_TIME_FORMATTER.format())) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (index != Defaults.INDEX) { if (indexed() != Defaults.TIMESTAMP_FIELD_TYPE.indexed()) {
builder.field("index", index.name().toLowerCase()); builder.field("index", indexed());
} }
if (store != Defaults.STORE) { if (stored() != Defaults.TIMESTAMP_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (enabled != Defaults.ENABLED) { if (enabled != Defaults.ENABLED) {
builder.field("enabled", enabled); builder.field("enabled", enabled);

View File

@ -21,7 +21,7 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.DeletionAwareConstantScoreQuery; import org.apache.lucene.search.DeletionAwareConstantScoreQuery;
@ -56,26 +56,29 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = TypeFieldMapper.NAME; public static final String NAME = TypeFieldMapper.NAME;
public static final String INDEX_NAME = TypeFieldMapper.NAME; public static final String INDEX_NAME = TypeFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final Field.Store STORE = Field.Store.NO; public static final FieldType TYPE_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final boolean OMIT_NORMS = true;
public static final IndexOptions INDEX_OPTIONS = IndexOptions.DOCS_ONLY; static {
TYPE_FIELD_TYPE.setIndexed(true);
TYPE_FIELD_TYPE.setTokenized(false);
TYPE_FIELD_TYPE.setStored(false);
TYPE_FIELD_TYPE.setOmitNorms(true);
TYPE_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_ONLY);
TYPE_FIELD_TYPE.freeze();
}
} }
public static class Builder extends AbstractFieldMapper.Builder<Builder, TypeFieldMapper> { public static class Builder extends AbstractFieldMapper.Builder<Builder, TypeFieldMapper> {
public Builder() { public Builder() {
super(Defaults.NAME); super(Defaults.NAME, new FieldType(Defaults.TYPE_FIELD_TYPE));
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
index = Defaults.INDEX;
store = Defaults.STORE;
omitNorms = Defaults.OMIT_NORMS;
indexOptions = Defaults.INDEX_OPTIONS;
} }
@Override @Override
public TypeFieldMapper build(BuilderContext context) { public TypeFieldMapper build(BuilderContext context) {
return new TypeFieldMapper(name, indexName, index, store, termVector, boost, omitNorms, indexOptions); return new TypeFieldMapper(name, indexName, boost, fieldType);
} }
} }
@ -94,23 +97,21 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
} }
protected TypeFieldMapper(String name, String indexName) { protected TypeFieldMapper(String name, String indexName) {
this(name, indexName, Defaults.INDEX, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.BOOST, this(name, indexName, Defaults.BOOST, new FieldType(Defaults.TYPE_FIELD_TYPE));
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS);
} }
public TypeFieldMapper(String name, String indexName, Field.Index index, Field.Store store, Field.TermVector termVector, public TypeFieldMapper(String name, String indexName, float boost, FieldType fieldType) {
float boost, boolean omitNorms, IndexOptions indexOptions) { super(new Names(name, indexName, indexName, name), boost, fieldType, Lucene.KEYWORD_ANALYZER,
super(new Names(name, indexName, indexName, name), index, store, termVector, boost, omitNorms, indexOptions, Lucene.KEYWORD_ANALYZER,
Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER);
} }
public String value(Document document) { public String value(Document document) {
Fieldable field = document.getFieldable(names.indexName()); Field field = (Field) document.getField(names.indexName());
return field == null ? null : value(field); return field == null ? null : value(field);
} }
@Override @Override
public String value(Fieldable field) { public String value(Field field) {
return field.stringValue(); return field.stringValue();
} }
@ -120,7 +121,7 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return value(field); return value(field);
} }
@ -135,8 +136,8 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
@Override @Override
public Filter fieldFilter(String value, @Nullable QueryParseContext context) { public Filter fieldFilter(String value, @Nullable QueryParseContext context) {
if (index == Field.Index.NO) { if (!indexed()) {
return new PrefixFilter(UidFieldMapper.TERM_FACTORY.createTerm(Uid.typePrefix(value))); return new PrefixFilter(new Term(UidFieldMapper.NAME, Uid.typePrefix(value)));
} }
return new TermFilter(names().createIndexNameTerm(value)); return new TermFilter(names().createIndexNameTerm(value));
} }
@ -176,10 +177,10 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
@Override @Override
protected Field parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
if (index == Field.Index.NO && store == Field.Store.NO) { if (!indexed() && !stored()) {
return null; return null;
} }
return new Field(names.indexName(), false, context.type(), store, index, termVector); return new Field(names.indexName(), context.type(), fieldType);
} }
@Override @Override
@ -190,15 +191,15 @@ public class TypeFieldMapper extends AbstractFieldMapper<String> implements Inte
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all are defaults, no sense to write it at all // if all are defaults, no sense to write it at all
if (store == Defaults.STORE && index == Defaults.INDEX) { if (stored() == Defaults.TYPE_FIELD_TYPE.stored() && indexed() == Defaults.TYPE_FIELD_TYPE.indexed()) {
return builder; return builder;
} }
builder.startObject(CONTENT_TYPE); builder.startObject(CONTENT_TYPE);
if (store != Defaults.STORE) { if (stored() != Defaults.TYPE_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (index != Defaults.INDEX) { if (indexed() != Defaults.TYPE_FIELD_TYPE.indexed()) {
builder.field("index", index.name().toLowerCase()); builder.field("index", indexed());
} }
builder.endObject(); builder.endObject();
return builder; return builder;

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.mapper.internal; package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
@ -47,9 +47,17 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
public static class Defaults extends AbstractFieldMapper.Defaults { public static class Defaults extends AbstractFieldMapper.Defaults {
public static final String NAME = UidFieldMapper.NAME; public static final String NAME = UidFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final boolean OMIT_NORMS = true; public static final FieldType UID_FIELD_TYPE = new FieldType(AbstractFieldMapper.Defaults.FIELD_TYPE);
public static final FieldInfo.IndexOptions INDEX_OPTIONS = FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS; // we store payload (otherwise, we really need just docs)
static {
UID_FIELD_TYPE.setIndexed(true);
UID_FIELD_TYPE.setTokenized(false);
UID_FIELD_TYPE.setStored(true);
UID_FIELD_TYPE.setOmitNorms(true);
UID_FIELD_TYPE.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); // we store payload (otherwise, we really need just docs)
UID_FIELD_TYPE.freeze();
}
} }
public static class Builder extends Mapper.Builder<Builder, UidFieldMapper> { public static class Builder extends Mapper.Builder<Builder, UidFieldMapper> {
@ -90,8 +98,8 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
} }
protected UidFieldMapper(String name, String indexName) { protected UidFieldMapper(String name, String indexName) {
super(new Names(name, indexName, indexName, name), Defaults.INDEX, Field.Store.YES, Defaults.TERM_VECTOR, Defaults.BOOST, super(new Names(name, indexName, indexName, name), Defaults.BOOST, new FieldType(Defaults.UID_FIELD_TYPE),
Defaults.OMIT_NORMS, Defaults.INDEX_OPTIONS, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER); Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
} }
@Override @Override
@ -115,7 +123,7 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
// since we did not have the uid in the pre phase, we did not add it automatically to the nested docs // since we did not have the uid in the pre phase, we did not add it automatically to the nested docs
// as they were created we need to make sure we add it to all the nested docs... // as they were created we need to make sure we add it to all the nested docs...
if (context.docs().size() > 1) { if (context.docs().size() > 1) {
UidField uidField = (UidField) context.rootDoc().getFieldable(UidFieldMapper.NAME); UidField uidField = (UidField) context.rootDoc().getField(UidFieldMapper.NAME);
assert uidField != null; assert uidField != null;
// we need to go over the docs and add it... // we need to go over the docs and add it...
for (int i = 1; i < context.docs().size(); i++) { for (int i = 1; i < context.docs().size(); i++) {
@ -141,7 +149,7 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
} }
@Override @Override
protected Fieldable parseCreateField(ParseContext context) throws IOException { protected Field parseCreateField(ParseContext context) throws IOException {
context.uid(Uid.createUid(context.stringBuilder(), context.type(), context.id())); context.uid(Uid.createUid(context.stringBuilder(), context.type(), context.id()));
// so, caching uid stream and field is fine // so, caching uid stream and field is fine
// since we don't do any mapping parsing without immediate indexing // since we don't do any mapping parsing without immediate indexing
@ -152,7 +160,7 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
} }
@Override @Override
public Uid value(Fieldable field) { public Uid value(Field field) {
return Uid.createUid(field.stringValue()); return Uid.createUid(field.stringValue());
} }
@ -162,7 +170,7 @@ public class UidFieldMapper extends AbstractFieldMapper<Uid> implements Internal
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
return field.stringValue(); return field.stringValue();
} }

View File

@ -21,9 +21,11 @@ package org.elasticsearch.index.mapper.ip;
import org.apache.lucene.analysis.NumericTokenStream; import org.apache.lucene.analysis.NumericTokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Explicit;
@ -86,6 +88,12 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
public static class Defaults extends NumberFieldMapper.Defaults { public static class Defaults extends NumberFieldMapper.Defaults {
public static final String NULL_VALUE = null; public static final String NULL_VALUE = null;
public static final FieldType IP_FIELD_TYPE = new FieldType(NumberFieldMapper.Defaults.NUMBER_FIELD_TYPE);
static {
IP_FIELD_TYPE.freeze();
}
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, IpFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, IpFieldMapper> {
@ -93,7 +101,7 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
protected String nullValue = Defaults.NULL_VALUE; protected String nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name); super(name, new FieldType(Defaults.IP_FIELD_TYPE));
builder = this; builder = this;
} }
@ -104,8 +112,9 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
public IpFieldMapper build(BuilderContext context) { public IpFieldMapper build(BuilderContext context) {
fieldType.setOmitNorms(fieldType.omitNorms() || boost != 1.0f);
IpFieldMapper fieldMapper = new IpFieldMapper(buildNames(context), IpFieldMapper fieldMapper = new IpFieldMapper(buildNames(context),
precisionStep, index, store, boost, omitNorms, indexOptions, nullValue, ignoreMalformed(context)); precisionStep, boost, fieldType, nullValue, ignoreMalformed(context));
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
} }
@ -130,10 +139,9 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
private String nullValue; private String nullValue;
protected IpFieldMapper(Names names, int precisionStep, protected IpFieldMapper(Names names, int precisionStep,
Field.Index index, Field.Store store, float boost, FieldType fieldType,
float boost, boolean omitNorms, IndexOptions indexOptions,
String nullValue, Explicit<Boolean> ignoreMalformed) { String nullValue, Explicit<Boolean> ignoreMalformed) {
super(names, precisionStep, null, index, store, boost, omitNorms, indexOptions, super(names, precisionStep, null, boost, fieldType,
ignoreMalformed, new NamedAnalyzer("_ip/" + precisionStep, new NumericIpAnalyzer(precisionStep)), ignoreMalformed, new NamedAnalyzer("_ip/" + precisionStep, new NumericIpAnalyzer(precisionStep)),
new NamedAnalyzer("_ip/max", new NumericIpAnalyzer(Integer.MAX_VALUE))); new NamedAnalyzer("_ip/max", new NumericIpAnalyzer(Integer.MAX_VALUE)));
this.nullValue = nullValue; this.nullValue = nullValue;
@ -145,8 +153,8 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Long value(Fieldable field) { public Long value(Field field) {
byte[] value = field.getBinaryValue(); byte[] value = field.binaryValue().bytes;
if (value == null) { if (value == null) {
return null; return null;
} }
@ -159,15 +167,15 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
/** /**
* IPs should return as a string, delegates to {@link #valueAsString(org.apache.lucene.document.Fieldable)}. * IPs should return as a string.
*/ */
@Override @Override
public Object valueForSearch(Fieldable field) { public Object valueForSearch(Field field) {
return valueAsString(field); return valueAsString(field);
} }
@Override @Override
public String valueAsString(Fieldable field) { public String valueAsString(Field field) {
Long value = value(field); Long value = value(field);
if (value == null) { if (value == null) {
return null; return null;
@ -177,7 +185,9 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
public String indexedValue(String value) { public String indexedValue(String value) {
return NumericUtils.longToPrefixCoded(ipToLong(value)); BytesRef bytesRef = new BytesRef();
NumericUtils.longToPrefixCoded(ipToLong(value), precisionStep(), bytesRef);
return bytesRef.utf8ToString();
} }
@Override @Override
@ -241,7 +251,7 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
protected Fieldable innerParseCreateField(ParseContext context) throws IOException { protected Field innerParseCreateField(ParseContext context) throws IOException {
String ipAsString; String ipAsString;
if (context.externalValueSet()) { if (context.externalValueSet()) {
ipAsString = (String) context.externalValue(); ipAsString = (String) context.externalValue();
@ -264,7 +274,7 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
final long value = ipToLong(ipAsString); final long value = ipToLong(ipAsString);
return new LongFieldMapper.CustomLongNumericField(this, value); return new LongFieldMapper.CustomLongNumericField(this, value, fieldType);
} }
@Override @Override
@ -291,20 +301,30 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
@Override @Override
protected void doXContentBody(XContentBuilder builder) throws IOException { protected void doXContentBody(XContentBuilder builder) throws IOException {
super.doXContentBody(builder); super.doXContentBody(builder);
if (index != Defaults.INDEX) { if (indexed() != Defaults.IP_FIELD_TYPE.indexed() ||
builder.field("index", index.name().toLowerCase()); analyzed() != Defaults.IP_FIELD_TYPE.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed(), analyzed()));
} }
if (store != Defaults.STORE) { if (stored() != Defaults.IP_FIELD_TYPE.stored()) {
builder.field("store", store.name().toLowerCase()); builder.field("store", stored());
} }
if (termVector != Defaults.TERM_VECTOR) { if (storeTermVectors() != Defaults.IP_FIELD_TYPE.storeTermVectors()) {
builder.field("term_vector", termVector.name().toLowerCase()); builder.field("store_term_vector", storeTermVectors());
} }
if (omitNorms != Defaults.OMIT_NORMS) { if (storeTermVectorOffsets() != Defaults.IP_FIELD_TYPE.storeTermVectorOffsets()) {
builder.field("omit_norms", omitNorms); builder.field("store_term_vector_offsets", storeTermVectorOffsets());
} }
if (indexOptions != Defaults.INDEX_OPTIONS) { if (storeTermVectorPositions() != Defaults.IP_FIELD_TYPE.storeTermVectorPositions()) {
builder.field("index_options", indexOptionToString(indexOptions)); builder.field("store_term_vector_positions", storeTermVectorPositions());
}
if (storeTermVectorPayloads() != Defaults.IP_FIELD_TYPE.storeTermVectorPayloads()) {
builder.field("store_term_vector_payloads", storeTermVectorPayloads());
}
if (omitNorms() != Defaults.IP_FIELD_TYPE.omitNorms()) {
builder.field("omit_norms", omitNorms());
}
if (indexOptions() != Defaults.IP_FIELD_TYPE.indexOptions()) {
builder.field("index_options", indexOptionToString(indexOptions()));
} }
if (precisionStep != Defaults.PRECISION_STEP) { if (precisionStep != Defaults.PRECISION_STEP) {
builder.field("precision_step", precisionStep); builder.field("precision_step", precisionStep);

View File

@ -22,7 +22,8 @@ package org.elasticsearch.index.mapper.object;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -304,7 +305,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
this.mappers = copyOf(mappers); this.mappers = copyOf(mappers);
} }
this.nestedTypePath = "__" + fullPath; this.nestedTypePath = "__" + fullPath;
this.nestedTypeFilter = new TermFilter(TypeFieldMapper.TERM_FACTORY.createTerm(nestedTypePath)); this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePath));
} }
@Override @Override
@ -412,7 +413,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
if (nested.isNested()) { if (nested.isNested()) {
Document nestedDoc = new Document(); Document nestedDoc = new Document();
// pre add the uid field if possible (id was already provided) // pre add the uid field if possible (id was already provided)
Fieldable uidField = context.doc().getFieldable(UidFieldMapper.NAME); Field uidField = (Field) context.doc().getField(UidFieldMapper.NAME);
if (uidField != null) { if (uidField != null) {
// we don't need to add it as a full uid field in nested docs, since we don't need versioning // we don't need to add it as a full uid field in nested docs, since we don't need versioning
// we also rely on this for UidField#loadVersion // we also rely on this for UidField#loadVersion
@ -465,7 +466,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
if (nested.isNested()) { if (nested.isNested()) {
Document nestedDoc = context.switchDoc(restoreDoc); Document nestedDoc = context.switchDoc(restoreDoc);
if (nested.isIncludeInParent()) { if (nested.isIncludeInParent()) {
for (Fieldable field : nestedDoc.getFields()) { for (IndexableField field : nestedDoc.getFields()) {
if (field.name().equals(UidFieldMapper.NAME) || field.name().equals(TypeFieldMapper.NAME)) { if (field.name().equals(UidFieldMapper.NAME) || field.name().equals(TypeFieldMapper.NAME)) {
continue; continue;
} else { } else {
@ -476,7 +477,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
if (nested.isIncludeInRoot()) { if (nested.isIncludeInRoot()) {
// don't add it twice, if its included in parent, and we are handling the master doc... // don't add it twice, if its included in parent, and we are handling the master doc...
if (!(nested.isIncludeInParent() && context.doc() == context.rootDoc())) { if (!(nested.isIncludeInParent() && context.doc() == context.rootDoc())) {
for (Fieldable field : nestedDoc.getFields()) { for (IndexableField field : nestedDoc.getFields()) {
if (field.name().equals(UidFieldMapper.NAME) || field.name().equals(TypeFieldMapper.NAME)) { if (field.name().equals(UidFieldMapper.NAME) || field.name().equals(TypeFieldMapper.NAME)) {
continue; continue;
} else { } else {