Fix xcontent serialization of timestamp/routing index field

The index field was serialized as a boolean instead of showing the
'analyed', 'not_analzyed', 'no' options. Fixed by calling
indexTokenizeOptionToString() in the builder.

Closes #3174
This commit is contained in:
Alexander Reelsen 2013-07-15 17:48:58 +02:00
parent baea7fd1c2
commit 28b9e25053
4 changed files with 65 additions and 6 deletions

View File

@ -238,7 +238,7 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
}
builder.startObject(CONTENT_TYPE);
if (fieldType.indexed() != Defaults.FIELD_TYPE.indexed()) {
builder.field("index", fieldType.indexed());
builder.field("index", indexTokenizeOptionToString(fieldType.indexed(), fieldType.tokenized()));
}
if (fieldType.stored() != Defaults.FIELD_TYPE.stored()) {
builder.field("store", fieldType.stored());

View File

@ -226,7 +226,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements InternalMap
}
if (enabledState.enabled) {
if (fieldType.indexed() != Defaults.FIELD_TYPE.indexed()) {
builder.field("index", fieldType.indexed());
builder.field("index", indexTokenizeOptionToString(fieldType.indexed(), fieldType.tokenized()));
}
if (fieldType.stored() != Defaults.FIELD_TYPE.stored()) {
builder.field("store", fieldType.stored());

View File

@ -19,15 +19,19 @@
package org.elasticsearch.test.unit.index.mapper.routing;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.*;
/**
*
@ -49,4 +53,39 @@ public class RoutingTypeMapperTests {
assertThat(doc.rootDoc().get("_routing"), equalTo("routing_value"));
assertThat(doc.rootDoc().get("field"), equalTo("value"));
}
@Test
public void testSetValues() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing")
.field("store", "no")
.field("index", "no")
.field("path", "route")
.endObject()
.endObject().endObject().string();
DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
assertThat(docMapper.routingFieldMapper().fieldType().stored(), equalTo(false));
assertThat(docMapper.routingFieldMapper().fieldType().indexed(), equalTo(false));
assertThat(docMapper.routingFieldMapper().path(), equalTo("route"));
}
@Test
public void testThatSerializationWorksCorrectlyForIndexField() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_routing").field("store", "no").field("index", "no").endObject()
.endObject().endObject().string();
DocumentMapper enabledMapper = MapperTests.newParser().parse(enabledMapping);
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
enabledMapper.routingFieldMapper().toXContent(builder, null).endObject();
builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose();
assertThat(serializedMap, hasKey("_routing"));
assertThat(serializedMap.get("_routing"), instanceOf(Map.class));
Map<String, Object> routingConfiguration = (Map<String, Object>) serializedMap.get("_routing");
assertThat(routingConfiguration, hasKey("store"));
assertThat(routingConfiguration.get("store").toString(), is("false"));
assertThat(routingConfiguration, hasKey("index"));
assertThat(routingConfiguration.get("index").toString(), is("no"));
}
}

View File

@ -20,10 +20,12 @@
package org.elasticsearch.test.unit.index.mapper.timestamp;
import java.util.Locale;
import java.util.Map;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
@ -32,9 +34,7 @@ import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;
/**
*/
@ -132,4 +132,24 @@ public class TimestampMappingTests {
assertThat(builder.string(), is(String.format(Locale.ROOT, "{\"%s\":{}}", TimestampFieldMapper.NAME)));
}
@Test // issue 3174
public void testThatSerializationWorksCorrectlyForIndexField() throws Exception {
String enabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_timestamp").field("enabled", true).field("store", "yes").field("index", "no").endObject()
.endObject().endObject().string();
DocumentMapper enabledMapper = MapperTests.newParser().parse(enabledMapping);
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
enabledMapper.timestampFieldMapper().toXContent(builder, null).endObject();
builder.close();
Map<String, Object> serializedMap = JsonXContent.jsonXContent.createParser(builder.bytes()).mapAndClose();
assertThat(serializedMap, hasKey("_timestamp"));
assertThat(serializedMap.get("_timestamp"), instanceOf(Map.class));
Map<String, Object> timestampConfiguration = (Map<String, Object>) serializedMap.get("_timestamp");
assertThat(timestampConfiguration, hasKey("store"));
assertThat(timestampConfiguration.get("store").toString(), is("true"));
assertThat(timestampConfiguration, hasKey("index"));
assertThat(timestampConfiguration.get("index").toString(), is("no"));
}
}