Dynamic templates(动态模板)

动态模板允许你定义可以用于动态添加的字段的自定义映射:

  • 由Elasticsearch中的match_mapping_type检测到数据类型。
  • 字段的名称可以是match(匹配)和unmatch(不匹配)或match_pattern(模式匹配)。
  • 全点路径的字段可以是path_match(路径匹配)和path_unmatch(不匹配路径)。

原始字段名称{name}和检测导的数据类型{dynamic_type}模板变量可以在映射规范中用作占位符。

Tips

仅当字段包含具体值(不为空或空数组)时才添加动态字段映射。这意味着如果在dynamic_template中使用null_value选项,只能在该字段具有的具体值已被索引第一个文档之后才会应用该值。

动态模板是指定为命名对象的一个数组:

  1. "dynamic_templates": [
  2. {
  3. "my_template_name": { #1
  4. ... match conditions ... #2
  5. "mapping": { ... } #3
  6. }
  7. },
  8. ...
  9. ]

| 1 | 模板名称可以是任意字符串。 | | 2 | 匹配条件可以包括以下任何一种:match_mapping_type(匹配映射类型),match(匹配),match_pattern(模式匹配),unmatch(不匹配),path_match(路径匹配),path_unmatch(取消路径匹配)。 | | 3 | 匹配字段应该使用的映射。 |

模板按顺序进行处理-第一个匹配模板达到要求。可以使用PUT mapping API将新的模板附加到列表的尾部。如果新的模板与现有的模板同名,它将会替换旧的版本。

match_mapping_type(匹配映射类型)

match_mapping_type通过动态字段映射检测数据类型的匹配,换句话说,就是Elasticsearch认为该字段应该具有的数据类型。只能自动检测以下数据类型:boolean(布尔类型),date(日期),double(浮点型),long(长整型),object(对象类型),string(字符类型)。同时,它也接受*来匹配所有数据类型。

例如,如果我们要将所有整数字段映射为integer(整型)而不是long(长整型),并且所有string(字符串类型)字段都是text(文本)和keyword(关键词),我们可以使用以下模板:

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "integers": {
  8. "match_mapping_type": "long",
  9. "mapping": {
  10. "type": "integer"
  11. }
  12. }
  13. },
  14. {
  15. "strings": {
  16. "match_mapping_type": "string",
  17. "mapping": {
  18. "type": "text",
  19. "fields": {
  20. "raw": {
  21. "type": "keyword",
  22. "ignore_above": 256
  23. }
  24. }
  25. }
  26. }
  27. }
  28. ]
  29. }
  30. }}
  31. PUT my_index/my_type/1{
  32. "my_integer": 5, #1
  33. "my_string": "Some string" } #2

| 1 | my_integer字段被映射为integer(整数类型)。 | | 2 | my_string字段被映射为text(文本类型),并且是keword(关键字)多字段。 |

match and unmatch(匹配和不匹配)

match(匹配)参数使用模式匹配字段名称,而unmatch(不匹配)使用模式排除match(匹配)的字段。

以下示例匹配名称为long_开头(除了以_text结尾的字段字符串除外)的所有string(字符串类型)字段,并将其映射为long(长整型)字段:

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "longs_as_strings": {
  8. "match_mapping_type": "string",
  9. "match": "long_*",
  10. "unmatch": "*_text",
  11. "mapping": {
  12. "type": "long"
  13. }
  14. }
  15. }
  16. ]
  17. }
  18. }}
  19. PUT my_index/my_type/1{
  20. "long_num": "5", #1
  21. "long_text": "foo" } #2

| 1 | long_num字段被映射为long(长整型)。 | | 2 | long_text字段使用默认的string(字符串类型)映射。 |

match_pattern(模式匹配)

match_pattern参数调整match参数的行为,使其在字段名称上支持匹配完整的Java正则表达式,而不是简单的通配符,例如:

  1. "match_pattern": "regex",
  2. "match": "^profit_\d+$"

path_match and path_unmatch(路径匹配和不匹配路径)

path_match和path_unmatch参数的工作方式与match和unmathc相同,但是字段运行在完整的路径上,而不仅仅是最终的名称,例如:some_object.*.some_field。

