Made `search_analyzer` updateable via put mapping api.

Closes #2604
This commit is contained in:
Martijn van Groningen 2013-01-30 11:49:20 +01:00
parent e074e00f76
commit bc20f068c9
2 changed files with 45 additions and 10 deletions

View File

@ -278,7 +278,7 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
protected float boost;
protected final FieldType fieldType;
protected final NamedAnalyzer indexAnalyzer;
protected final NamedAnalyzer searchAnalyzer;
protected NamedAnalyzer searchAnalyzer;
protected PostingsFormatProvider postingsFormat;
protected final SimilarityProvider similarity;
@ -557,15 +557,6 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} else if (!this.indexAnalyzer.name().equals(fieldMergeWith.indexAnalyzer.name())) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different index_analyzer");
}
if (this.searchAnalyzer == null) {
if (fieldMergeWith.searchAnalyzer != null) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different search_analyzer");
}
} else if (fieldMergeWith.searchAnalyzer == null) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different search_analyzer");
} else if (!this.searchAnalyzer.name().equals(fieldMergeWith.searchAnalyzer.name())) {
mergeContext.addConflict("mapper [" + names.fullName() + "] has different search_analyzer");
}
if (this.similarity == null) {
if (fieldMergeWith.similarity() != null) {
@ -583,6 +574,9 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
if (fieldMergeWith.postingsFormat != null) {
this.postingsFormat = fieldMergeWith.postingsFormat;
}
if (fieldMergeWith.searchAnalyzer != null) {
this.searchAnalyzer = fieldMergeWith.searchAnalyzer;
}
if (fieldMergeWith.customFieldDataSettings != null) {
if (!Objects.equal(fieldMergeWith.customFieldDataSettings, this.customFieldDataSettings)) {
this.customFieldDataSettings = fieldMergeWith.customFieldDataSettings;

View File

@ -20,6 +20,7 @@
package org.elasticsearch.test.unit.index.mapper.merge;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
@ -83,4 +84,44 @@ public class TestMergeMapperTests {
assertThat(mergeResult.conflicts().length, equalTo(1));
assertThat(mergeResult.conflicts()[0], equalTo("object mapping [obj] can't be changed from nested to non-nested"));
}
@Test
public void testMergeSearchAnalyzer() throws Exception {
String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("search_analyzer", "whitespace").endObject().endObject()
.endObject().endObject().string();
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("search_analyzer", "keyword").endObject().endObject()
.endObject().endObject().string();
DocumentMapper existing = MapperTests.newParser().parse(mapping1);
DocumentMapper changed = MapperTests.newParser().parse(mapping2);
assertThat(((NamedAnalyzer) existing.mappers().name("field").mapper().searchAnalyzer()).name(), equalTo("whitespace"));
DocumentMapper.MergeResult mergeResult = existing.merge(changed, mergeFlags().simulate(false));
assertThat(mergeResult.hasConflicts(), equalTo(false));
assertThat(((NamedAnalyzer) existing.mappers().name("field").mapper().searchAnalyzer()).name(), equalTo("keyword"));
}
@Test
public void testNotChangeSearchAnalyzer() throws Exception {
String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("search_analyzer", "whitespace").endObject().endObject()
.endObject().endObject().string();
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("postings_format", "direct").endObject().endObject()
.endObject().endObject().string();
DocumentMapper existing = MapperTests.newParser().parse(mapping1);
DocumentMapper changed = MapperTests.newParser().parse(mapping2);
assertThat(((NamedAnalyzer) existing.mappers().name("field").mapper().searchAnalyzer()).name(), equalTo("whitespace"));
DocumentMapper.MergeResult mergeResult = existing.merge(changed, mergeFlags().simulate(false));
assertThat(mergeResult.hasConflicts(), equalTo(false));
assertThat(((NamedAnalyzer) existing.mappers().name("field").mapper().searchAnalyzer()).name(), equalTo("whitespace"));
assertThat((existing.mappers().name("field").mapper().postingsFormatProvider()).name(), equalTo("direct"));
}
}