文章目录
0. 前言
在基础篇,我们只介绍了索引 Mapping 的基本用法。
本章将深入探讨日常中较经常使用的 Mapping 其他参数配置。
不想看过程,可直接看最后的总结。
1. 前置知识 - _source
在介绍本章内容时,这边先补充一个知识点:
如果细心观察你会发现,搜索 API 返回的结果都包含在一个叫 _source
的字段中。如下图所示
_source
在 ES 中非常重要。默认情况下,所有的字段都会存储在 _source
中。
以下 API 都需要用到 _source
- 搜索返回结果
update
、update by query
、reindex
API- 高亮显示 API
因此,虽说我们可以通过以下操作将 _source
禁用,但一般不禁用
PUT test20_source
{
"mappings": {
"_source": {
"enabled": false
}
}
}
2. copy_to
- 描述: 将多个字段的值拷贝至 1 个或多个字段
- 作用: 一般用于优化多个 text 字段查询
其使用如下:
PUT test20_copyto
{
"mappings": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
GET test20_copyto/_search
{
"query": {
"match": {
"full_name": "hello"
}
}
}
注意:
copy_to
不会改变_source
的值
3. doc_values
- 描述: 专为字段值存储而设计的列式存储格式。默认情况下,除了
text
,annotated_text
不启用,其他字段默认启用。禁用该字段,可以节约磁盘存储。 - 作用: 允许字段使用排序、聚合、script 字段访问。
其使用如下:
PUT test_doc_value
{
"mappings": {