Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix merging of search_as_you_type field mapper #40593

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field

@Override
protected String contentType() {
return CONTENT_TYPE;
return "shingle";
}
}

Expand Down Expand Up @@ -663,6 +663,16 @@ public SearchAsYouTypeFieldMapper(String simpleName,
this.maxShingleSize = maxShingleSize;
}

@Override
public FieldMapper updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) {
SearchAsYouTypeFieldMapper fieldMapper = (SearchAsYouTypeFieldMapper) super.updateFieldType(fullNameToFieldType);
fieldMapper.prefixField = (PrefixFieldMapper) fieldMapper.prefixField.updateFieldType(fullNameToFieldType);
for (int i = 0; i < fieldMapper.shingleFields.length; i++) {
fieldMapper.shingleFields[i] = (ShingleFieldMapper) fieldMapper.shingleFields[i].updateFieldType(fullNameToFieldType);
}
return fieldMapper;
}

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
final String value = context.externalValueSet() ? context.externalValue().toString() : context.parser().textOrNull();
Expand Down Expand Up @@ -692,10 +702,12 @@ protected void doMerge(Mapper mergeWith) {
super.doMerge(mergeWith);
SearchAsYouTypeFieldMapper mw = (SearchAsYouTypeFieldMapper) mergeWith;
if (mw.maxShingleSize != maxShingleSize) {
throw new IllegalArgumentException("mapper [" + name() + "] has different maxShingleSize setting, current ["
throw new IllegalArgumentException("mapper [" + name() + "] has different [max_shingle_size] setting, current ["
+ this.maxShingleSize + "], merged [" + mw.maxShingleSize + "]");
}
this.prefixField = (PrefixFieldMapper) this.prefixField.merge(mw);
if (prefixField.equals(mw.prefixField) == false) {
this.prefixField = (PrefixFieldMapper) this.prefixField.merge(mw.prefixField);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use this same logic for the shingle fields below (i.e. only merge if they're unequal)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a leftover, we should always merge, I removed the test


ShingleFieldMapper[] shingleFieldMappers = new ShingleFieldMapper[mw.shingleFields.length];
for (int i = 0; i < shingleFieldMappers.length; i++) {
Expand Down Expand Up @@ -736,8 +748,7 @@ public Iterator<Mapper> iterator() {
List<Mapper> subIterators = new ArrayList<>();
subIterators.add(prefixField);
subIterators.addAll(Arrays.asList(shingleFields));
@SuppressWarnings("unchecked") Iterator<Mapper> concat = Iterators.concat(super.iterator(), subIterators.iterator());
return concat;
return Iterators.concat(super.iterator(), subIterators.iterator());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we fail compilation on the warning from the call to Iterators.concat here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I restored the original

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import static java.util.Arrays.asList;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasProperty;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -180,6 +181,60 @@ public void testConfiguration() throws IOException {
getShingleFieldMapper(defaultMapper, "a_field._4gram").fieldType(), 4, analyzerName, prefixFieldMapper.fieldType());
}

public void testSimpleMerge() throws IOException {
MapperService mapperService = createIndex("test").mapperService();
{
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("_doc")
.startObject("properties")
.startObject("a_field")
.field("type", "search_as_you_type")
.field("analyzer", "standard")
.endObject()
.endObject()
.endObject().endObject());
DocumentMapper mapper = mapperService.merge("_doc",
new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
}

{
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("_doc")
.startObject("properties")
.startObject("a_field")
.field("type", "search_as_you_type")
.field("analyzer", "standard")
.endObject()
.startObject("b_field")
.field("type", "text")
.endObject()
.endObject()
.endObject().endObject());
DocumentMapper mapper = mapperService.merge("_doc",
new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
}

{
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject()
.startObject("_doc")
.startObject("properties")
.startObject("a_field")
.field("type", "search_as_you_type")
.field("analyzer", "standard")
.field("max_shingle_size", "4")
.endObject()
.startObject("b_field")
.field("type", "text")
.endObject()
.endObject()
.endObject().endObject());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> mapperService.merge("_doc",
new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE));
assertThat(e.getMessage(), containsString("different [max_shingle_size]"));
}
}

public void testIndexOptions() throws IOException {
final String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
Expand Down