include the path when serializing _id field mappings

This commit is contained in:
Njal Karevoll 2011-09-20 16:37:14 +02:00 committed by Shay Banon
parent 8c322b4cc2
commit 6a6cba1ff3
2 changed files with 25 additions and 1 deletions

View File

@ -198,7 +198,7 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
// if all are defaults, no sense to write it at all
if (store == Defaults.STORE && index == Defaults.INDEX) {
if (store == Defaults.STORE && index == Defaults.INDEX && path == Defaults.PATH) {
return builder;
}
builder.startObject(CONTENT_TYPE);
@ -208,6 +208,9 @@ public class IdFieldMapper extends AbstractFieldMapper<String> implements Intern
if (index != Defaults.INDEX) {
builder.field("index", index.name().toLowerCase());
}
if (path != Defaults.PATH) {
builder.field("path", path);
}
builder.endObject();
return builder;
}

View File

@ -19,6 +19,8 @@
package org.elasticsearch.index.mapper.id;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
@ -91,4 +93,23 @@ public class IdMappingTests {
assertThat(doc.rootDoc().get(UidFieldMapper.NAME), notNullValue());
assertThat(doc.rootDoc().get(IdFieldMapper.NAME), notNullValue());
}
@Test public void testIdPath() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_id").field("path", "my_path").endObject()
.endObject().endObject().string();
DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
// serialize the id mapping
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
builder = docMapper.idFieldMapper().toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
String serialized_id_mapping = builder.string();
String expected_id_mapping = XContentFactory.jsonBuilder().startObject()
.startObject("_id").field("path", "my_path").endObject()
.endObject().string();
assertThat(serialized_id_mapping, equalTo(expected_id_mapping));
}
}