【问题标题】:Elasticsearch mapping select all fields via template to change their data type ElasticsearchElasticsearch 映射通过模板选择所有字段以更改其数据类型 Elasticsearch
【发布时间】:2016-12-08 10:15:39
【问题描述】:

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

{
    "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"}
                        }
                        }
                    }
                }
            }
        }
    }
}

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

【问题讨论】:

    标签: elasticsearch logstash kibana elastic-stack


    【解决方案1】:

    我相信您正在寻找的是 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"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我先尝试了上面的方法。但是碰巧它接收到的第一个文档将类型设置为字符串,但是对于空白或没有值的字段,它将其设置为对象,然后下一个具有这些空白字段值的文档会引发错误。
    • 您需要删除索引并重新加载数据才能获取模板
    • 是的,这就是我所做的,它正在使用模板,但对于我上面提到的情况,它最初将空字段设置为对象数据类型,然后当下一个文档到达时,字段值为字符串/数字它无法索引它
    • 修复了我的答案,包括 path_match 和一个完整的工作示例
    • 好的..so 在反复​​尝试您的解决方案之后。似乎问题出现在这个用例中......让我们在第一个文档中说你发送的值是空白的,即对于“x”,没有发送任何值......所以 ES 将“x”字段数据类型设置为对象和然后当第二个文档带有一些值时,它会尝试将其转换为相应的数据类型并失败并出现异常:“error”=>{“type”=>“mapper_parsing_exception”,“reason”=>“对象映射 [level1 .level2.x] 尝试将字段 [null] 解析为对象,但找到了具体值"}}}
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 2017-05-13
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多