【es】ES 字段复制和字段存储-copy_to 参数

1.copy_to 参数 

copy_to 参数可以实现复制多个其他字段的内容。在搜索时可以只搜索一个字段达到搜索多个字段的效果。
比 multi_match性能更好。

创建索引  
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test008 -H 'Content-Type: application/json' -d  '
{
"mappings":{
    "properties":{
	    "title":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"author":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"abstract":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"full_text":{
           "type": "text"
        }
    }
}
}'

--相当于把:title,author,abstract 的内容都复制到 full_text字段。查询full_text里面任意一个值,可以查询到整个记录的值。
写入数据  
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test008/_doc/1 -H 'Content-Type: application/json' -d  '
{
"title":"how to use mysql",
"author":"xsq",
"abstract":"mysql is to difficult!"
}'

--通过full_text 字段查询 
curl  -u elastic:elastic  -k -XPOST http://192.168.1.1:9200/test008/_search -H 'Content-Type: application/json' -d  '
{
"query":{"match":{"full_text":"xsq"}}
}'

{
    "took":133,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":{
            "value":1,
            "relation":"eq"
        },
        "max_score":0.2876821,
        "hits":[
            {
                "_index":"test008",
                "_type":"_doc",
                "_id":"1",
                "_score":0.2876821,
                "_source":{
                    "title":"how to use mysql",
                    "author":"xsq",
                    "abstract":"mysql is to difficult!"
                }
            }
        ]
    }
}

_source 元数据中并不包含full_text 字段的内容,因为 _source 保存的是构建索引时的原始JSON文本。
如上复制字段 full_text 部落盘。

2.将复制字段保存到磁盘上。

curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test009 -H 'Content-Type: application/json' -d  '
{
"mappings":{
    "properties":{
	    "title":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"author":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"abstract":{
           "type": "text",
		   "copy_to":"full_text"
        },
		"full_text":{
           "type": "text",
		   "store":"true"
        }
    }
}
}'

--写入数据  
curl  -u elastic:elastic  -k -XPUT http://192.168.1.1:9200/test009/_doc/1 -H 'Content-Type: application/json' -d  '
{
"title":"how to use mysql",
"author":"xsq",
"abstract":"mysql is to difficult!"
}'

--再次查看 full_text是否落盘  
使用 stored_fields 获取 full_text 字段的内容 。
curl  -u elastic:elastic  -k -XPOST http://192.168.1.1:9200/test009/_search -H 'Content-Type: application/json' -d  '
{
"stored_fields":["full_text"]
}'

{
    "took":3,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":{
            "value":1,
            "relation":"eq"
        },
        "max_score":1,
        "hits":[
            {
                "_index":"test009",
                "_type":"_doc",
                "_id":"1",
                "_score":1,
                "fields":{
                    "full_text":[
                        "how to use mysql",
                        "xsq",
                        "mysql is to difficult!"
                    ]
                }
            }
        ]
    }
}

可以看到:full_text 字段的内容是由:三个字段拼接成的。
默认除了 _source 字段落盘,其他字段不落盘。fields 返回的值是一个数组。

### Elasticsearch `copy_to` 字段的用法 在Elasticsearch中,`copy_to`参数允许索引中的一个或多个字段的内容被复制到指定的目标字段。这使得可以在查询时通过目标字段来检索来自不同源字段的数据,而无需单独针对每个原始字段执行查询。 #### 定义 `copy_to` 当定义映射(mapping)的时候,在源字段上设置`copy_to`属性指向希望接收拷贝文本的目的字段名称。如果存在多级嵌套结构,则可以使用点号`.`表示路径访问子字段[^1]。 ```json 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" } } } } ``` 上述配置会把`first_name``last_name`两个字段的内容都复制给`full_name`这个字段。因此,任何对`full_name`的全文搜索都将匹配包含于这两个名字部分的信息。 #### 使用场景举例 假设有一个文档如下: ```json POST my-index-000001/_doc/1?refresh=true { "first_name": "John", "last_name" : "Smith" } ``` 此时向索引内新增了一条记录,其中`first_name`为"John"`以及`last_name`为"Smith"。由于之前设置了`copy_to="full_name"`,所以实际上相当于也同时创建了一个名为`full_name`的新字段其值为"John Smith"。 现在可以通过查询`full_name`来进行联合查找: ```json GET /my-index-000001/_search?q=full_name:john smith ``` 此命令将会返回所有满足条件的结果,即使实际存储的是分开的第一名姓氏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值