Elasticsearch的7.9.0版本配置索引模板template不生效问题

  采坑了。首先吐槽一下,下载elasticsearch的最新版本7.9.0了,配置索引模板跟原来6.X.X、6.X.X之前的都有点差别。新版的api中除了_template,又有了_index_template和_component_template,很容易混淆,让人不知所措。好了回归正题,我们这里使用的特指7.9.0的_template这个api。

  事情是这样的,我在logstash配置了一个output指向elasticsearch的template:

output {
    elasticsearch{
        hosts => "localhost:9200"
        index => "hello-world-%{+YYYY.MM.dd}"
        manage_template => true
        template_name => "hello-world"
        template_overwrite => true
        template => "D:elklogstash-7.9.0confighello-world.json"
    }
}

  hello-world.json内容:

{
    "index_patterns": ["hello-world-%{+YYYY.MM.dd}"],
    "order": 0,
    "settings": {
        "index.refresh_interval": "10s"
    },
    "mappings": {
        "properties": {
            "createTime": {
                "type": "long"                
            },
            "sessionId": {
                "type": "text",    
                "fielddata": true,
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "chart": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
    }
}

  结果是创建的hello-world-2020.09.09索引的mapping并非模板中所指定的,而是按动态模板来的。明眼人也许一样就发现了问题:我把logstash的索引样式hello-world-%{+YYYY.MM.dd}直接复制给了模板中的index_patterns赋值了。这会导致什么问题呢?让我们来kibana中试验一下:

  1、先创建上面的索引模板,执行

PUT _template/hello-world
{
    "index_patterns": ["hello-world-%{+YYYY.MM.dd}"],
    "order": 0,
    "settings": {
        "index.refresh_interval": "10s"
    },
    "mappings": {
        "properties": {
            "createTime": {
                "type": "long"                
            },
            "sessionId": {
                "type": "text",    
                "fielddata": true,
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "chart": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
    }
}

  2、创建索引并插入数据(注意新模板对老索引不生效,可以把索引删掉重建):

POST hello-world-2020.09.10/_doc/1
{
  "createTime": 1599185288000,
  "chart": "今天天气怎么样",
  "sid":"12345"
}

  3、看下该索引的mapping:

GET hello-world-2020.09.10/_mapping

  结果是:

{
  "hello-world-2020.09.10" : {
    "mappings" : {
      "properties" : {
        "chart" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "createTime" : {
          "type" : "long"
        },
        "sid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

  

  嗯,结果是hello-world-2020.09.10索引的映射都是elasticsearch自动识别创建出来的,或者说它匹配不到我们的hello-world模板而使用了默认的mapping。模板的作用就是创建索引时能指定映射,现在模板不生效了。我尝试了各种设置,发现不使用模板,直接给hello-world-2020.09.10索引指定mapping是没有问题的,所以问题不在映射配置上,而在于模板本身。然后就被误导了,改了order的优先级顺序,尝试了开篇提到的各种es版本的配置差别和其他api。最后把各种坑都采完了,才恍然发现我的索引模式可能没匹配上。问题就出现在模板的第一行,我把index_patterns改成hello-word*,创建索引时就使用了模板了。

  1、再次put模板覆盖一下:

  

  2、新建一个新索引:

  3、看它的mapping:

  我们看到新索引hello-world-2020.09.11被模板识别到了。所以,日志索引就别在模板中的index_patterns乱添样式了,直接用日志前缀加*通配就好了。

  

原文地址:https://www.cnblogs.com/wuxun1997/p/13643325.html