Elasticsearch 映射 select 所有字段通过模板更改它们的数据类型 Elasticsearch

Elasticsearch mapping select all fields via template to change their data type Elasticsearch

大家好我正在使用 elasticsearch-template.json 将我所有字段的数据类型设置为字符串。以下是模板的片段:

{
    "template": "logstash-*",
    "settings": {
        "index.refresh_interval": "5s",
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings": {
        "logs": {
            "_all": {
                "enabled": true
            },
            "properties": {
                "level1": {
                    "properties": {
                      "level2": {
                        "properties": {
                            "_all": {"type": "string"}
                        }
                        }
                    }
                }
            }
        }
    }
}

在 2 级下,我创建了很多字段,我想将它们全部设置为字符串,我该如何设置。我已经尝试将“*”字符和“%”字符添加到 select 所有字段。但不幸的是,它只是作为一个新字段添加到映射中。如何在模板中指定select某个级别下的所有字段?

我相信您正在寻找的是 dynamic_templates 并使用 path_match 而不是 match。这演示了它是如何工作的:

curl -DELETE localhost:9200/test-*
curl -XDELETE http://localhost:9200/_template/test
curl -XPOST http://localhost:9200/_template/test -d '
{
    "template": "test-*",
    "mappings": {
        "_default_": {
            "dynamic_templates": [
            {
                "level1_level2_all": {
                    "path_match": "level1.level2.*",
                    "match_mapping_type": "*",
                    "mapping": {
                        "index": "not_analyzed",
                        "type": "string"
                    }
                }
            }
            ]
        }
    }
}
'

curl -XPOST http://localhost:9200/test-1/a -d '
{
    "level1": {
        "level2": {
            "x":1
        }
    }
}'
curl -XPOST http://localhost:9200/test-1/a -d '
{
    "level1": {
        "level2": {
            "y":1
        }
    }
}'

curl http://localhost:9200/test-1/_mapping?pretty

其输出为:

  "test-1" : {
    "mappings" : {
      "_default_" : {
        "dynamic_templates" : [ {
          "level1_level2_all" : {
            "mapping" : {
              "index" : "not_analyzed",
              "type" : "string"
            },
            "match_mapping_type" : "*",
            "path_match" : "level1.level2.*"
          }
        } ],
        "properties" : { }
      },
      "a" : {
        "dynamic_templates" : [ {
          "level1_level2_all" : {
            "mapping" : {
              "index" : "not_analyzed",
              "type" : "string"
            },
            "match_mapping_type" : "*",
            "path_match" : "level1.level2.*"
          }
        } ],
        "properties" : {
          "level1" : {
            "properties" : {
              "level2" : {
                "properties" : {
                  "x" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  },
                  "y" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}