elk 改为使用 ik 中文分词器

下文是在已经部署了 elk 的情况下,对中文内容加分词。

主要操作步骤

1. es 安装 ik 插件
2. es 配置 logstash 模板,设置 message 字段使用 ik 中文分词器
3. logstash 禁用自动管理模板
4. 重建已存在的索引

具体操作如下。

1. es 安装 ik 插件

过程略。插件内容安装在 es/plugins/ik/ 目录中。安装完成后要重启 es.

2. es 配置 logstash 模板,设置 message 字段使用 ik 中文分词器

最好是借用 kibana 可视化页面。登录 kibana 用户需要相关权限,登录后,点击左边的 Dev Tools. 在左边输入 GET /_template/logstash.

 复制输出的内容,找到以下内容并修改。其一,index_patterns 内容改为 * (根据实际索引的模式来,* 表示从 logstash 来的所有索引都用这个模板).

    "index_patterns" : [
      "*"
    ]


其二,修改 message 字段,指定使用 ik 索引。需要提醒的是 ik_smart 可以改成 ik_max_word. 关于他们之间的区别可以从网上找一下。

      "dynamic_templates" : [
        {
          "message_field" : {
            "path_match" : "message",
            "mapping" : {
              "norms" : false,
              "type" : "text",
              // 这是新增的两行
              "analyzer": "ik_smart",
              "search_analyzer": "ik_smart"
            },
            "match_mapping_type" : "string"
          }
        },


把 logstash 节点下面的内容拿出来单独组成一个 json. 再调用 POST 更改。完整的 json 如下。

POST /_template/logstash
{
    "order" : 0,
    "version" : 60001,
    "index_patterns" : [
      "*"
    ],
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "refresh_interval" : "5s"
      }
    },
    "mappings" : {
      "dynamic_templates" : [
        {
          "message_field" : {
            "path_match" : "message",
            "mapping" : {
              "norms" : false,
              "type" : "text",
              "analyzer": "ik_smart",
              "search_analyzer": "ik_smart"
            },
            "match_mapping_type" : "string"
          }
        },
        {
          "string_fields" : {
            "mapping" : {
              "norms" : false,
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "ignore_above" : 256,
                  "type" : "keyword"
                }
              }
            },
            "match_mapping_type" : "string",
            "match" : "*"
          }
        }
      ],
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "geoip" : {
          "dynamic" : true,
          "properties" : {
            "ip" : {
              "type" : "ip"
            },
            "latitude" : {
              "type" : "half_float"
            },
            "location" : {
              "type" : "geo_point"
            },
            "longitude" : {
              "type" : "half_float"
            }
          }
        },
        "@version" : {
          "type" : "keyword"
        }
      }
    },
    "aliases" : { }
}

3. logstash 禁用自动管理模板

到 logstash 模块,编辑 conf/logstash.conf, 在 output --- elashticsearch 中加入两行,添加后内容如下:

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "%{app_name}"
    user => "elastic"
    password => "xxx"

    # 添加下面两行
    template_overwrite => false
    manage_template => false
  }
  stdout { codec => rubydebug }
}


上面两行的意思是不要 logstash 自动管理模板,因为我们在 es 中配置好了模板,不希望被覆盖。完成后,重启 logstash.

4. 重建索引

如果之前的索引不重要的话,可以在 kibana 中直接删除,这样如果有新的日志,会自动创建索引。如果不想删除,则要执行重建索引的操作。

假如索引名称为 app, 重建索引需要这样,先把 logstash 停掉,防止自动创建索引。再通过 app --> app-tmp -->app 还原。

POST /_reindex
{
  "source": {
    "index": "app"
  },
  "dest": {
    "index": "app-1"
  }
}

DELETE /app

POST /_reindex
{
  "source": {
    "index": "app-1"
  },
  "dest": {
    "index": "app"
  }
}

DELETE /app-1
原文地址:https://www.cnblogs.com/qkhh/p/14252911.html