Setting "_content_type" in indexing request has no effect

Example below. I set the type as text/plain but it is identified as text/html.

```sh
#!/bin/sh

echo "\n\n Delete testidx \n"
curl -XDELETE "http://localhost:9200/testidx"

echo "\n\n Create index and mapping \n"
curl -XPUT "http://localhost:9200/testidx" -d'
{
  "mappings": {
    "session": {
      "properties": {
        "Content": {
          "properties": {
            "content": {
              "type": "attachment",
              "path": "full",
              "store": "yes",
              "fields": {
                "content": {
                  "type": "string",
                  "store": "yes"
                },
                "author": {
                  "type": "string",
                  "store": "yes"
                },
                "title": {
                  "type": "string",
                  "store": "yes"
                },
                "name": {
                  "type": "string",
                  "store": "yes"
                },
                "date": {
                  "type": "date",
                  "format": "dateOptionalTime",
                  "store": "yes"
                },
                "keywords": {
                  "type": "string",
                  "store": "yes"
                },
                "content_type": {
                  "type": "string",
                  "store": "yes"
                },
                "content_length": {
                  "type": "integer",
                  "store": "yes"
                }
              }
            }
          }
        }
      }
    }
  }
}'

echo "\n\n Index document \n"
curl -XPOST "http://localhost:9200/_bulk" -d'
  {"index":{"_index":"testidx","_type":"session"}}
  {"Content":[{"_content_type":"text/plain","content":"BASE64ENCODED_CONTENT"}]}
'

echo "\n\n Refresh \n"
curl -XPOST "http://localhost:9200/testidx/_refresh"

echo "\n\n Get doc type \n"
curl -XPOST "http://localhost:9200/testidx/_search?pretty" -d'
{
  "fields": ["Content.content.content_type","Content.content.content_length","Content.content"]
}'
```

Closes #65.
(cherry picked from commit 38075dc)
This commit is contained in:
David Pilato 2014-06-03 09:30:22 +02:00
parent 7f8143ff12
commit 4b35501cf3
4 changed files with 49 additions and 4 deletions

View File

@ -352,6 +352,7 @@ public class AttachmentMapper implements Mapper {
} else if ("_name".equals(currentFieldName)) { } else if ("_name".equals(currentFieldName)) {
name = parser.text(); name = parser.text();
} else if ("language".equals(currentFieldName)) { } else if ("language".equals(currentFieldName)) {
// TODO should be _language
language = parser.text(); language = parser.text();
} }
} else if (token == XContentParser.Token.VALUE_NUMBER) { } else if (token == XContentParser.Token.VALUE_NUMBER) {
@ -448,7 +449,11 @@ public class AttachmentMapper implements Mapper {
} }
try { try {
if (contentType != null) {
context.externalValue(contentType);
} else {
context.externalValue(metadata.get(Metadata.CONTENT_TYPE)); context.externalValue(metadata.get(Metadata.CONTENT_TYPE));
}
contentTypeMapper.parse(context); contentTypeMapper.parse(context);
} catch(MapperParsingException e){ } catch(MapperParsingException e){
if (!ignoreErrors) throw e; if (!ignoreErrors) throw e;

View File

@ -38,6 +38,7 @@ import static org.hamcrest.Matchers.equalTo;
/** /**
* Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/18 * Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/18
*/ */
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE)
public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegrationTest { public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegrationTest {
private boolean ignore_errors = true; private boolean ignore_errors = true;
@ -65,7 +66,6 @@ public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegration
ignore_errors = true; ignore_errors = true;
logger.info("creating index [test]"); logger.info("creating index [test]");
createIndex("test"); createIndex("test");
ensureGreen();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json");
byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html"); byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html");
@ -93,7 +93,6 @@ public class MultipleAttachmentIntegrationTests extends ElasticsearchIntegration
logger.info("creating index [test]"); logger.info("creating index [test]");
createIndex("test"); createIndex("test");
ensureGreen();
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json"); String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multipledocs/test-mapping.json");
byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html"); byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/htmlWithValidDateMeta.html");

View File

@ -21,6 +21,7 @@ package org.elasticsearch.plugin.mapper.attachments.test;
import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.plugins.PluginsService; 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.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.queryString; import static org.elasticsearch.index.query.QueryBuilders.queryString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
/** /**
* *
*/ */
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE)
public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTest { public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTest {
@Override @Override
@ -53,7 +56,6 @@ public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTe
public void createEmptyIndex() throws Exception { public void createEmptyIndex() throws Exception {
logger.info("creating index [test]"); logger.info("creating index [test]");
createIndex("test"); createIndex("test");
ensureGreen();
} }
@Override @Override
@ -129,4 +131,30 @@ public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTe
index("test", "person", jsonBuilder().startObject().field("file").startObject().endObject()); 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));
}
} }

View File

@ -0,0 +1,13 @@
{
"person":{
"properties":{
"file":{
"type":"attachment",
"fields": {
"content_type" : {"store" : "yes"},
"name" : {"store" : "yes"}
}
}
}
}
}