此示例将name对象中的任何字段的值复制到顶级full_name字段,但middle字段除外:

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "full_name": {
  8. "path_match": "name.*",
  9. "path_unmatch": "*.middle",
  10. "mapping": {
  11. "type": "text",
  12. "copy_to": "full_name"
  13. }
  14. }
  15. }
  16. ]
  17. }
  18. }}
  19. PUT my_index/my_type/1{
  20. "name": {
  21. "first": "Alice",
  22. "middle": "Mary",
  23. "last": "White" }}

{name} and {dynamic_type}

在映射中{name}和{dynamic_type}占位符会被替换为字段名称和检测到的动态类型。以下示例将所有的字符串类型设置为使用与该字段名称相同的analyzer(分析器),并禁用所有非字符串字段的doc_values:

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "named_analyzers": {
  8. "match_mapping_type": "string",
  9. "match": "*",
  10. "mapping": {
  11. "type": "text",
  12. "analyzer": "{name}"
  13. }
  14. }
  15. },
  16. {
  17. "no_doc_values": {
  18. "match_mapping_type":"*",
  19. "mapping": {
  20. "type": "{dynamic_type}",
  21. "doc_values": false
  22. }
  23. }
  24. }
  25. ]
  26. }
  27. }}
  28. PUT my_index/my_type/1{
  29. "english": "Some English text", #1
  30. "count": 5 } #2

| 1 | english字段通过english分析器映射为string(字符串)字段。 | | 2 | count字段通过禁用的doc_values映射为long(长整型)字段。 |

Template examples(模板示例)

以下是一些可能有用的动态模板示例:

Structured search(结构化搜索)

默认情况下,elasticsearch将字符串字段映射为具有子keyword(关键字)字段的text(文本)字段。但是,如果你只想索引结构化内容,对全文检索不感兴趣,你可以使用elasticsearch将你的字段映射为“关键字”。请注意,这意味着为了搜索这些字段,你必须搜索与索引完全相同的值。

  1. PUT my_index
  2. { "mappings": {
  3. "my_type": {
  4. "dynamic_templates": [
  5. {
  6. "strings_as_keywords": {
  7. "match_mapping_type": "string",
  8. "mapping": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. ]
  14. }
  15. }}
text-only mappings for strings(字符串的纯文字映射)

与上一个例子相反,如果你唯一关心的字符串字段是全文检索,并且如果你不打算在该字符串字段上运行聚合,排序或精确搜索,你可以告诉elasticsearch将它仅映射为文本字段(这是5.0之前的默认行为):

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "strings_as_text": {
  8. "match_mapping_type": "string",
  9. "mapping": {
  10. "type": "text"
  11. }
  12. }
  13. }
  14. ]
  15. }
  16. }}
Disabled norms(禁用规范)

Norms(规范)是索引时评分的因子。如果你不关心评分,不按分数排序文档,则可能会出现这种情况,你可以在索引中禁用这些评分因子的存储,这样能够节省一些空间。

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. { "strings_as_keywords": {
  7. "match_mapping_type": "string",
  8. "mapping": {
  9. "type": "text",
  10. "norms": false,
  11. "fields": {
  12. "keyword": {
  13. "type": "keyword",
  14. "ignore_above": 256
  15. }
  16. }
  17. }
  18. }
  19. }
  20. ]
  21. }
  22. }}

子keyword字段出现在此模板中,用以同动态映射的默认规则保持一致。当然,如果你不需要它们,你可以按上一节所述删除它,因为你不需要在此字段上执行精确搜索或聚合。

Time-series(时间序列)

当使用elasticsearch进行时间序列分析,通常你会经常聚合许多数字字段,但不会过滤。在这种情况下,你可以禁用这些字段上的索引来节省磁盘空间,同时可能获得一些索引速度的提升:

  1. PUT my_index
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "dynamic_templates": [
  6. {
  7. "unindexed_longs":{
  8. "match_mapping_type": "long",
  9. "mapping": {
  10. "type": "long",
  11. "index": false
  12. }
  13. }
  14. },
  15. {
  16. "unindexed_doubles": {
  17. "match_mapping_type": "double",
  18. "mapping": {
  19. "type": "float", #1
  20. "index": false
  21. }
  22. }
  23. }
  24. ]
  25. }
  26. }}

| 1 | 像默认的动态映射规则一样,doubles(双精度)映射为floats(浮点),通常情况下是足够准确的,但只需要一半的磁盘空间。 |

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/dynamic-templates.html(修改该链接为官网对应的链接)

译文链接 : Dynamic templates(动态模板)(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 曾少峰ApacheCNApache中文网