diff --git a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java index d312fa1b780..e1d4963575f 100644 --- a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java @@ -352,6 +352,7 @@ public class AttachmentMapper implements Mapper { } else if ("_name".equals(currentFieldName)) { name = parser.text(); } else if ("language".equals(currentFieldName)) { + // TODO should be _language language = parser.text(); } } else if (token == XContentParser.Token.VALUE_NUMBER) { @@ -448,7 +449,11 @@ public class AttachmentMapper implements Mapper { } try { - context.externalValue(metadata.get(Metadata.CONTENT_TYPE)); + if (contentType != null) { + context.externalValue(contentType); + } else { + context.externalValue(metadata.get(Metadata.CONTENT_TYPE)); + } contentTypeMapper.parse(context); } catch(MapperParsingException e){ if (!ignoreErrors) throw e; diff --git a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/MultipleAttachmentIntegrationTests.java b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/MultipleAttachmentIntegrationTests.java index 3f8b10fb6b1..1153d450d33 100644 --- a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/MultipleAttachmentIntegrationTests.java +++ b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/MultipleAttachmentIntegrationTests.java @@ -38,6 +38,7 @@ import static org.hamcrest.Matchers.equalTo; /** * Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/18 */ +@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegrationTest { private boolean ignore_errors = true; @@ -65,7 +66,6 @@ public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegration ignore_errors = true; logger.info("creating index [test]"); createIndex("test"); - ensureGreen(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json"); byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html"); @@ -93,7 +93,6 @@ public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegration logger.info("creating index [test]"); createIndex("test"); - ensureGreen(); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json"); byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html"); diff --git a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java index 8c143178f8a..d0c4315724b 100644 --- a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java +++ b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java @@ -21,6 +21,7 @@ package org.elasticsearch.plugin.mapper.attachments.test; import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.plugins.PluginsService; @@ -35,10 +36,12 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.queryString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; /** * */ +@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE) public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTest { @Override @@ -53,7 +56,6 @@ public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTe public void createEmptyIndex() throws Exception { logger.info("creating index [test]"); createIndex("test"); - ensureGreen(); } @Override @@ -129,4 +131,30 @@ public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTe index("test", "person", jsonBuilder().startObject().field("file").startObject().endObject()); } + + @Test + public void testContentTypeAndName() throws Exception { + String dummyContentType = "text/my-dummy-content-type"; + String dummyName = "my-dummy-name-txt"; + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-mapping-store-content-type.json"); + byte[] txt = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/testContentLength.txt"); + + client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); + + index("test", "person", jsonBuilder().startObject().field("file").startObject().field("content", txt) + .field("_content_type", dummyContentType) + .field("_name", dummyName) + .endObject()); + refresh(); + + SearchResponse response = client().prepareSearch("test") + .addField("content_type") + .addField("name") + .execute().get(); + String contentType = response.getHits().getAt(0).getFields().get("file.content_type").getValue(); + String name = response.getHits().getAt(0).getFields().get("file.name").getValue(); + assertThat(contentType, is(dummyContentType)); + assertThat(name, is(dummyName)); + } + } diff --git a/src/test/resources/org/elasticsearch/index/mapper/xcontent/test-mapping-store-content-type.json b/src/test/resources/org/elasticsearch/index/mapper/xcontent/test-mapping-store-content-type.json new file mode 100644 index 00000000000..e95233189c4 --- /dev/null +++ b/src/test/resources/org/elasticsearch/index/mapper/xcontent/test-mapping-store-content-type.json @@ -0,0 +1,13 @@ +{ + "person":{ + "properties":{ + "file":{ + "type":"attachment", + "fields": { + "content_type" : {"store" : "yes"}, + "name" : {"store" : "yes"} + } + } + } + } +}