Skip to content

Commit

Permalink
Mappings: Fixed an equality check in StringFieldMapper.
Browse files Browse the repository at this point in the history
The check was ineffective and was causing search_quote_analyzer to be added to the mapping unnecessarily.

Closes #10357
closes #10359
  • Loading branch information
jtibshirani authored and rjernst committed Apr 17, 2015
1 parent 2f06e2b commit c8f85d0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Expand Up @@ -371,7 +371,7 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
if (includeDefaults || positionOffsetGap != Defaults.POSITION_OFFSET_GAP) {
builder.field("position_offset_gap", positionOffsetGap);
}
if (searchQuotedAnalyzer != null && searchAnalyzer != searchQuotedAnalyzer) {
if (searchQuotedAnalyzer != null && !searchQuotedAnalyzer.name().equals(searchAnalyzer.name())) {
builder.field("search_quote_analyzer", searchQuotedAnalyzer.name());
} else if (includeDefaults) {
if (searchQuotedAnalyzer == null) {
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.mapper.string;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfo.DocValuesType;
import org.apache.lucene.index.IndexableField;
Expand All @@ -31,7 +32,9 @@
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.fielddata.FieldDataType;
import org.elasticsearch.index.mapper.ContentPath;
Expand All @@ -50,6 +53,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -215,6 +219,79 @@ public void testDefaultsForNotAnalyzed() throws Exception {
assertThat(fieldType.omitNorms(), equalTo(false));
assertParseIdemPotent(fieldType, defaultMapper);
}

@Test
public void testSearchQuoteAnalyzerSerialization() throws Exception {
// Cases where search_quote_analyzer should not be added to the mapping.
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("field1")
.field("type", "string")
.field("position_offset_gap", 1000)
.endObject()
.startObject("field2")
.field("type", "string")
.field("position_offset_gap", 1000)
.field("analyzer", "standard")
.endObject()
.startObject("field3")
.field("type", "string")
.field("position_offset_gap", 1000)
.field("index_analyzer", "standard")
.field("search_analyzer", "simple")
.endObject()
.startObject("field4")
.field("type", "string")
.field("position_offset_gap", 1000)
.field("index_analyzer", "standard")
.field("search_analyzer", "simple")
.field("search_quote_analyzer", "simple")
.endObject()
.endObject()
.endObject().endObject().string();

DocumentMapper mapper = parser.parse(mapping);
for (String fieldName : Lists.newArrayList("field1", "field2", "field3", "field4")) {
Map<String, Object> serializedMap = getSerializedMap(fieldName, mapper);
assertFalse(serializedMap.containsKey("search_quote_analyzer"));
}

// Cases where search_quote_analyzer should be present.
mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("field1")
.field("type", "string")
.field("position_offset_gap", 1000)
.field("search_quote_analyzer", "simple")
.endObject()
.startObject("field2")
.field("type", "string")
.field("position_offset_gap", 1000)
.field("analyzer", "standard")
.field("search_analyzer", "standard")
.field("search_quote_analyzer", "simple")
.endObject()
.endObject()
.endObject().endObject().string();

mapper = parser.parse(mapping);
for (String fieldName : Lists.newArrayList("field1", "field2")) {
Map<String, Object> serializedMap = getSerializedMap(fieldName, mapper);
assertEquals(serializedMap.get("search_quote_analyzer"), "simple");
}
}

private Map<String, Object> getSerializedMap(String fieldName, DocumentMapper mapper) throws Exception {
FieldMapper<?> fieldMapper = mapper.mappers().smartNameFieldMapper(fieldName);
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
fieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
builder.close();

Map<String, Object> fieldMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose();
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) fieldMap.get(fieldName);
return result;
}

@Test
public void testTermVectors() throws Exception {
Expand Down

0 comments on commit c8f85d0

Please sign in to comment.