Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
Add ignore_above in ICUCollationKeywordFieldMapper. Close elastic#40413
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément Tourrière committed May 21, 2019
1 parent 65b6179 commit 0690016
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Expand Up @@ -73,6 +73,7 @@ public static class Defaults {
}

public static final String NULL_VALUE = null;
public static final int IGNORE_ABOVE = Integer.MAX_VALUE;
}

public static final class CollationFieldType extends StringFieldType {
Expand Down Expand Up @@ -237,6 +238,7 @@ public static class Builder extends FieldMapper.Builder<Builder, ICUCollationKey
private String variableTop = null;
private boolean hiraganaQuaternaryMode = false;
private String nullValue = Defaults.NULL_VALUE;
protected int ignoreAbove = Defaults.IGNORE_ABOVE;

public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE);
Expand All @@ -258,6 +260,14 @@ public Builder indexOptions(IndexOptions indexOptions) {
return super.indexOptions(indexOptions);
}

public Builder ignoreAbove(int ignoreAbove) {
if (ignoreAbove < 0) {
throw new IllegalArgumentException("[ignore_above] must be positive, got " + ignoreAbove);
}
this.ignoreAbove = ignoreAbove;
return this;
}

public String rules() {
return rules;
}
Expand Down Expand Up @@ -469,7 +479,7 @@ public ICUCollationKeywordFieldMapper build(BuilderContext context) {
setupFieldType(context);
return new ICUCollationKeywordFieldMapper(name, fieldType, defaultFieldType, context.indexSettings(),
multiFieldsBuilder.build(this, context), copyTo, rules, language, country, variant, strength, decomposition,
alternate, caseLevel, caseFirst, numeric, variableTop, hiraganaQuaternaryMode, collator);
alternate, caseLevel, caseFirst, numeric, variableTop, hiraganaQuaternaryMode, ignoreAbove, collator);
}
}

Expand Down Expand Up @@ -543,6 +553,10 @@ public static class TypeParser implements Mapper.TypeParser {
builder.hiraganaQuaternaryMode(XContentMapValues.nodeBooleanValue(fieldNode, false));
iterator.remove();
break;
case "ignore_above":
builder.ignoreAbove(XContentMapValues.nodeIntegerValue(fieldNode, -1));
iterator.remove();
break;
default:
break;
}
Expand All @@ -564,14 +578,16 @@ public static class TypeParser implements Mapper.TypeParser {
private final boolean numeric;
private final String variableTop;
private final boolean hiraganaQuaternaryMode;
private int ignoreAbove;
private final Collator collator;
private final BiFunction<String, BytesRef, Field> getDVField;

protected ICUCollationKeywordFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo, String rules, String language,
String country, String variant,
String strength, String decomposition, String alternate, boolean caseLevel, String caseFirst,
boolean numeric, String variableTop, boolean hiraganaQuaternaryMode, Collator collator) {
boolean numeric, String variableTop, boolean hiraganaQuaternaryMode,
int ignoreAbove, Collator collator) {
super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo);
assert collator.isFrozen();
this.rules = rules;
Expand All @@ -586,6 +602,7 @@ protected ICUCollationKeywordFieldMapper(String simpleName, MappedFieldType fiel
this.numeric = numeric;
this.variableTop = variableTop;
this.hiraganaQuaternaryMode = hiraganaQuaternaryMode;
this.ignoreAbove = ignoreAbove;
this.collator = collator;
if (indexCreatedVersion.onOrAfter(Version.V_5_6_0)) {
getDVField = SortedSetDocValuesField::new;
Expand Down Expand Up @@ -659,6 +676,10 @@ protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
conflicts.add("Cannot update hiragana_quaternary_mode setting for [" + CONTENT_TYPE + "]");
}

if (ignoreAbove != icuMergeWith.ignoreAbove) {
conflicts.add("Cannot update ignore_above settings for [" + CONTENT_TYPE + "]");
}

if (!conflicts.isEmpty()) {
throw new IllegalArgumentException("Can't merge because of conflicts: " + conflicts);
}
Expand Down Expand Up @@ -719,6 +740,10 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
if (includeDefaults || hiraganaQuaternaryMode) {
builder.field("hiragana_quaternary_mode", hiraganaQuaternaryMode);
}

if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) {
builder.field("ignore_above", ignoreAbove);
}
}

@Override
Expand All @@ -735,7 +760,7 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
}
}

if (value == null) {
if (value == null || value.length() > ignoreAbove) {
return;
}

Expand Down
Expand Up @@ -403,4 +403,36 @@ public void testUpdateCollator() throws IOException {
assertEquals("Can't merge because of conflicts: [Cannot update language setting for [" + FIELD_TYPE
+ "], Cannot update strength setting for [" + FIELD_TYPE + "]]", e.getMessage());
}


public void testIgnoreAbove() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", FIELD_TYPE)
.field("ignore_above", 5).endObject().endObject()
.endObject().endObject());

DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));

assertEquals(mapping, mapper.mappingSource().toString());

ParsedDocument doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", "elk")
.endObject()),
XContentType.JSON));

IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.length);

doc = mapper.parse(new SourceToParse("test", "type", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.field("field", "elasticsearch")
.endObject()),
XContentType.JSON));

fields = doc.rootDoc().getFields("field");
assertEquals(0, fields.length);
}
}

0 comments on commit 0690016

Please sign in to comment.