- Elasticsearch 指南其他版本
- 8.17 中的新功能
- Elasticsearch 基础
- 快速入门
- 设置 Elasticsearch
- 升级 Elasticsearch
- 索引模块
- 映射
- 文本分析
- 索引模板
- 数据流
- 摄取管道
- 别名
- 搜索您的数据
- 重新排名
- 查询 DSL
- 聚合
- 地理空间分析
- 连接器
- EQL
- ES|QL
- SQL
- 脚本
- 数据管理
- 自动缩放
- 监视集群
- 汇总或转换数据
- 设置高可用性集群
- 快照和还原
- 保护 Elastic Stack 的安全
- Watcher
- 命令行工具
- elasticsearch-certgen
- elasticsearch-certutil
- elasticsearch-create-enrollment-token
- elasticsearch-croneval
- elasticsearch-keystore
- elasticsearch-node
- elasticsearch-reconfigure-node
- elasticsearch-reset-password
- elasticsearch-saml-metadata
- elasticsearch-service-tokens
- elasticsearch-setup-passwords
- elasticsearch-shard
- elasticsearch-syskeygen
- elasticsearch-users
- 优化
- 故障排除
- 修复常见的集群问题
- 诊断未分配的分片
- 向系统中添加丢失的层
- 允许 Elasticsearch 在系统中分配数据
- 允许 Elasticsearch 分配索引
- 索引将索引分配过滤器与数据层节点角色混合,以在数据层之间移动
- 没有足够的节点来分配所有分片副本
- 单个节点上索引的分片总数已超过
- 每个节点的分片总数已达到
- 故障排除损坏
- 修复磁盘空间不足的数据节点
- 修复磁盘空间不足的主节点
- 修复磁盘空间不足的其他角色节点
- 启动索引生命周期管理
- 启动快照生命周期管理
- 从快照恢复
- 故障排除损坏的存储库
- 解决重复的快照策略失败问题
- 故障排除不稳定的集群
- 故障排除发现
- 故障排除监控
- 故障排除转换
- 故障排除 Watcher
- 故障排除搜索
- 故障排除分片容量健康问题
- 故障排除不平衡的集群
- 捕获诊断信息
- REST API
- API 约定
- 通用选项
- REST API 兼容性
- 自动缩放 API
- 行为分析 API
- 紧凑和对齐文本 (CAT) API
- 集群 API
- 跨集群复制 API
- 连接器 API
- 数据流 API
- 文档 API
- 丰富 API
- EQL API
- ES|QL API
- 功能 API
- Fleet API
- 图表探索 API
- 索引 API
- 别名是否存在
- 别名
- 分析
- 分析索引磁盘使用量
- 清除缓存
- 克隆索引
- 关闭索引
- 创建索引
- 创建或更新别名
- 创建或更新组件模板
- 创建或更新索引模板
- 创建或更新索引模板(旧版)
- 删除组件模板
- 删除悬挂索引
- 删除别名
- 删除索引
- 删除索引模板
- 删除索引模板(旧版)
- 存在
- 字段使用情况统计信息
- 刷新
- 强制合并
- 获取别名
- 获取组件模板
- 获取字段映射
- 获取索引
- 获取索引设置
- 获取索引模板
- 获取索引模板(旧版)
- 获取映射
- 导入悬挂索引
- 索引恢复
- 索引段
- 索引分片存储
- 索引统计信息
- 索引模板是否存在(旧版)
- 列出悬挂索引
- 打开索引
- 刷新
- 解析索引
- 解析集群
- 翻转
- 收缩索引
- 模拟索引
- 模拟模板
- 拆分索引
- 解冻索引
- 更新索引设置
- 更新映射
- 索引生命周期管理 API
- 推理 API
- 信息 API
- 摄取 API
- 许可 API
- Logstash API
- 机器学习 API
- 机器学习异常检测 API
- 机器学习数据帧分析 API
- 机器学习训练模型 API
- 迁移 API
- 节点生命周期 API
- 查询规则 API
- 重新加载搜索分析器 API
- 存储库计量 API
- 汇总 API
- 根 API
- 脚本 API
- 搜索 API
- 搜索应用程序 API
- 可搜索快照 API
- 安全 API
- 身份验证
- 更改密码
- 清除缓存
- 清除角色缓存
- 清除权限缓存
- 清除 API 密钥缓存
- 清除服务帐户令牌缓存
- 创建 API 密钥
- 创建或更新应用程序权限
- 创建或更新角色映射
- 创建或更新角色
- 批量创建或更新角色 API
- 批量删除角色 API
- 创建或更新用户
- 创建服务帐户令牌
- 委托 PKI 身份验证
- 删除应用程序权限
- 删除角色映射
- 删除角色
- 删除服务帐户令牌
- 删除用户
- 禁用用户
- 启用用户
- 注册 Kibana
- 注册节点
- 获取 API 密钥信息
- 获取应用程序权限
- 获取内置权限
- 获取角色映射
- 获取角色
- 查询角色
- 获取服务帐户
- 获取服务帐户凭据
- 获取安全设置
- 获取令牌
- 获取用户权限
- 获取用户
- 授予 API 密钥
- 具有权限
- 使 API 密钥失效
- 使令牌失效
- OpenID Connect 准备身份验证
- OpenID Connect 身份验证
- OpenID Connect 注销
- 查询 API 密钥信息
- 查询用户
- 更新 API 密钥
- 更新安全设置
- 批量更新 API 密钥
- SAML 准备身份验证
- SAML 身份验证
- SAML 注销
- SAML 失效
- SAML 完成注销
- SAML 服务提供商元数据
- SSL 证书
- 激活用户配置文件
- 禁用用户配置文件
- 启用用户配置文件
- 获取用户配置文件
- 建议用户配置文件
- 更新用户配置文件数据
- 具有用户配置文件权限
- 创建跨集群 API 密钥
- 更新跨集群 API 密钥
- 快照和还原 API
- 快照生命周期管理 API
- SQL API
- 同义词 API
- 文本结构 API
- 转换 API
- 使用情况 API
- Watcher API
- 定义
- 迁移指南
- 发行说明
- Elasticsearch 版本 8.17.0
- Elasticsearch 版本 8.16.1
- Elasticsearch 版本 8.16.0
- Elasticsearch 版本 8.15.5
- Elasticsearch 版本 8.15.4
- Elasticsearch 版本 8.15.3
- Elasticsearch 版本 8.15.2
- Elasticsearch 版本 8.15.1
- Elasticsearch 版本 8.15.0
- Elasticsearch 版本 8.14.3
- Elasticsearch 版本 8.14.2
- Elasticsearch 版本 8.14.1
- Elasticsearch 版本 8.14.0
- Elasticsearch 版本 8.13.4
- Elasticsearch 版本 8.13.3
- Elasticsearch 版本 8.13.2
- Elasticsearch 版本 8.13.1
- Elasticsearch 版本 8.13.0
- Elasticsearch 版本 8.12.2
- Elasticsearch 版本 8.12.1
- Elasticsearch 版本 8.12.0
- Elasticsearch 版本 8.11.4
- Elasticsearch 版本 8.11.3
- Elasticsearch 版本 8.11.2
- Elasticsearch 版本 8.11.1
- Elasticsearch 版本 8.11.0
- Elasticsearch 版本 8.10.4
- Elasticsearch 版本 8.10.3
- Elasticsearch 版本 8.10.2
- Elasticsearch 版本 8.10.1
- Elasticsearch 版本 8.10.0
- Elasticsearch 版本 8.9.2
- Elasticsearch 版本 8.9.1
- Elasticsearch 版本 8.9.0
- Elasticsearch 版本 8.8.2
- Elasticsearch 版本 8.8.1
- Elasticsearch 版本 8.8.0
- Elasticsearch 版本 8.7.1
- Elasticsearch 版本 8.7.0
- Elasticsearch 版本 8.6.2
- Elasticsearch 版本 8.6.1
- Elasticsearch 版本 8.6.0
- Elasticsearch 版本 8.5.3
- Elasticsearch 版本 8.5.2
- Elasticsearch 版本 8.5.1
- Elasticsearch 版本 8.5.0
- Elasticsearch 版本 8.4.3
- Elasticsearch 版本 8.4.2
- Elasticsearch 版本 8.4.1
- Elasticsearch 版本 8.4.0
- Elasticsearch 版本 8.3.3
- Elasticsearch 版本 8.3.2
- Elasticsearch 版本 8.3.1
- Elasticsearch 版本 8.3.0
- Elasticsearch 版本 8.2.3
- Elasticsearch 版本 8.2.2
- Elasticsearch 版本 8.2.1
- Elasticsearch 版本 8.2.0
- Elasticsearch 版本 8.1.3
- Elasticsearch 版本 8.1.2
- Elasticsearch 版本 8.1.1
- Elasticsearch 版本 8.1.0
- Elasticsearch 版本 8.0.1
- Elasticsearch 版本 8.0.0
- Elasticsearch 版本 8.0.0-rc2
- Elasticsearch 版本 8.0.0-rc1
- Elasticsearch 版本 8.0.0-beta1
- Elasticsearch 版本 8.0.0-alpha2
- Elasticsearch 版本 8.0.0-alpha1
- 依赖项和版本
动态模板
编辑动态模板
编辑动态模板允许你更好地控制 Elasticsearch 如何映射你的数据,超出默认的动态字段映射规则。 你可以通过将 dynamic 参数设置为 true
或 runtime
来启用动态映射。 然后,你可以使用动态模板来定义自定义映射,这些映射可以根据匹配条件应用于动态添加的字段。
-
match_mapping_type
和unmatch_mapping_type
对 Elasticsearch 检测到的数据类型进行操作 -
match
和unmatch
使用模式来匹配字段名称 -
path_match
和path_unmatch
对字段的完整点式路径进行操作 - 如果动态模板没有定义
match_mapping_type
、match
或path_match
,它将不会匹配任何字段。 你仍然可以在批量请求的dynamic_templates
部分通过名称引用该模板。
在映射规范中使用 {name}
和 {dynamic_type}
模板变量作为占位符。
仅当字段包含具体值时,才会添加动态字段映射。当字段包含 null
或空数组时,Elasticsearch 不会添加动态字段映射。如果 dynamic_template
中使用了 null_value
选项,则只有在索引了第一个包含该字段的具体值的文档之后,才会应用该选项。
动态模板被指定为命名对象的数组
"dynamic_templates": [ { "my_template_name": { ... match conditions ... "mapping": { ... } } }, ... ]
模板名称可以是任何字符串值。 |
|
匹配条件可以包括以下任何一个: |
|
匹配的字段应使用的映射。 |
验证动态模板
编辑如果提供的映射包含无效的映射片段,则会返回验证错误。 验证发生在索引时应用动态模板时,以及在大多数情况下,更新动态模板时。 提供无效的映射片段可能会导致在某些条件下动态模板的更新或验证失败
- 如果未指定
match_mapping_type
,但该模板对于至少一个预定义的映射类型有效,则该映射片段被认为是有效的。但是,如果在索引时将与该模板匹配的字段索引为不同的类型,则会在索引时返回验证错误。例如,配置没有match_mapping_type
的动态模板被认为是有效的字符串类型,但如果将与该动态模板匹配的字段索引为 long,则会在索引时返回验证错误。建议将match_mapping_type
配置为预期的 JSON 类型,或在映射片段中配置所需的type
。 - 如果在映射片段中使用了
{name}
占位符,则在更新动态模板时会跳过验证。 这是因为此时字段名称未知。 相反,验证发生在索引时应用模板时。
模板按顺序处理 — 第一个匹配的模板获胜。 当通过更新映射 API 放入新的动态模板时,所有现有模板都会被覆盖。 这允许在最初添加后重新排序或删除动态模板。
在动态模板中映射运行时字段
编辑如果你希望 Elasticsearch 将某种类型的新字段动态映射为运行时字段,请在索引映射中设置 "dynamic":"runtime"
。 这些字段不会被索引,并且在查询时从 _source
加载。
或者,你可以使用默认的动态映射规则,然后创建动态模板以将特定字段映射为运行时字段。 你可以在索引映射中设置 "dynamic":"true"
,然后创建一个动态模板以将某种类型的新字段映射为运行时字段。
假设你有的数据中每个字段都以 ip_
开头。 根据动态映射规则,Elasticsearch 将任何通过 numeric
检测的 string
映射为 float
或 long
。 但是,你可以创建一个动态模板,将新字符串映射为 ip
类型的运行时字段。
以下请求定义了一个名为 strings_as_ip
的动态模板。 当 Elasticsearch 检测到与 ip*
模式匹配的新 string
字段时,它会将这些字段映射为 ip
类型的运行时字段。 因为 ip
字段不会被动态映射,所以你可以将此模板与 "dynamic":"true"
或 "dynamic":"runtime"
一起使用。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "strings_as_ip": { "match_mapping_type": "string", "match": "ip*", "runtime": { "type": "ip" } } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { strings_as_ip: { match_mapping_type: 'string', match: 'ip*', runtime: { type: 'ip' } } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { strings_as_ip: { match_mapping_type: "string", match: "ip*", runtime: { type: "ip", }, }, }, ], }, }); console.log(response);
PUT my-index-000001/ { "mappings": { "dynamic_templates": [ { "strings_as_ip": { "match_mapping_type": "string", "match": "ip*", "runtime": { "type": "ip" } } } ] } }
请参阅此示例,了解如何使用动态模板将 string
字段映射为索引字段或运行时字段。
match_mapping_type
和 unmatch_mapping_type
编辑match_mapping_type
参数按 JSON 解析器检测到的数据类型匹配字段,而 unmatch_mapping_type
根据数据类型排除字段。
因为 JSON 不区分 long
和 integer
或 double
和 float
,所以任何解析的浮点数都被视为 double
JSON 数据类型,而任何解析的 integer
数字都被视为 long
。
使用动态映射,Elasticsearch 将始终选择更宽的数据类型。 一个例外是 float
,它比 double
需要更少的存储空间,并且对于大多数应用程序来说足够精确。 运行时字段不支持 float
,这就是为什么 "dynamic":"runtime"
使用 double
。
Elasticsearch 自动检测以下数据类型
Elasticsearch 数据类型 |
||
JSON 数据类型 |
|
|
|
未添加字段 |
未添加字段 |
|
|
|
|
|
|
|
|
|
|
|
未添加字段 |
|
取决于数组中的第一个非 |
取决于数组中的第一个非 |
通过日期检测的 |
|
|
通过数字检测的 |
|
|
未通过 |
带有 |
|
你可以为 match_mapping_type
或 unmatch_mapping_type
参数指定单个数据类型或数据类型列表。 你还可以使用通配符 (*
) 作为 match_mapping_type
参数,以匹配所有数据类型。
例如,如果我们想将所有整数字段映射为 integer
而不是 long
,并将所有 string
字段映射为 text
和 keyword
,我们可以使用以下模板
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "numeric_counts": { "match_mapping_type": [ "long", "double" ], "match": "count", "mapping": { "type": "{dynamic_type}", "index": False } } }, { "integers": { "match_mapping_type": "long", "mapping": { "type": "integer" } } }, { "strings": { "match_mapping_type": "string", "mapping": { "type": "text", "fields": { "raw": { "type": "keyword", "ignore_above": 256 } } } } }, { "non_objects_keyword": { "match_mapping_type": "*", "unmatch_mapping_type": "object", "mapping": { "type": "keyword" } } } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "my_integer": 5, "my_string": "Some string", "my_boolean": "false", "field": { "count": 4 } }, ) print(resp1)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { numeric_counts: { match_mapping_type: [ 'long', 'double' ], match: 'count', mapping: { type: '{dynamic_type}', index: false } } }, { integers: { match_mapping_type: 'long', mapping: { type: 'integer' } } }, { strings: { match_mapping_type: 'string', mapping: { type: 'text', fields: { raw: { type: 'keyword', ignore_above: 256 } } } } }, { non_objects_keyword: { match_mapping_type: '*', unmatch_mapping_type: 'object', mapping: { type: 'keyword' } } } ] } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { my_integer: 5, my_string: 'Some string', my_boolean: 'false', field: { count: 4 } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { numeric_counts: { match_mapping_type: ["long", "double"], match: "count", mapping: { type: "{dynamic_type}", index: false, }, }, }, { integers: { match_mapping_type: "long", mapping: { type: "integer", }, }, }, { strings: { match_mapping_type: "string", mapping: { type: "text", fields: { raw: { type: "keyword", ignore_above: 256, }, }, }, }, }, { non_objects_keyword: { match_mapping_type: "*", unmatch_mapping_type: "object", mapping: { type: "keyword", }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { my_integer: 5, my_string: "Some string", my_boolean: "false", field: { count: 4, }, }, }); console.log(response1);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "numeric_counts": { "match_mapping_type": ["long", "double"], "match": "count", "mapping": { "type": "{dynamic_type}", "index": false } } }, { "integers": { "match_mapping_type": "long", "mapping": { "type": "integer" } } }, { "strings": { "match_mapping_type": "string", "mapping": { "type": "text", "fields": { "raw": { "type": "keyword", "ignore_above": 256 } } } } }, { "non_objects_keyword": { "match_mapping_type": "*", "unmatch_mapping_type": "object", "mapping": { "type": "keyword" } } } ] } } PUT my-index-000001/_doc/1 { "my_integer": 5, "my_string": "Some string", "my_boolean": "false", "field": {"count": 4} }
|
|
|
|
|
|
|
match
和 unmatch
编辑match
参数使用一个或多个模式来匹配字段名称,而 unmatch
使用一个或多个模式来排除 match
匹配的字段。
match_pattern
参数调整 match
参数的行为,以支持在字段名称上进行完整的 Java 正则表达式匹配,而不是简单的通配符。 例如
"match_pattern": "regex", "match": "^profit_\d+$"
以下示例匹配名称以 long_
开头的所有 string
字段(除了以 _text
结尾的字段),并将它们映射为 long
字段
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "long_num": "5", "long_text": "foo" }, ) print(resp1)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { longs_as_strings: { match_mapping_type: 'string', match: 'long_*', unmatch: '*_text', mapping: { type: 'long' } } } ] } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { long_num: '5', long_text: 'foo' } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { longs_as_strings: { match_mapping_type: "string", match: "long_*", unmatch: "*_text", mapping: { type: "long", }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { long_num: "5", long_text: "foo", }, }); console.log(response1);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] } } PUT my-index-000001/_doc/1 { "long_num": "5", "long_text": "foo" }
你可以为 match
或 unmatch
字段指定使用 JSON 数组的模式列表。
下一个示例匹配名称以 ip_
开头或以 _ip
结尾的所有字段,但以 one
开头或以 two
结尾的字段除外,并将它们映射为 ip
字段
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "ip_fields": { "match": [ "ip_*", "*_ip" ], "unmatch": [ "one*", "*two" ], "mapping": { "type": "ip" } } } ] }, ) print(resp) resp1 = client.index( index="my-index", id="1", document={ "one_ip": "will not match", "ip_two": "will not match", "three_ip": "12.12.12.12", "ip_four": "13.13.13.13" }, ) print(resp1)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { ip_fields: { match: [ 'ip_*', '*_ip' ], unmatch: [ 'one*', '*two' ], mapping: { type: 'ip' } } } ] } } ) puts response response = client.index( index: 'my-index', id: 1, body: { one_ip: 'will not match', ip_two: 'will not match', three_ip: '12.12.12.12', ip_four: '13.13.13.13' } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { ip_fields: { match: ["ip_*", "*_ip"], unmatch: ["one*", "*two"], mapping: { type: "ip", }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index", id: 1, document: { one_ip: "will not match", ip_two: "will not match", three_ip: "12.12.12.12", ip_four: "13.13.13.13", }, }); console.log(response1);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "ip_fields": { "match": ["ip_*", "*_ip"], "unmatch": ["one*", "*two"], "mapping": { "type": "ip" } } } ] } } PUT my-index/_doc/1 { "one_ip": "will not match", "ip_two": "will not match", "three_ip": "12.12.12.12", "ip_four": "13.13.13.13" }
path_match
和 path_unmatch
编辑path_match
和 path_unmatch
参数的工作方式与 match
和 unmatch
相同,但操作的是字段的完整点式路径,而不仅仅是最终名称,例如 some_object.*.some_field
。
此示例将 name
对象中任何字段的值复制到顶层 full_name
字段,除了 middle
字段
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "full_name": { "path_match": "name.*", "path_unmatch": "*.middle", "mapping": { "type": "text", "copy_to": "full_name" } } } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "name": { "first": "John", "middle": "Winston", "last": "Lennon" } }, ) print(resp1)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { full_name: { path_match: 'name.*', path_unmatch: '*.middle', mapping: { type: 'text', copy_to: 'full_name' } } } ] } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { name: { first: 'John', middle: 'Winston', last: 'Lennon' } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { full_name: { path_match: "name.*", path_unmatch: "*.middle", mapping: { type: "text", copy_to: "full_name", }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { name: { first: "John", middle: "Winston", last: "Lennon", }, }, }); console.log(response1);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "full_name": { "path_match": "name.*", "path_unmatch": "*.middle", "mapping": { "type": "text", "copy_to": "full_name" } } } ] } } PUT my-index-000001/_doc/1 { "name": { "first": "John", "middle": "Winston", "last": "Lennon" } }
以下示例对 path_match
和 path_unmatch
都使用模式数组。
将 name
对象或 user.name
对象中任何字段的值复制到顶层 full_name
字段,除了 middle
和 midinitial
字段
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "full_name": { "path_match": [ "name.*", "user.name.*" ], "path_unmatch": [ "*.middle", "*.midinitial" ], "mapping": { "type": "text", "copy_to": "full_name" } } } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "name": { "first": "John", "middle": "Winston", "last": "Lennon" } }, ) print(resp1) resp2 = client.index( index="my-index-000001", id="2", document={ "user": { "name": { "first": "Jane", "midinitial": "M", "last": "Salazar" } } }, ) print(resp2)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { full_name: { path_match: [ 'name.*', 'user.name.*' ], path_unmatch: [ '*.middle', '*.midinitial' ], mapping: { type: 'text', copy_to: 'full_name' } } } ] } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { name: { first: 'John', middle: 'Winston', last: 'Lennon' } } ) puts response response = client.index( index: 'my-index-000001', id: 2, body: { user: { name: { first: 'Jane', midinitial: 'M', last: 'Salazar' } } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { full_name: { path_match: ["name.*", "user.name.*"], path_unmatch: ["*.middle", "*.midinitial"], mapping: { type: "text", copy_to: "full_name", }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { name: { first: "John", middle: "Winston", last: "Lennon", }, }, }); console.log(response1); const response2 = await client.index({ index: "my-index-000001", id: 2, document: { user: { name: { first: "Jane", midinitial: "M", last: "Salazar", }, }, }, }); console.log(response2);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "full_name": { "path_match": ["name.*", "user.name.*"], "path_unmatch": ["*.middle", "*.midinitial"], "mapping": { "type": "text", "copy_to": "full_name" } } } ] } } PUT my-index-000001/_doc/1 { "name": { "first": "John", "middle": "Winston", "last": "Lennon" } } PUT my-index-000001/_doc/2 { "user": { "name": { "first": "Jane", "midinitial": "M", "last": "Salazar" } } }
请注意,path_match
和 path_unmatch
参数除了匹配叶字段外,还匹配对象路径。 例如,索引以下文档会导致错误,因为 path_match
设置还会匹配对象字段 name.title
,该字段无法映射为文本
resp = client.index( index="my-index-000001", id="2", document={ "name": { "first": "Paul", "last": "McCartney", "title": { "value": "Sir", "category": "order of chivalry" } } }, ) print(resp)
response = client.index( index: 'my-index-000001', id: 2, body: { name: { first: 'Paul', last: 'McCartney', title: { value: 'Sir', category: 'order of chivalry' } } } ) puts response
const response = await client.index({ index: "my-index-000001", id: 2, document: { name: { first: "Paul", last: "McCartney", title: { value: "Sir", category: "order of chivalry", }, }, }, }); console.log(response);
PUT my-index-000001/_doc/2 { "name": { "first": "Paul", "last": "McCartney", "title": { "value": "Sir", "category": "order of chivalry" } } }
模板变量
编辑在 mapping
中,{name}
和 {dynamic_type}
占位符会被替换为字段名称和检测到的动态类型。以下示例将所有字符串字段设置为使用与该字段同名的 analyzer
,并为所有非字符串字段禁用 doc_values
。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "named_analyzers": { "match_mapping_type": "string", "match": "*", "mapping": { "type": "text", "analyzer": "{name}" } } }, { "no_doc_values": { "match_mapping_type": "*", "mapping": { "type": "{dynamic_type}", "doc_values": False } } } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="1", document={ "english": "Some English text", "count": 5 }, ) print(resp1)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { named_analyzers: { match_mapping_type: 'string', match: '*', mapping: { type: 'text', analyzer: '{name}' } } }, { no_doc_values: { match_mapping_type: '*', mapping: { type: '{dynamic_type}', doc_values: false } } } ] } } ) puts response response = client.index( index: 'my-index-000001', id: 1, body: { english: 'Some English text', count: 5 } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { named_analyzers: { match_mapping_type: "string", match: "*", mapping: { type: "text", analyzer: "{name}", }, }, }, { no_doc_values: { match_mapping_type: "*", mapping: { type: "{dynamic_type}", doc_values: false, }, }, }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 1, document: { english: "Some English text", count: 5, }, }); console.log(response1);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "named_analyzers": { "match_mapping_type": "string", "match": "*", "mapping": { "type": "text", "analyzer": "{name}" } } }, { "no_doc_values": { "match_mapping_type":"*", "mapping": { "type": "{dynamic_type}", "doc_values": false } } } ] } } PUT my-index-000001/_doc/1 { "english": "Some English text", "count": 5 }
动态模板示例
编辑以下是一些可能有用的动态模板示例
结构化搜索
编辑当您设置 "dynamic":"true"
时,Elasticsearch 会将字符串字段映射为带有 keyword
子字段的 text
字段。如果您只索引结构化内容,而不对全文搜索感兴趣,您可以让 Elasticsearch 将您的字段仅映射为 keyword
字段。但是,您必须搜索与索引的值完全相同的值才能搜索这些字段。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: 'string', mapping: { type: 'keyword' } } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: "string", mapping: { type: "keyword", }, }, }, ], }, }); console.log(response);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] } }
字符串的仅 text
映射
编辑与前面的示例相反,如果您只关心字符串字段的全文搜索,而不打算运行聚合、排序或精确搜索,您可以指示 Elasticsearch 将字符串映射为 text
。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "strings_as_text": { "match_mapping_type": "string", "mapping": { "type": "text" } } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { strings_as_text: { match_mapping_type: 'string', mapping: { type: 'text' } } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { strings_as_text: { match_mapping_type: "string", mapping: { type: "text", }, }, }, ], }, }); console.log(response);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "strings_as_text": { "match_mapping_type": "string", "mapping": { "type": "text" } } } ] } }
或者,您可以在映射的 runtime 部分创建一个动态模板,将您的字符串字段映射为 keyword
字段。当 Elasticsearch 检测到新的 string
类型字段时,这些字段将根据 Elasticsearch 用于向映射添加字段类型的动态映射规则创建为 keyword
类型的 runtime 字段。任何未通过日期检测或数值检测的 string
都会自动映射为 keyword
。
虽然您的 string
字段不会被索引,但它们的值存储在 _source
中,并可以在搜索请求、聚合、过滤和排序中使用。
例如,以下请求创建一个动态模板,将 string
字段映射为 keyword
类型的 runtime 字段。虽然 runtime
定义为空,但新的 string
字段将根据 Elasticsearch 用于向映射添加字段类型的动态映射规则映射为 keyword
runtime 字段。任何未通过日期检测或数值检测的 string
都会自动映射为 keyword
。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "runtime": {} } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: 'string', runtime: {} } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: "string", runtime: {}, }, }, ], }, }); console.log(response);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "runtime": {} } } ] } }
您索引一个简单的文档
resp = client.index( index="my-index-000001", id="1", document={ "english": "Some English text", "count": 5 }, ) print(resp)
response = client.index( index: 'my-index-000001', id: 1, body: { english: 'Some English text', count: 5 } ) puts response
const response = await client.index({ index: "my-index-000001", id: 1, document: { english: "Some English text", count: 5, }, }); console.log(response);
PUT my-index-000001/_doc/1 { "english": "Some English text", "count": 5 }
当您查看映射时,您会看到 english
字段是 keyword
类型的 runtime 字段
resp = client.indices.get_mapping( index="my-index-000001", ) print(resp)
response = client.indices.get_mapping( index: 'my-index-000001' ) puts response
const response = await client.indices.getMapping({ index: "my-index-000001", }); console.log(response);
GET my-index-000001/_mapping
{ "my-index-000001" : { "mappings" : { "dynamic_templates" : [ { "strings_as_keywords" : { "match_mapping_type" : "string", "runtime" : { } } } ], "runtime" : { "english" : { "type" : "keyword" } }, "properties" : { "count" : { "type" : "long" } } } } }
禁用 norms
编辑Norms 是索引时的评分因素。如果您不关心评分(例如,如果您从不按分数对文档进行排序),您可以禁用索引中这些评分因素的存储,并节省一些空间。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "text", "norms": False, "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: 'string', mapping: { type: 'text', norms: false, fields: { keyword: { type: 'keyword', ignore_above: 256 } } } } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { strings_as_keywords: { match_mapping_type: "string", mapping: { type: "text", norms: false, fields: { keyword: { type: "keyword", ignore_above: 256, }, }, }, }, }, ], }, }); console.log(response);
PUT my-index-000001 { "mappings": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "text", "norms": false, "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } ] } }
子 keyword
字段出现在此模板中,以与动态映射的默认规则保持一致。当然,如果您不需要它们,因为您不需要执行精确搜索或聚合此字段,您可以按照上一节中的描述将其删除。
时间序列
编辑当使用 Elasticsearch 进行时间序列分析时,通常会有许多数字字段,您经常会对这些字段进行聚合,但从不进行过滤。在这种情况下,您可以禁用这些字段的索引以节省磁盘空间,也可能会提高索引速度。
resp = client.indices.create( index="my-index-000001", mappings={ "dynamic_templates": [ { "unindexed_longs": { "match_mapping_type": "long", "mapping": { "type": "long", "index": False } } }, { "unindexed_doubles": { "match_mapping_type": "double", "mapping": { "type": "float", "index": False } } } ] }, ) print(resp)
response = client.indices.create( index: 'my-index-000001', body: { mappings: { dynamic_templates: [ { unindexed_longs: { match_mapping_type: 'long', mapping: { type: 'long', index: false } } }, { unindexed_doubles: { match_mapping_type: 'double', mapping: { type: 'float', index: false } } } ] } } ) puts response
const response = await client.indices.create({ index: "my-index-000001", mappings: { dynamic_templates: [ { unindexed_longs: { match_mapping_type: "long", mapping: { type: "long", index: false, }, }, }, { unindexed_doubles: { match_mapping_type: "double", mapping: { type: "float", index: false, }, }, }, ], }, }); console.log(response);
On this page