Fix mapping examples in documentation

Closes #179
(cherry picked from commit 7700340)
(cherry picked from commit ad68433)
(cherry picked from commit f83ffd2)
This commit is contained in:
David Pilato 2015-10-29 19:20:26 +01:00
parent b6b510bed2
commit c65db1a008
3 changed files with 61 additions and 11 deletions

View File

@ -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: Using the attachment type is simple, in your mapping JSON, simply set a certain JSON element as attachment, for example:
```javascript ```javascript
PUT /test
PUT /test/person/_mapping PUT /test/person/_mapping
{ {
"person" : { "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 The `attachment` type not only indexes the content of the doc in `content` sub field, but also automatically adds meta
as well (when available). data on the attachment as well (when available).
The metadata supported are: The metadata supported are:
@ -143,7 +144,7 @@ PUT /test/person/_mapping
"file" : { "file" : {
"type" : "attachment", "type" : "attachment",
"fields" : { "fields" : {
"file" : {"index" : "no"}, "content" : {"index" : "no"},
"title" : {"store" : "yes"}, "title" : {"store" : "yes"},
"date" : {"store" : "yes"}, "date" : {"store" : "yes"},
"author" : {"analyzer" : "myAnalyzer"}, "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 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. need to specify the `type` (like `string` or `date`) since it is already known.
@ -176,7 +177,7 @@ PUT /test/person/_mapping
"file": { "file": {
"type": "attachment", "type": "attachment",
"fields": { "fields": {
"file": { "content": {
"type": "string", "type": "string",
"copy_to": "copy" "copy_to": "copy"
} }
@ -322,7 +323,7 @@ PUT /test/person/_mapping
"file": { "file": {
"type": "attachment", "type": "attachment",
"fields": { "fields": {
"file": { "content": {
"type": "string", "type": "string",
"term_vector":"with_positions_offsets", "term_vector":"with_positions_offsets",
"store": true "store": true
@ -341,12 +342,12 @@ GET /test/person/_search
"fields": [], "fields": [],
"query": { "query": {
"match": { "match": {
"file": "king queen" "file.content": "king queen"
} }
}, },
"highlight": { "highlight": {
"fields": { "fields": {
"file": { "file.content": {
} }
} }
} }
@ -374,7 +375,7 @@ It gives back:
"_id": "1", "_id": "1",
"_score": 0.13561106, "_score": 0.13561106,
"highlight": { "highlight": {
"file": [ "file.content": [
"\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n" "\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n"
] ]
} }

View File

@ -28,7 +28,6 @@ import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.attachment.AttachmentMapper; import org.elasticsearch.index.mapper.attachment.AttachmentMapper;
import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils; import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils;
import org.junit.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath; import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath;
@ -40,7 +39,6 @@ import static org.hamcrest.Matchers.*;
*/ */
public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase { public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase {
@Test
public void testSimpleMappings() throws Exception { public void testSimpleMappings() throws Exception {
DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir()); DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir());
mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser()); 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(); 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")); 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"));
}
} }

View File

@ -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"}
}
}
}
}
}