如何使用 Elasticsearch 中的 dynamic template

前言

之前我们了解到 Elasticsearch index mapping 默认采用 dynamic = true 的方式 自动的把新的字段添加到 mapping 中[1]; 有些时候,Elasticsearch 对于 输入的字段的类型解析是非预期的,如下:

PUT index_temp_test/_doc/1
{
 "my_integer": 5,
 "my_string": "Some string"
}

我们期望 my_interger 类型为 int 而不是 long,如下:

GET index_temp_test/_mapping

输出如下:

{
  "index_temp_test" : {
    "mappings" : {
      "properties" : {
        "my_integer" : {
          "type" : "long"
        },
        "my_string" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

因此,dynamic template 是根据 Elasticsearch 识别的数据类型,可以根据字段名称来动态的设定字段类型。

如何使用 dynamic template

Dynamic template 是配置在 index mapping 中的,我们可以设定一些匹配规则,当匹配到之后,会为匹配到的字段设定 mappings。

1. 根据传入类型来进行映射 - match_mapping_type


设定一个 dynamic template

PUT index_temp_test1
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      }
    ]
  }
} 

插入数据:

PUT index_temp_test1/_doc/1
{
 "my_integer": 5,
 "my_string": "Some string"
}

查看 index template:输出如下:

{
  "index_temp_test1" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "integers" : {
            "match_mapping_type" : "long",
            "mapping" : {
              "type" : "integer"
            }
          }
        }
      ],
      "properties" : {
        "my_integer" : {
          "type" : "integer"
        },
        "my_string" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}


2. 根据字段名称进行映射 - match

在此案例中,我们希望对于 TRUE | FALSE 自动设置为Boolean 并匹配特定字符开头的字段:


POST index_temp_test2/_doc/1
{
 "firstName":"rudonx",
 "isBeiing":"true"
}

查看 index template,发现自动设置为 text

{
  "index_temp_test2" : {
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isBeiing" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

使用 dynamic template 进行动态匹配:我们希望遇到 "is" 开头的字段自动转化为Boolean


设置 template

PUT index_temp_test3
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_boolean": {
          "match_mapping_type": "string",
          "match": "is*",
          "mapping": {
            "type": "boolean"
          }
        }
      }
    ]
  }
}

插入数据:

POST index_temp_test3/_doc/1
{
 "firstName":"rudonx",
 "isBeiing":"true"
}

查看 mapping:发现数据自动转化为了 Boolean

{
  "index_temp_test3" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "strings_as_boolean" : {
            "match" : "is*",
            "match_mapping_type" : "string",
            "mapping" : {
              "type" : "boolean"
            }
          }
        }
      ],
      "properties" : {
        "firstName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isBeiing" : {
          "type" : "boolean"
        }
      }
    }
  }
}

如果输入字段不是is开头,依旧会转化为text:

"properties" : {
        "Beiing" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },

关于 dynamic mapping 的更多用法,您可以参考下面的文档。

参考文档

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

如果您有其他问题,欢迎您联系火山引擎技术支持服务

77
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论