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