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;
}
@Override public Builder indexName(String indexName) {
return super.indexName(indexName);
}
@Override public JsonBinaryFieldMapper build(BuilderContext context) {
return new JsonBinaryFieldMapper(name, buildIndexName(context), buildFullName(context));
}

View File

@ -52,6 +52,30 @@ public class JsonBooleanFieldMapper extends JsonFieldMapper<Boolean> {
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) {
return new JsonBooleanFieldMapper(name, buildIndexName(context), buildFullName(context), index, store,
termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue);

View File

@ -129,17 +129,17 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
}
private JsonUidFieldMapper.Builder parseUidField(ObjectNode uidNode) {
String name = uidNode.get("name") == null ? JsonUidFieldMapper.Defaults.NAME : uidNode.get("name").getTextValue();
JsonUidFieldMapper.Builder builder = uid(name);
for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = uidNode.getFields(); fieldsIt.hasNext();) {
Map.Entry<String, JsonNode> entry = fieldsIt.next();
String fieldName = entry.getKey();
JsonNode fieldNode = entry.getValue();
if ("indexName".equals(fieldName)) {
builder.indexName(fieldNode.getTextValue());
}
}
// String name = uidNode.get("name") == null ? JsonUidFieldMapper.Defaults.NAME : uidNode.get("name").getTextValue();
JsonUidFieldMapper.Builder builder = uid();
// for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = uidNode.getFields(); fieldsIt.hasNext();) {
// Map.Entry<String, JsonNode> entry = fieldsIt.next();
// String fieldName = entry.getKey();
// JsonNode fieldNode = entry.getValue();
//
// if ("indexName".equals(fieldName)) {
// builder.indexName(fieldNode.getTextValue());
// }
// }
return builder;
}
@ -159,23 +159,23 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
}
private JsonTypeFieldMapper.Builder parseTypeField(ObjectNode typeNode) {
String name = typeNode.get("name") == null ? JsonTypeFieldMapper.Defaults.NAME : typeNode.get("name").getTextValue();
JsonTypeFieldMapper.Builder builder = type(name);
parseJsonField(builder, name, typeNode);
// String name = typeNode.get("name") == null ? JsonTypeFieldMapper.Defaults.NAME : typeNode.get("name").getTextValue();
JsonTypeFieldMapper.Builder builder = type();
parseJsonField(builder, builder.name, typeNode);
return builder;
}
private JsonIdFieldMapper.Builder parseIdField(ObjectNode idNode) {
String name = idNode.get("name") == null ? JsonIdFieldMapper.Defaults.NAME : idNode.get("name").getTextValue();
JsonIdFieldMapper.Builder builder = id(name);
parseJsonField(builder, name, idNode);
// String name = idNode.get("name") == null ? JsonIdFieldMapper.Defaults.NAME : idNode.get("name").getTextValue();
JsonIdFieldMapper.Builder builder = id();
parseJsonField(builder, builder.name, idNode);
return builder;
}
private JsonSourceFieldMapper.Builder parseSourceField(ObjectNode sourceNode) {
String name = sourceNode.get("name") == null ? JsonSourceFieldMapper.Defaults.NAME : sourceNode.get("name").getTextValue();
JsonSourceFieldMapper.Builder builder = source(name);
// String name = sourceNode.get("name") == null ? JsonSourceFieldMapper.Defaults.NAME : sourceNode.get("name").getTextValue();
JsonSourceFieldMapper.Builder builder = source();
for (Iterator<Map.Entry<String, JsonNode>> fieldsIt = sourceNode.getFields(); fieldsIt.hasNext();) {
Map.Entry<String, JsonNode> entry = fieldsIt.next();
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 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> {
protected Field.Index index = Defaults.INDEX;
@ -64,47 +107,47 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
protected Analyzer searchAnalyzer;
public Builder(String name) {
protected Builder(String name) {
super(name);
indexName = name;
}
public T index(Field.Index index) {
protected T index(Field.Index index) {
this.index = index;
return builder;
}
public T store(Field.Store store) {
protected T store(Field.Store store) {
this.store = store;
return builder;
}
public T termVector(Field.TermVector termVector) {
protected T termVector(Field.TermVector termVector) {
this.termVector = termVector;
return builder;
}
public T boost(float boost) {
protected T boost(float boost) {
this.boost = boost;
return builder;
}
public T omitNorms(boolean omitNorms) {
protected T omitNorms(boolean omitNorms) {
this.omitNorms = omitNorms;
return builder;
}
public T omitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
protected T omitTermFreqAndPositions(boolean omitTermFreqAndPositions) {
this.omitTermFreqAndPositions = omitTermFreqAndPositions;
return builder;
}
public T indexName(String indexName) {
protected T indexName(String indexName) {
this.indexName = indexName;
return builder;
}
public T indexAnalyzer(Analyzer indexAnalyzer) {
protected T indexAnalyzer(Analyzer indexAnalyzer) {
this.indexAnalyzer = indexAnalyzer;
if (this.searchAnalyzer == null) {
this.searchAnalyzer = indexAnalyzer;
@ -112,7 +155,7 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
return builder;
}
public T searchAnalyzer(Analyzer searchAnalyzer) {
protected T searchAnalyzer(Analyzer searchAnalyzer) {
this.searchAnalyzer = searchAnalyzer;
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 Builder(String name) {
super(name);
public Builder() {
super(Defaults.NAME);
indexName = Defaults.INDEX_NAME;
store = Defaults.STORE;
index = Defaults.INDEX;

View File

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

View File

@ -32,20 +32,20 @@ public final class JsonMapperBuilders {
return new JsonDocumentMapper.Builder(objectBuilder);
}
public static JsonSourceFieldMapper.Builder source(String name) {
return new JsonSourceFieldMapper.Builder(name);
public static JsonSourceFieldMapper.Builder source() {
return new JsonSourceFieldMapper.Builder();
}
public static JsonIdFieldMapper.Builder id(String name) {
return new JsonIdFieldMapper.Builder(name);
public static JsonIdFieldMapper.Builder id() {
return new JsonIdFieldMapper.Builder();
}
public static JsonUidFieldMapper.Builder uid(String name) {
return new JsonUidFieldMapper.Builder(name);
public static JsonUidFieldMapper.Builder uid() {
return new JsonUidFieldMapper.Builder();
}
public static JsonTypeFieldMapper.Builder type(String name) {
return new JsonTypeFieldMapper.Builder(name);
public static JsonTypeFieldMapper.Builder type() {
return new JsonTypeFieldMapper.Builder();
}
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;
}
@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) {
this.precisionStep = precisionStep;
return builder;

View File

@ -52,14 +52,15 @@ public class JsonSourceFieldMapper extends JsonFieldMapper<String> implements So
private int compressionThreshold = Defaults.NO_COMPRESSION;
public Builder(String name) {
super(name);
public Builder() {
super(Defaults.NAME);
}
public Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}
// source is always enabled for now
// public Builder enabled(boolean enabled) {
// this.enabled = enabled;
// return this;
// }
public Builder compressor(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 class Builder extends JsonFieldMapper.Builder<Builder, JsonStringFieldMapper> {
public static class Builder extends JsonFieldMapper.OpenBuilder<Builder, JsonStringFieldMapper> {
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 Builder(String name) {
super(name);
public Builder() {
super(Defaults.NAME);
indexName = Defaults.INDEX_NAME;
index = Defaults.INDEX;
store = Defaults.STORE;

View File

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

View File

@ -43,7 +43,7 @@ public class SimpleJsonMapperTests {
JsonDocumentMapper docMapper = doc(
object("person")
.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");
Document doc = docMapper.parse("person", "1", json).doc();