Skip to content

Commit 77081e3

Browse files
committed
[Doc] copy_to using attachment field type
If you want to use [copy_to](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to) feature, you need to define it on each sub-field you want to copy to another field: ```javascript PUT /test/person/_mapping { "person": { "properties": { "file": { "type": "attachment", "path": "full", "fields": { "file": { "type": "string", "copy_to": "copy" } } }, "copy": { "type": "string" } } } } ``` In this example, the extracted content will be copy as well to `copy` field. Closes elastic#97. (cherry picked from commit f4f6b57) (cherry picked from commit 5878a62)
1 parent ec59d38 commit 77081e3

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,37 @@ In the above example, the actual content indexed is mapped under `fields` name `
117117
it will only be available in the `_all` field. The other fields map to their respective metadata names, but there is no
118118
need to specify the `type` (like `string` or `date`) since it is already known.
119119

120+
Copy To feature
121+
---------------
122+
123+
If you want to use [copy_to](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to)
124+
feature, you need to define it on each sub-field you want to copy to another field:
125+
126+
```javascript
127+
PUT /test/person/_mapping
128+
{
129+
"person": {
130+
"properties": {
131+
"file": {
132+
"type": "attachment",
133+
"path": "full",
134+
"fields": {
135+
"file": {
136+
"type": "string",
137+
"copy_to": "copy"
138+
}
139+
}
140+
},
141+
"copy": {
142+
"type": "string"
143+
}
144+
}
145+
}
146+
}
147+
```
148+
149+
In this example, the extracted content will be copy as well to `copy` field.
150+
120151
Querying or accessing metadata
121152
------------------------------
122153

src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.plugins.PluginsService;
2828
import org.elasticsearch.test.ElasticsearchIntegrationTest;
2929
import org.junit.Before;
30+
import org.junit.Ignore;
3031
import org.junit.Test;
3132

3233
import static org.elasticsearch.client.Requests.putMappingRequest;
@@ -149,4 +150,42 @@ public void testContentTypeAndName() throws Exception {
149150
assertThat(name, is(dummyName));
150151
}
151152

153+
/**
154+
* As for now, we don't support a global `copy_to` property for `attachment` type.
155+
* So this test is failing.
156+
*/
157+
@Test @Ignore
158+
public void testCopyTo() throws Exception {
159+
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/copy-to/copy-to.json");
160+
byte[] txt = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/text-in-english.txt");
161+
162+
client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet();
163+
164+
index("test", "person", jsonBuilder().startObject().field("file", txt).endObject());
165+
refresh();
166+
167+
CountResponse countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("file")).execute().get();
168+
assertThat(countResponse.getCount(), equalTo(1l));
169+
170+
countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("copy")).execute().get();
171+
assertThat(countResponse.getCount(), equalTo(1l));
172+
}
173+
174+
@Test
175+
public void testCopyToSubField() throws Exception {
176+
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json");
177+
byte[] txt = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/text-in-english.txt");
178+
179+
client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet();
180+
181+
index("test", "person", jsonBuilder().startObject().field("file", txt).endObject());
182+
refresh();
183+
184+
CountResponse countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("file")).execute().get();
185+
assertThat(countResponse.getCount(), equalTo(1l));
186+
187+
countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("copy")).execute().get();
188+
assertThat(countResponse.getCount(), equalTo(1l));
189+
}
190+
152191
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"person": {
3+
"properties": {
4+
"file": {
5+
"type": "attachment",
6+
"path": "full",
7+
"fields": {
8+
"file": {
9+
"type": "string",
10+
"copy_to": "copy"
11+
}
12+
}
13+
},
14+
"copy": {
15+
"type": "string"
16+
}
17+
}
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"person": {
3+
"properties": {
4+
"file": {
5+
"type": "attachment",
6+
"copy_to": "copy"
7+
},
8+
"copy": {
9+
"type": "attachment"
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)