Issue #2121 Added limit parameter for string type.
This commit is contained in:
parent
68c76e6738
commit
41b3a454cf
|
@ -49,6 +49,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
// NOTE, when adding defaults here, make sure you add them in the builder
|
||||
public static final String NULL_VALUE = null;
|
||||
public static final int POSITION_OFFSET_GAP = 0;
|
||||
public static final int DEFAULT_LIMIT = -1;
|
||||
}
|
||||
|
||||
public static class Builder extends AbstractFieldMapper.OpenBuilder<Builder, StringFieldMapper> {
|
||||
|
@ -59,6 +60,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
|
||||
protected NamedAnalyzer searchQuotedAnalyzer;
|
||||
|
||||
protected int limit = Defaults.DEFAULT_LIMIT;
|
||||
|
||||
public Builder(String name) {
|
||||
super(name);
|
||||
builder = this;
|
||||
|
@ -94,6 +97,11 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
return builder;
|
||||
}
|
||||
|
||||
public Builder limit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringFieldMapper build(BuilderContext context) {
|
||||
if (positionOffsetGap > 0) {
|
||||
|
@ -103,7 +111,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
}
|
||||
StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
|
||||
index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue,
|
||||
indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap);
|
||||
indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, limit);
|
||||
fieldMapper.includeInAll(includeInAll);
|
||||
return fieldMapper;
|
||||
}
|
||||
|
@ -138,6 +146,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
if (builder.searchQuotedAnalyzer == null) {
|
||||
builder.searchQuotedAnalyzer = parserContext.analysisService().defaultSearchQuoteAnalyzer();
|
||||
}
|
||||
} else if (propName.equals("limit")) {
|
||||
builder.limit(XContentMapValues.nodeIntegerValue(propNode, -1));
|
||||
}
|
||||
}
|
||||
return builder;
|
||||
|
@ -152,19 +162,24 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
|
||||
private NamedAnalyzer searchQuotedAnalyzer;
|
||||
|
||||
private int limit;
|
||||
|
||||
protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
|
||||
float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
|
||||
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
|
||||
this(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer, searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP);
|
||||
this(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer,
|
||||
searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP, Defaults.DEFAULT_LIMIT);
|
||||
}
|
||||
|
||||
protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
|
||||
float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
|
||||
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap) {
|
||||
String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer,
|
||||
NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap, int limit) {
|
||||
super(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, indexAnalyzer, searchAnalyzer);
|
||||
this.nullValue = nullValue;
|
||||
this.positionOffsetGap = positionOffsetGap;
|
||||
this.searchQuotedAnalyzer = searchQuotedAnalyzer != null ? searchQuotedAnalyzer : this.searchAnalyzer;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -254,6 +269,9 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (limit > 0 && value.length() > limit) {
|
||||
return null;
|
||||
}
|
||||
if (context.includeInAll(includeInAll, this)) {
|
||||
context.allEntries().addText(names.fullName(), value, boost);
|
||||
}
|
||||
|
@ -313,5 +331,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
|
|||
if (searchQuotedAnalyzer != null && searchAnalyzer != searchQuotedAnalyzer) {
|
||||
builder.field("search_quote_analyzer", searchQuotedAnalyzer.name());
|
||||
}
|
||||
if (limit != Defaults.DEFAULT_LIMIT) {
|
||||
builder.field("limit", limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,4 +120,19 @@ public class SimpleMapperTests {
|
|||
DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
|
||||
assertThat((String) builtDocMapper.meta().get("param1"), equalTo("value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLimit() throws Exception {
|
||||
String mapping = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test-mapping.json");
|
||||
DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
|
||||
String builtMapping = docMapper.mappingSource().string();
|
||||
|
||||
// reparse it
|
||||
DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
|
||||
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
|
||||
Document doc = builtDocMapper.parse(json).rootDoc();
|
||||
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
|
||||
assertThat(doc.getValues("cats").length, equalTo(1));
|
||||
assertThat(doc.getValues("cats")[0], equalTo("felix"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,6 +71,11 @@
|
|||
type:"string",
|
||||
index_name:"dog"
|
||||
},
|
||||
cats:{
|
||||
type:"string",
|
||||
index_name:"cats",
|
||||
limit:10
|
||||
},
|
||||
complex:{
|
||||
type:"object",
|
||||
properties:{
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
birthDate:"1977-11-15",
|
||||
nerd:true,
|
||||
dogs:["buck", "mia"],
|
||||
cats:["name to long for a cat", "felix"],
|
||||
complex:[
|
||||
{
|
||||
value1:"value1"
|
||||
|
|
Loading…
Reference in New Issue