diff --git a/README.md b/README.md index 26c3577be78..b712aa2996d 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Usage Using the attachment type is simple, in your mapping JSON, simply set a certain JSON element as attachment, for example: ```javascript +PUT /test PUT /test/person/_mapping { "person" : { @@ -116,8 +117,8 @@ PUT /test/person/1 } ``` -The `attachment` type not only indexes the content of the doc, but also automatically adds meta data on the attachment -as well (when available). +The `attachment` type not only indexes the content of the doc in `content` sub field, but also automatically adds meta +data on the attachment as well (when available). The metadata supported are: @@ -143,7 +144,7 @@ PUT /test/person/_mapping "file" : { "type" : "attachment", "fields" : { - "file" : {"index" : "no"}, + "content" : {"index" : "no"}, "title" : {"store" : "yes"}, "date" : {"store" : "yes"}, "author" : {"analyzer" : "myAnalyzer"}, @@ -158,7 +159,7 @@ PUT /test/person/_mapping } ``` -In the above example, the actual content indexed is mapped under `fields` name `file`, and we decide not to index it, so +In the above example, the actual content indexed is mapped under `fields` name `content`, and we decide not to index it, so it will only be available in the `_all` field. The other fields map to their respective metadata names, but there is no need to specify the `type` (like `string` or `date`) since it is already known. @@ -176,7 +177,7 @@ PUT /test/person/_mapping "file": { "type": "attachment", "fields": { - "file": { + "content": { "type": "string", "copy_to": "copy" } @@ -322,7 +323,7 @@ PUT /test/person/_mapping "file": { "type": "attachment", "fields": { - "file": { + "content": { "type": "string", "term_vector":"with_positions_offsets", "store": true @@ -341,12 +342,12 @@ GET /test/person/_search "fields": [], "query": { "match": { - "file": "king queen" + "file.content": "king queen" } }, "highlight": { "fields": { - "file": { + "file.content": { } } } @@ -374,7 +375,7 @@ It gives back: "_id": "1", "_score": 0.13561106, "highlight": { - "file": [ + "file.content": [ "\"God Save the Queen\" (alternatively \"God Save the King\"\n" ] } diff --git a/src/test/java/org/elasticsearch/index/mapper/attachment/test/unit/SimpleAttachmentMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/attachment/test/unit/SimpleAttachmentMapperTests.java index be38c615036..8a5a925b038 100644 --- a/src/test/java/org/elasticsearch/index/mapper/attachment/test/unit/SimpleAttachmentMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/attachment/test/unit/SimpleAttachmentMapperTests.java @@ -28,7 +28,6 @@ import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.attachment.AttachmentMapper; import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils; -import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath; @@ -40,7 +39,6 @@ import static org.hamcrest.Matchers.*; */ public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase { - @Test public void testSimpleMappings() throws Exception { DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir()); mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser()); @@ -83,4 +81,36 @@ public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase { ParseContext.Document doc = docMapper.parse("person", "person", "1", json).rootDoc(); assertThat(doc.get("file"), containsString("This document tests the ability of Apache Tika to extract content")); } + + /** + * test for https://github.com/elastic/elasticsearch-mapper-attachments/issues/179 + * @throws Exception + */ + public void testSimpleMappingsWithAllFields() throws Exception { + DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir()); + mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser()); + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/attachment/test/unit/simple/test-mapping-all-fields.json"); + DocumentMapper docMapper = mapperParser.parse(mapping); + byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/attachment/test/sample-files/testXHTML.html"); + + BytesReference json = jsonBuilder().startObject().field("file", html).endObject().bytes(); + ParseContext.Document doc = docMapper.parse("person", "person", "1", json).rootDoc(); + + assertThat(doc.get(docMapper.mappers().getMapper("file.content_type").fieldType().names().indexName()), startsWith("application/xhtml+xml")); + assertThat(doc.get(docMapper.mappers().getMapper("file.title").fieldType().names().indexName()), equalTo("XHTML test document")); + assertThat(doc.get(docMapper.mappers().getMapper("file.content").fieldType().names().indexName()), containsString("This document tests the ability of Apache Tika to extract content")); + + // re-parse it + String builtMapping = docMapper.mappingSource().string(); + docMapper = mapperParser.parse(builtMapping); + + json = jsonBuilder().startObject().field("file", html).endObject().bytes(); + + doc = docMapper.parse("person", "person", "1", json).rootDoc(); + + assertThat(doc.get(docMapper.mappers().getMapper("file.content_type").fieldType().names().indexName()), startsWith("application/xhtml+xml")); + assertThat(doc.get(docMapper.mappers().getMapper("file.title").fieldType().names().indexName()), equalTo("XHTML test document")); + assertThat(doc.get(docMapper.mappers().getMapper("file.content").fieldType().names().indexName()), containsString("This document tests the ability of Apache Tika to extract content")); + } + } diff --git a/src/test/resources/org/elasticsearch/index/mapper/attachment/test/unit/simple/test-mapping-all-fields.json b/src/test/resources/org/elasticsearch/index/mapper/attachment/test/unit/simple/test-mapping-all-fields.json new file mode 100644 index 00000000000..ea83b98ceec --- /dev/null +++ b/src/test/resources/org/elasticsearch/index/mapper/attachment/test/unit/simple/test-mapping-all-fields.json @@ -0,0 +1,19 @@ +{ + "person":{ + "properties":{ + "file":{ + "type":"attachment", + "fields" : { + "content" : {"store" : "yes"}, + "title" : {"store" : "yes"}, + "date" : {"store" : "yes"}, + "author" : {"analyzer" : "standard"}, + "keywords" : {"store" : "yes"}, + "content_type" : {"store" : "yes"}, + "content_length" : {"store" : "yes"}, + "language" : {"store" : "yes"} + } + } + } + } +}