【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 返回的值是一个数组。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值