copy_to
editcopy_to
editThe copy_to parameter allows you to copy the values of multiple
fields into a group field, which can then be queried as a single
field.
If you often search multiple fields, you can improve search speeds by using
copy_to to search fewer fields. See Search as few fields as possible.
For example, the first_name and last_name fields can be copied to
the full_name field as follows:
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
properties: {
first_name: {
type: 'text',
copy_to: 'full_name'
},
last_name: {
type: 'text',
copy_to: 'full_name'
},
full_name: {
type: 'text'
}
}
}
}
)
puts response
response = client.index(
index: 'my-index-000001',
id: 1,
body: {
first_name: 'John',
last_name: 'Smith'
}
)
puts response
response = client.search(
index: 'my-index-000001',
body: {
query: {
match: {
full_name: {
query: 'John Smith',
operator: 'and'
}
}
}
}
)
puts response
PUT my-index-000001
{
"mappings": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
PUT my-index-000001/_doc/1
{
"first_name": "John",
"last_name": "Smith"
}
GET my-index-000001/_search
{
"query": {
"match": {
"full_name": {
"query": "John Smith",
"operator": "and"
}
}
}
}
|
The values of the |
|
|
The |
Some important points:
- It is the field value which is copied, not the terms (which result from the analysis process).
-
The original
_sourcefield will not be modified to show the copied values. -
The same value can be copied to multiple fields, with
"copy_to": [ "field_1", "field_2" ] -
You cannot copy recursively via intermediary fields such as a
copy_toonfield_1tofield_2andcopy_toonfield_2tofield_3expecting indexing intofield_1will eventuate infield_3, instead use copy_to directly to multiple fields from the originating field. -
If the target field does not exist in the index mappings, the usual
dynamic mapping behavior applies. By default, with
dynamicset totrue, a non-existent target field will be dynamically added to the index mappings. Ifdynamicis set tofalse, the target field will not be added to the index mappings, and the value will not be copied. Ifdynamicis set tostrict, copying to a non-existent field will result in an error.
copy_to is not supported for field types where values take the form of objects, e.g. date_range