better encapsulation of mapping builders

This commit is contained in:
kimchy 2010-02-17 18:19:52 +02:00
parent 872781536d
commit 0545423837
13 changed files with 136 additions and 57 deletions

View File

@ -37,6 +37,10 @@ public class JsonBinaryFieldMapper extends JsonFieldMapper<byte[]> {
builder = this; builder = this;
} }
@Override public Builder indexName(String indexName) {
return super.indexName(indexName);
}
@Override public JsonBinaryFieldMapper build(BuilderContext context) { @Override public JsonBinaryFieldMapper build(BuilderContext context) {
return new JsonBinaryFieldMapper(name, buildIndexName(context), buildFullName(context)); return new JsonBinaryFieldMapper(name, buildIndexName(context), buildFullName(context));
} }

View File

@ -52,6 +52,30 @@ public class JsonBooleanFieldMapper extends JsonFieldMapper<Boolean> {
return this; return this;
} }
@Override public Builder index(Field.Index index) {
return super.index(index);
}
@Override public Builder store(Field.Store store) {
return super.store(store);
}
@Override public Builder termVector(Field.TermVector termVector) {
return super.termVector(termVector);
}
@Override public Builder boost(float boost) {
return super.boost(boost);
}
@Override public Builder indexName(String indexName) {
return super.indexName(indexName);
}
@Override public Builder omitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
return super.omitTermFreqAndPositions(omitTermFreqAndPositions);
}
@Override public JsonBooleanFieldMapper build(BuilderContext context) { @Override public JsonBooleanFieldMapper build(BuilderContext context) {
return new JsonBooleanFieldMapper(name, buildIndexName(context), buildFullName(context), index, store, return new JsonBooleanFieldMapper(name, buildIndexName(context), buildFullName(context), index, store,
termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue); termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue);

View File

@ -129,17 +129,17 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
} }
private JsonUidFieldMapper.Builder parseUidField(ObjectNode uidNode) { private JsonUidFieldMapper.Builder parseUidField(ObjectNode uidNode) {
String name = uidNode.get("name") == null ? JsonUidFieldMapper.Defaults.NAME : uidNode.get("name").getTextValue(); // String name = uidNode.get("name") == null ? JsonUidFieldMapper.Defaults.NAME : uidNode.get("name").getTextValue();
JsonUidFieldMapper.Builder builder = uid(name); JsonUidFieldMapper.Builder builder = uid();
for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = uidNode.getFields(); fieldsIt.hasNext();) { // for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = uidNode.getFields(); fieldsIt.hasNext();) {
Map.Entry<String, JsonNode> entry = fieldsIt.next(); // Map.Entry<String, JsonNode> entry = fieldsIt.next();
String fieldName = entry.getKey(); // String fieldName = entry.getKey();
JsonNode fieldNode = entry.getValue(); // JsonNode fieldNode = entry.getValue();
//
if ("indexName".equals(fieldName)) { // if ("indexName".equals(fieldName)) {
builder.indexName(fieldNode.getTextValue()); // builder.indexName(fieldNode.getTextValue());
} // }
} // }
return builder; return builder;
} }
@ -159,23 +159,23 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
} }
private JsonTypeFieldMapper.Builder parseTypeField(ObjectNode typeNode) { private JsonTypeFieldMapper.Builder parseTypeField(ObjectNode typeNode) {
String name = typeNode.get("name") == null ? JsonTypeFieldMapper.Defaults.NAME : typeNode.get("name").getTextValue(); // String name = typeNode.get("name") == null ? JsonTypeFieldMapper.Defaults.NAME : typeNode.get("name").getTextValue();
JsonTypeFieldMapper.Builder builder = type(name); JsonTypeFieldMapper.Builder builder = type();
parseJsonField(builder, name, typeNode); parseJsonField(builder, builder.name, typeNode);
return builder; return builder;
} }
private JsonIdFieldMapper.Builder parseIdField(ObjectNode idNode) { private JsonIdFieldMapper.Builder parseIdField(ObjectNode idNode) {
String name = idNode.get("name") == null ? JsonIdFieldMapper.Defaults.NAME : idNode.get("name").getTextValue(); // String name = idNode.get("name") == null ? JsonIdFieldMapper.Defaults.NAME : idNode.get("name").getTextValue();
JsonIdFieldMapper.Builder builder = id(name); JsonIdFieldMapper.Builder builder = id();
parseJsonField(builder, name, idNode); parseJsonField(builder, builder.name, idNode);
return builder; return builder;
} }
private JsonSourceFieldMapper.Builder parseSourceField(ObjectNode sourceNode) { private JsonSourceFieldMapper.Builder parseSourceField(ObjectNode sourceNode) {
String name = sourceNode.get("name") == null ? JsonSourceFieldMapper.Defaults.NAME : sourceNode.get("name").getTextValue(); // String name = sourceNode.get("name") == null ? JsonSourceFieldMapper.Defaults.NAME : sourceNode.get("name").getTextValue();
JsonSourceFieldMapper.Builder builder = source(name); JsonSourceFieldMapper.Builder builder = source();
for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = sourceNode.getFields(); fieldsIt.hasNext();) { for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = sourceNode.getFields(); fieldsIt.hasNext();) {
Map.Entry<String, JsonNode> entry = fieldsIt.next(); Map.Entry<String, JsonNode> entry = fieldsIt.next();
String fieldName = entry.getKey(); String fieldName = entry.getKey();

View File

@ -44,6 +44,49 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
public static final boolean OMIT_TERM_FREQ_AND_POSITIONS = false; public static final boolean OMIT_TERM_FREQ_AND_POSITIONS = false;
} }
public abstract static class OpenBuilder<T extends Builder, Y extends JsonFieldMapper> extends JsonFieldMapper.Builder<T, Y> {
protected OpenBuilder(String name) {
super(name);
}
@Override public T index(Field.Index index) {
return super.index(index);
}
@Override public T store(Field.Store store) {
return super.store(store);
}
@Override public T termVector(Field.TermVector termVector) {
return super.termVector(termVector);
}
@Override public T boost(float boost) {
return super.boost(boost);
}
@Override public T omitNorms(boolean omitNorms) {
return super.omitNorms(omitNorms);
}
@Override public T omitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
return super.omitTermFreqAndPositions(omitTermFreqAndPositions);
}
@Override public T indexName(String indexName) {
return super.indexName(indexName);
}
@Override public T indexAnalyzer(Analyzer indexAnalyzer) {
return super.indexAnalyzer(indexAnalyzer);
}
@Override public T searchAnalyzer(Analyzer searchAnalyzer) {
return super.searchAnalyzer(searchAnalyzer);
}
}
public abstract static class Builder<T extends Builder, Y extends JsonFieldMapper> extends JsonMapper.Builder<T, Y> { public abstract static class Builder<T extends Builder, Y extends JsonFieldMapper> extends JsonMapper.Builder<T, Y> {
protected Field.Index index = Defaults.INDEX; protected Field.Index index = Defaults.INDEX;
@ -64,47 +107,47 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
protected Analyzer searchAnalyzer; protected Analyzer searchAnalyzer;
public Builder(String name) { protected Builder(String name) {
super(name); super(name);
indexName = name; indexName = name;
} }
public T index(Field.Index index) { protected T index(Field.Index index) {
this.index = index; this.index = index;
return builder; return builder;
} }
public T store(Field.Store store) { protected T store(Field.Store store) {
this.store = store; this.store = store;
return builder; return builder;
} }
public T termVector(Field.TermVector termVector) { protected T termVector(Field.TermVector termVector) {
this.termVector = termVector; this.termVector = termVector;
return builder; return builder;
} }
public T boost(float boost) { protected T boost(float boost) {
this.boost = boost; this.boost = boost;
return builder; return builder;
} }
public T omitNorms(boolean omitNorms) { protected T omitNorms(boolean omitNorms) {
this.omitNorms = omitNorms; this.omitNorms = omitNorms;
return builder; return builder;
} }
public T omitTermFreqAndPositions(boolean omitTermFreqAndPositions) { protected T omitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
this.omitTermFreqAndPositions = omitTermFreqAndPositions; this.omitTermFreqAndPositions = omitTermFreqAndPositions;
return builder; return builder;
} }
public T indexName(String indexName) { protected T indexName(String indexName) {
this.indexName = indexName; this.indexName = indexName;
return builder; return builder;
} }
public T indexAnalyzer(Analyzer indexAnalyzer) { protected T indexAnalyzer(Analyzer indexAnalyzer) {
this.indexAnalyzer = indexAnalyzer; this.indexAnalyzer = indexAnalyzer;
if (this.searchAnalyzer == null) { if (this.searchAnalyzer == null) {
this.searchAnalyzer = indexAnalyzer; this.searchAnalyzer = indexAnalyzer;
@ -112,7 +155,7 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
return builder; return builder;
} }
public T searchAnalyzer(Analyzer searchAnalyzer) { protected T searchAnalyzer(Analyzer searchAnalyzer) {
this.searchAnalyzer = searchAnalyzer; this.searchAnalyzer = searchAnalyzer;
return builder; return builder;
} }

View File

@ -45,8 +45,8 @@ public class JsonIdFieldMapper extends JsonFieldMapper<String> implements IdFiel
public static class Builder extends JsonFieldMapper.Builder<Builder, JsonIdFieldMapper> { public static class Builder extends JsonFieldMapper.Builder<Builder, JsonIdFieldMapper> {
public Builder(String name) { public Builder() {
super(name); super(Defaults.NAME);
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
store = Defaults.STORE; store = Defaults.STORE;
index = Defaults.INDEX; index = Defaults.INDEX;

View File

@ -51,7 +51,7 @@ public interface JsonMapper {
protected T builder; protected T builder;
public Builder(String name) { protected Builder(String name) {
this.name = name; this.name = name;
} }

View File

@ -32,20 +32,20 @@ public final class JsonMapperBuilders {
return new JsonDocumentMapper.Builder(objectBuilder); return new JsonDocumentMapper.Builder(objectBuilder);
} }
public static JsonSourceFieldMapper.Builder source(String name) { public static JsonSourceFieldMapper.Builder source() {
return new JsonSourceFieldMapper.Builder(name); return new JsonSourceFieldMapper.Builder();
} }
public static JsonIdFieldMapper.Builder id(String name) { public static JsonIdFieldMapper.Builder id() {
return new JsonIdFieldMapper.Builder(name); return new JsonIdFieldMapper.Builder();
} }
public static JsonUidFieldMapper.Builder uid(String name) { public static JsonUidFieldMapper.Builder uid() {
return new JsonUidFieldMapper.Builder(name); return new JsonUidFieldMapper.Builder();
} }
public static JsonTypeFieldMapper.Builder type(String name) { public static JsonTypeFieldMapper.Builder type() {
return new JsonTypeFieldMapper.Builder(name); return new JsonTypeFieldMapper.Builder();
} }
public static JsonBoostFieldMapper.Builder boost(String name) { public static JsonBoostFieldMapper.Builder boost(String name) {

View File

@ -54,6 +54,18 @@ public abstract class JsonNumberFieldMapper<T extends Number> extends JsonFieldM
this.omitTermFreqAndPositions = Defaults.OMIT_TERM_FREQ_AND_POSITIONS; this.omitTermFreqAndPositions = Defaults.OMIT_TERM_FREQ_AND_POSITIONS;
} }
@Override public T store(Field.Store store) {
return super.store(store);
}
@Override protected T boost(float boost) {
return super.boost(boost);
}
@Override protected T indexName(String indexName) {
return super.indexName(indexName);
}
public T precisionStep(int precisionStep) { public T precisionStep(int precisionStep) {
this.precisionStep = precisionStep; this.precisionStep = precisionStep;
return builder; return builder;

View File

@ -52,14 +52,15 @@ public class JsonSourceFieldMapper extends JsonFieldMapper<String> implements So
private int compressionThreshold = Defaults.NO_COMPRESSION; private int compressionThreshold = Defaults.NO_COMPRESSION;
public Builder(String name) { public Builder() {
super(name); super(Defaults.NAME);
} }
public Builder enabled(boolean enabled) { // source is always enabled for now
this.enabled = enabled; // public Builder enabled(boolean enabled) {
return this; // this.enabled = enabled;
} // return this;
// }
public Builder compressor(Compressor compressor) { public Builder compressor(Compressor compressor) {
this.compressor = compressor; this.compressor = compressor;

View File

@ -36,7 +36,7 @@ public class JsonStringFieldMapper extends JsonFieldMapper<String> {
public static final String NULL_VALUE = null; public static final String NULL_VALUE = null;
} }
public static class Builder extends JsonFieldMapper.Builder<Builder, JsonStringFieldMapper> { public static class Builder extends JsonFieldMapper.OpenBuilder<Builder, JsonStringFieldMapper> {
protected String nullValue = Defaults.NULL_VALUE; protected String nullValue = Defaults.NULL_VALUE;

View File

@ -44,8 +44,8 @@ public class JsonTypeFieldMapper extends JsonFieldMapper<String> implements Type
public static class Builder extends JsonFieldMapper.Builder<Builder, JsonTypeFieldMapper> { public static class Builder extends JsonFieldMapper.Builder<Builder, JsonTypeFieldMapper> {
public Builder(String name) { public Builder() {
super(name); super(Defaults.NAME);
indexName = Defaults.INDEX_NAME; indexName = Defaults.INDEX_NAME;
index = Defaults.INDEX; index = Defaults.INDEX;
store = Defaults.STORE; store = Defaults.STORE;

View File

@ -45,16 +45,11 @@ public class JsonUidFieldMapper extends JsonFieldMapper<Uid> implements UidField
protected String indexName; protected String indexName;
public Builder(String name) { public Builder() {
super(name); super(Defaults.NAME);
this.indexName = name; this.indexName = name;
} }
public Builder indexName(String indexName) {
this.indexName = indexName;
return this;
}
@Override public JsonUidFieldMapper build(BuilderContext context) { @Override public JsonUidFieldMapper build(BuilderContext context) {
return new JsonUidFieldMapper(name, indexName); return new JsonUidFieldMapper(name, indexName);
} }

View File

@ -43,7 +43,7 @@ public class SimpleJsonMapperTests {
JsonDocumentMapper docMapper = doc( JsonDocumentMapper docMapper = doc(
object("person") object("person")
.add(object("name").add(stringField("first").store(YES).index(Field.Index.NO))) .add(object("name").add(stringField("first").store(YES).index(Field.Index.NO)))
).sourceField(source("_source").compressionThreshold(0)).build(); ).sourceField(source().compressionThreshold(0)).build();
String json = copyToStringFromClasspath("/org/elasticsearch/index/mapper/json/simple/test1.json"); String json = copyToStringFromClasspath("/org/elasticsearch/index/mapper/json/simple/test1.json");
Document doc = docMapper.parse("person", "1", json).doc(); Document doc = docMapper.parse("person", "1", json).doc();