01 Elasticsearch x-pack 监控工具

x-pack 是 Elastic Stack 扩展的功能,提供安全性,警报,监视,报告,机器学习和许多其他功能。 ES7.0+之后,默认情况下,当安装Elasticsearch时,会安装X-Pack,无需单独再安装。

Elasticsearch x-pack

02 ElasticSearch Search Guard 安全认证工具

在 x-pack 免费安全认证功能之后,这个工具就显得比较鸡肋

ElasticSearch Search Guard

03 ElasticSearch 系统优化配置

配置内容一般包括如下内容,但是在新版本中,ElasticSearch 系统内部已经完成了优化,不需要额外的配置:

  • JVM最大最小内容调整
  • 关闭Swap分区
  • vm.max_map_count调整
  • ulimit调整
  • 磁盘性能

ElasticSearch系统优化官方文档

04 ElasticSearch dump 数据备份工具

ElasticSearch dump工具主要是用来对ES中的数据进行数据导入/导出,以及对数据迁移相关,使用elasticdump工具需要使用到npm,所以需要安装相关的依赖,目前使用到的ES版本是7.x

ElasticSearch dump

ElasticSearch dump安装与使用

05 ElasticSearch python 操作

Elasticsearch 的官方低级客户端,它的目标是为 Python 中所有与 Elasticsearch 相关的代码提供共同点;正因为如此,它试图做到无意见且非常可扩展。

ElasticSearch python第三方库下载地址

ElasticSearch python第三方库官方文档

06 ElasticSearch 防止分裂故障配置

前面介绍过,如果集群的物理主机分布在不同机房中需要通过远程网络连接进行数据交互。这种情况下,如果出现网络中断,可能导致不同机房中节点的集群独立,导致原始集群被分裂。

配置如下参数,防止这种分裂情况,值得注意的是新版本的Elasticsearch选项命名发生了变化,需要根据官方文档进行配置:

discovery.zen.minimum_master_nodes: 2
discovery.zen.fd.ping_interval: 10s
discovery.zen.fd.ping_timeout: 60s
discovery.zen.fd.ping_retries: 6

ElasticSearch discovery配置官方文档

07 ElasticSearch 中文分词器

该项目旨在解决ElasticSearch对中文支持不友好的问题

中文查询示例:首先给集群中插入中文数据,然后进行关键词查找。

# 创建索引
curl -XPUT '172.16.255.131:9200/news?pretty' -H 'Content-Type:application/json' -d'
{
    "settings": { 
        "number_of_shards": 3, 
        "number_of_replicas": 1
    }
}'

# 插入数据
curl -XPOST '172.16.255.131:9200/news/fulltext/1?pretty' -H 'Content-Type:application/json' -d'{"content": "美国是美洲国家"}'
curl -XPOST '172.16.255.131:9200/news/fulltext/2?pretty' -H 'Content-Type:application/json' -d'{"content": "中国是亚洲国家"}'
curl -XPOST '172.16.255.131:9200/news/fulltext/3?pretty' -H 'Content-Type:application/json' -d'{"content": "中国是东亚国家"}'

# 关键词查询数据
curl -XPOST '172.16.255.131:9200/news/fulltext/_search?pretty' -H 'Content-Type:application/json' -d'{
    "query": {"match": {"content": "中国"}},
    "highlight":{
        "pre_tags": ["<tag1>", "<tag2>"],
        "post_tags": ["</tag1>", "</tag2>"],
        "fields": {
            "content": {}
        }
    }
}'

查询结果如下图所示,可以看出它将中和国两个字分开查询而非将中国当作一个词来查询。

es运维问题 es运维工具_运维

为了解决这一缺陷,使用第三方工具 Elasticsearch 中文分词器,Elasticsearch 中文分词器。需要注意的是,使用这一工具时要为集群中所有节点都装上该中文分词器工具,所以一般需要在物理机上装ES服务时,同时安装,安装完成之后将集群中的所有节点都重新启动。

在使用中文分词器时,要为查询指定分词器

# 1. 创建索引
curl -XPUT http://localhost:9200/index

# 2. 创建映射
curl -XPOST 'http://localhost:9200/index/_mapping' -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }
}'

# 3. 插入数据
curl -XPOST '172.16.255.131:9200/index/fulltext/1?pretty' -H 'Content-Type:application/json' -d'{"content": "美国是美洲国家"}'
curl -XPOST '172.16.255.131:9200/index/fulltext/2?pretty' -H 'Content-Type:application/json' -d'{"content": "中国是亚洲国家"}'
curl -XPOST '172.16.255.131:9200/index/fulltext/3?pretty' -H 'Content-Type:application/json' -d'{"content": "中国是东亚国家"}'

# 4. 查询语句
curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}'