From 77081e3dbf739547703ef538da83eb385fc18112 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Wed, 11 Feb 2015 23:04:07 +0100 Subject: [PATCH] [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 #97. (cherry picked from commit f4f6b57) (cherry picked from commit 5878a62) --- README.md | 31 +++++++++++++++ .../SimpleAttachmentIntegrationTests.java | 39 +++++++++++++++++++ .../mapper/copy-to/copy-to-subfield.json | 19 +++++++++ .../index/mapper/copy-to/copy-to.json | 13 +++++++ 4 files changed, 102 insertions(+) create mode 100644 src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json create mode 100644 src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to.json diff --git a/README.md b/README.md index 81378b2ab51..40e0f34a8ac 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,37 @@ In the above example, the actual content indexed is mapped under `fields` name ` 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. +Copy To feature +--------------- + +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. + Querying or accessing metadata ------------------------------ diff --git a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java index 8716e1f57c5..702742982e3 100644 --- a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java +++ b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.plugins.PluginsService; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import static org.elasticsearch.client.Requests.putMappingRequest; @@ -149,4 +150,42 @@ public class SimpleAttachmentIntegrationTests extends ElasticsearchIntegrationTe assertThat(name, is(dummyName)); } + /** + * As for now, we don't support a global `copy_to` property for `attachment` type. + * So this test is failing. + */ + @Test @Ignore + public void testCopyTo() throws Exception { + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/copy-to/copy-to.json"); + byte[] txt = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/text-in-english.txt"); + + client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); + + index("test", "person", jsonBuilder().startObject().field("file", txt).endObject()); + refresh(); + + CountResponse countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("file")).execute().get(); + assertThat(countResponse.getCount(), equalTo(1l)); + + countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("copy")).execute().get(); + assertThat(countResponse.getCount(), equalTo(1l)); + } + + @Test + public void testCopyToSubField() throws Exception { + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json"); + byte[] txt = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/text-in-english.txt"); + + client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); + + index("test", "person", jsonBuilder().startObject().field("file", txt).endObject()); + refresh(); + + CountResponse countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("file")).execute().get(); + assertThat(countResponse.getCount(), equalTo(1l)); + + countResponse = client().prepareCount("test").setQuery(queryStringQuery("Queen").defaultField("copy")).execute().get(); + assertThat(countResponse.getCount(), equalTo(1l)); + } + } diff --git a/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json b/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json new file mode 100644 index 00000000000..158625c8e09 --- /dev/null +++ b/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to-subfield.json @@ -0,0 +1,19 @@ +{ + "person": { + "properties": { + "file": { + "type": "attachment", + "path": "full", + "fields": { + "file": { + "type": "string", + "copy_to": "copy" + } + } + }, + "copy": { + "type": "string" + } + } + } +} diff --git a/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to.json b/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to.json new file mode 100644 index 00000000000..93333c3842b --- /dev/null +++ b/src/test/resources/org/elasticsearch/index/mapper/copy-to/copy-to.json @@ -0,0 +1,13 @@ +{ + "person": { + "properties": { + "file": { + "type": "attachment", + "copy_to": "copy" + }, + "copy": { + "type": "attachment" + } + } + } +}