Skip to content

Commit c65db1a

Browse files
committed
Fix mapping examples in documentation
Closes elastic#179 (cherry picked from commit 7700340) (cherry picked from commit ad68433) (cherry picked from commit f83ffd2)
1 parent b6b510b commit c65db1a

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Usage
8383
Using the attachment type is simple, in your mapping JSON, simply set a certain JSON element as attachment, for example:
8484

8585
```javascript
86+
PUT /test
8687
PUT /test/person/_mapping
8788
{
8889
"person" : {
@@ -116,8 +117,8 @@ PUT /test/person/1
116117
}
117118
```
118119

119-
The `attachment` type not only indexes the content of the doc, but also automatically adds meta data on the attachment
120-
as well (when available).
120+
The `attachment` type not only indexes the content of the doc in `content` sub field, but also automatically adds meta
121+
data on the attachment as well (when available).
121122

122123
The metadata supported are:
123124

@@ -143,7 +144,7 @@ PUT /test/person/_mapping
143144
"file" : {
144145
"type" : "attachment",
145146
"fields" : {
146-
"file" : {"index" : "no"},
147+
"content" : {"index" : "no"},
147148
"title" : {"store" : "yes"},
148149
"date" : {"store" : "yes"},
149150
"author" : {"analyzer" : "myAnalyzer"},
@@ -158,7 +159,7 @@ PUT /test/person/_mapping
158159
}
159160
```
160161

161-
In the above example, the actual content indexed is mapped under `fields` name `file`, and we decide not to index it, so
162+
In the above example, the actual content indexed is mapped under `fields` name `content`, and we decide not to index it, so
162163
it will only be available in the `_all` field. The other fields map to their respective metadata names, but there is no
163164
need to specify the `type` (like `string` or `date`) since it is already known.
164165

@@ -176,7 +177,7 @@ PUT /test/person/_mapping
176177
"file": {
177178
"type": "attachment",
178179
"fields": {
179-
"file": {
180+
"content": {
180181
"type": "string",
181182
"copy_to": "copy"
182183
}
@@ -322,7 +323,7 @@ PUT /test/person/_mapping
322323
"file": {
323324
"type": "attachment",
324325
"fields": {
325-
"file": {
326+
"content": {
326327
"type": "string",
327328
"term_vector":"with_positions_offsets",
328329
"store": true
@@ -341,12 +342,12 @@ GET /test/person/_search
341342
"fields": [],
342343
"query": {
343344
"match": {
344-
"file": "king queen"
345+
"file.content": "king queen"
345346
}
346347
},
347348
"highlight": {
348349
"fields": {
349-
"file": {
350+
"file.content": {
350351
}
351352
}
352353
}
@@ -374,7 +375,7 @@ It gives back:
374375
"_id": "1",
375376
"_score": 0.13561106,
376377
"highlight": {
377-
"file": [
378+
"file.content": [
378379
"\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n"
379380
]
380381
}

src/test/java/org/elasticsearch/index/mapper/attachment/test/unit/SimpleAttachmentMapperTests.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.index.mapper.ParseContext;
2929
import org.elasticsearch.index.mapper.attachment.AttachmentMapper;
3030
import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils;
31-
import org.junit.Test;
3231

3332
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
3433
import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath;
@@ -40,7 +39,6 @@
4039
*/
4140
public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase {
4241

43-
@Test
4442
public void testSimpleMappings() throws Exception {
4543
DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir());
4644
mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser());
@@ -83,4 +81,36 @@ public void testContentBackcompat() throws Exception {
8381
ParseContext.Document doc = docMapper.parse("person", "person", "1", json).rootDoc();
8482
assertThat(doc.get("file"), containsString("This document tests the ability of Apache Tika to extract content"));
8583
}
84+
85+
/**
86+
* test for https://github.com/elastic/elasticsearch-mapper-attachments/issues/179
87+
* @throws Exception
88+
*/
89+
public void testSimpleMappingsWithAllFields() throws Exception {
90+
DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(createTempDir());
91+
mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser());
92+
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/attachment/test/unit/simple/test-mapping-all-fields.json");
93+
DocumentMapper docMapper = mapperParser.parse(mapping);
94+
byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/attachment/test/sample-files/testXHTML.html");
95+
96+
BytesReference json = jsonBuilder().startObject().field("file", html).endObject().bytes();
97+
ParseContext.Document doc = docMapper.parse("person", "person", "1", json).rootDoc();
98+
99+
assertThat(doc.get(docMapper.mappers().getMapper("file.content_type").fieldType().names().indexName()), startsWith("application/xhtml+xml"));
100+
assertThat(doc.get(docMapper.mappers().getMapper("file.title").fieldType().names().indexName()), equalTo("XHTML test document"));
101+
assertThat(doc.get(docMapper.mappers().getMapper("file.content").fieldType().names().indexName()), containsString("This document tests the ability of Apache Tika to extract content"));
102+
103+
// re-parse it
104+
String builtMapping = docMapper.mappingSource().string();
105+
docMapper = mapperParser.parse(builtMapping);
106+
107+
json = jsonBuilder().startObject().field("file", html).endObject().bytes();
108+
109+
doc = docMapper.parse("person", "person", "1", json).rootDoc();
110+
111+
assertThat(doc.get(docMapper.mappers().getMapper("file.content_type").fieldType().names().indexName()), startsWith("application/xhtml+xml"));
112+
assertThat(doc.get(docMapper.mappers().getMapper("file.title").fieldType().names().indexName()), equalTo("XHTML test document"));
113+
assertThat(doc.get(docMapper.mappers().getMapper("file.content").fieldType().names().indexName()), containsString("This document tests the ability of Apache Tika to extract content"));
114+
}
115+
86116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"person":{
3+
"properties":{
4+
"file":{
5+
"type":"attachment",
6+
"fields" : {
7+
"content" : {"store" : "yes"},
8+
"title" : {"store" : "yes"},
9+
"date" : {"store" : "yes"},
10+
"author" : {"analyzer" : "standard"},
11+
"keywords" : {"store" : "yes"},
12+
"content_type" : {"store" : "yes"},
13+
"content_length" : {"store" : "yes"},
14+
"language" : {"store" : "yes"}
15+
}
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)