复制到
参数 copy_to
允许您将多个字段的值复制到一个单独的字段中。如果您经常跨多个字段进行搜索,此参数会很有用,因为它允许您转而搜索该分组字段。
仅复制字段值,而不复制分析过程产生的词元。原始的 _source
字段保持不变,并且可以使用 copy_to
参数将相同的值复制到多个字段。但是,不支持通过中间字段进行递归复制;相反,请直接从原始字段使用 copy_to
到多个目标字段。
示例
以下示例使用 copy_to
参数通过产品的名称和描述进行搜索,并将这些值复制到一个单独的字段中。
PUT my-products-index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"copy_to": "product_info"
},
"description": {
"type": "text",
"copy_to": "product_info"
},
"product_info": {
"type": "text"
},
"price": {
"type": "float"
}
}
}
}
PUT my-products-index/_doc/1
{
"name": "Wireless Headphones",
"description": "High-quality wireless headphones with noise cancellation",
"price": 99.99
}
PUT my-products-index/_doc/2
{
"name": "Bluetooth Speaker",
"description": "Portable Bluetooth speaker with long battery life",
"price": 49.99
}
在此示例中,name
和 description
字段的值被复制到 product_info
字段中。现在,您可以通过查询 product_info
字段来搜索产品,如下所示:
GET my-products-index/_search
{
"query": {
"match": {
"product_info": "wireless headphones"
}
}
}
响应
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.9061546,
"hits": [
{
"_index": "my-products-index",
"_id": "1",
"_score": 1.9061546,
"_source": {
"name": "Wireless Headphones",
"description": "High-quality wireless headphones with noise cancellation",
"price": 99.99
}
}
]
}
}