> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/mapping/323mapping-parametersff08-ying-she-can-shu-ff09/3235copyto-ff08-he-bing-can-shu-ff09.md).

# copy\_to（合并参数）

`copy_to`参数允许你创建自定义的 [**\_all**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-all-field.html) 字段.换句换来说,可以将多个字段的值复制到`group field`（组字段）,然后可以作为单个字段进行查询.例如, **first\_name**和 `last_name`可以复制到 `full_name`字段中,如下所示 :

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name"  # 1
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name"  # 2
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "full_name": {  # 3
        "query": "John Smith",
        "operator": "and"
      }
    }
  }
}
'
```

| 1 , 2 | `first_name`（名字）和 `last_name`（姓氏）字段复制到`full_name` 字段.                                          |
| ----- | ---------------------------------------------------------------------------------------------- |
| 3     | `first_name`（名字）和`last_name`（姓氏）字段仍然可以分别查询,`full_name`可以通过`first_name`（名字）和`last_name`（姓氏）来查询. |

查看数据

```
GET /my_index/my_type/_search
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
}
```

**一些要点:**

* 复制的是字段值,而不是 `term`（词条）（由分析过程产生）.
* `_source`字段不会被修改来显示复制的值.&#x20;
* 相同的值可以复制到多个字段,通过`"copy_to": [ "field_1", "field_2" ]` 来操作.
