Configure FieldData using a hash not a string

Closes #2876
This commit is contained in:
Simon Willnauer 2013-04-09 15:50:44 +02:00
parent 374bbbfa7b
commit ae74a8dbb7
4 changed files with 32 additions and 21 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.fielddata.plain;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -52,16 +53,12 @@ public abstract class AbstractBytesIndexFieldData<FD extends AtomicFieldData.Wit
protected AbstractBytesIndexFieldData(Index index, Settings indexSettings, Names fieldNames, FieldDataType fieldDataType,
IndexFieldDataCache cache) {
super(index, indexSettings, fieldNames, fieldDataType, cache);
frequency = getPrefixSettings(fieldDataType.getSettings(), "filter.frequency.");
regex = getPrefixSettings(fieldDataType.getSettings(), "filter.regex.");
final Map<String, Settings> groups = fieldDataType.getSettings().getGroups("filter");
frequency = groups.get("frequency");
regex = groups.get("regex");
}
private final Settings getPrefixSettings(Settings settings, String prefix) {
Settings byPrefix = settings.getByPrefix(prefix);
return byPrefix.getAsMap().isEmpty() ? null : byPrefix;
}
@Override
public final boolean valuesOrdered() {
return true;

View File

@ -47,6 +47,7 @@ import org.elasticsearch.index.similarity.SimilarityProvider;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
@ -145,7 +146,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
return super.similarity(similarity);
}
public T fieldDataSettings(String settings) {
public T fieldDataSettings(Settings settings) {
return super.fieldDataSettings(settings);
}
}
@ -255,8 +256,8 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
return builder;
}
protected T fieldDataSettings(String settings) {
this.fieldDataSettings = ImmutableSettings.builder().loadFromDelimitedString(settings, ';').build();
protected T fieldDataSettings(Settings settings) {
this.fieldDataSettings = settings;
return builder;
}
@ -646,7 +647,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
}
if (customFieldDataSettings != null) {
builder.field("fielddata", customFieldDataSettings.toDelimitedString(';'));
builder.field("fielddata", (Map)customFieldDataSettings.getAsMap());
}
}

View File

@ -24,6 +24,9 @@ import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.loader.SettingsLoader;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.Mapper;
@ -118,7 +121,13 @@ public class TypeParsers {
} else if (propName.equals("similarity")) {
builder.similarity(parserContext.similarityLookupService().similarity(propNode.toString()));
} else if (propName.equals("fielddata")) {
builder.fieldDataSettings(propNode.toString());
final Settings settings;
if (propNode instanceof Map){
settings = ImmutableSettings.builder().put(SettingsLoader.Helper.loadNestedFromMap((Map<String, Object>)propNode)).build();
} else {
throw new ElasticSearchParseException("fielddata should be a hash but was of type: " + propNode.getClass());
}
builder.fieldDataSettings(settings);
}
}
}

View File

@ -475,36 +475,40 @@ public class SimpleFacetsTests extends AbstractNodesTests {
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("name_concrete")
.field("type", "string")
.field("fielddata","format=concrete_bytes")
.startObject("fielddata").field("format", "concrete_bytes").endObject()
.endObject()
.startObject("name_paged")
.field("type", "string")
.field("fielddata","format=paged_bytes")
.startObject("fielddata").field("format", "paged_bytes").endObject()
.endObject()
.startObject("name_fst")
.field("type", "string")
.field("fielddata","format=fst")
.startObject("fielddata").field("format", "fst").endObject()
.endObject()
.startObject("name_concrete_mv")
.field("type", "string")
.field("fielddata","format=concrete_bytes")
.startObject("fielddata").field("format", "concrete_bytes").endObject()
.endObject()
.startObject("name_paged_mv")
.field("type", "string")
.field("fielddata","format=paged_bytes")
.startObject("fielddata").field("format", "paged_bytes").endObject()
.endObject()
.startObject("name_fst_mv")
.field("type", "string")
.field("fielddata","format=fst")
.startObject("fielddata").field("format", "fst").endObject()
.endObject()
.startObject("filtered")
.field("type", "string")
.field("fielddata","format=fst;filter.regex.pattern=\\d{1,2}") // only 1 or 2 digits
.startObject("fielddata").field("format", "fst").startObject("filter")
.startObject("regex").field("pattern", "\\d{1,2}").endObject().endObject()
.endObject()
// only 1 or 2 digits
.endObject()
.startObject("filtered_mv")
.field("type", "string")
.field("fielddata","format=fst;filter.regex.pattern=\\d{1,2}") // only 1 or 2 digits
.endObject()
.startObject("fielddata").field("format", "fst").startObject("filter")
.startObject("regex").field("pattern", "\\d{1,2}").endObject().endObject()
.endObject()
.endObject().endObject().endObject())
.execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();