Elasticsearch把cluster persistent update保存到哪里了?

本文深入解析了在Elasticsearch集群中如何持久化存储集群参数,包括参数保存的位置、文件格式以及重启时的状态恢复过程,详细解释了配置变更如何被存储和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

执行下面的设置:
PUT _cluster/settings
{
    "persistent" : {
        "indices.store.throttle.type" : "merge",
        "indices.store.throttle.max_bytes_per_sec" : "20mb"
    }
}

意思是持久更新集群参数,既然是持久化,那么它把这个变更保存到哪里了呢?

elasticsearch.yml里是没有这两个设置,但是重启后,这个变更它又确实是生效的:

[2016-01-19 18:22:49,500][INFO ][indices.store            ] [elasticsearch_179] updating indices.store.throttle.type from [MERGE] to [merge]
[2016-01-19 18:22:49,500][INFO ][indices.store            ] [elasticsearch_179] updating indices.store.throttle.max_bytes_per_sec from [20mb] to [20mb], note, type is [merge]

可以看到重启结点时,它会提示已更新。

原来它是保存在下面这个文件里:

{INSTALL_DIR}/data/{CLUSTER_NAME}/nodes/{N}/_state/global-{NNN} 

INSTALL_DIR 是elasticsearch的安装目录
CLUSTER_NAME 是集群名称
N 结点序号(0表示这台机器上只有一个结点)
NNN 是集群状态文件版本号

该文件默认是二进制格式,你可以在elasticsearch.yml增加如下:

format: json

ES就会把它保存为josn格式

让我们看一下这个文件都包含了什么:

{
  "meta-data": {
    "version": 7,
    "uuid": "MxNs_3pRRTa3eU4ZMbcI1A",
    "settings": {
      "indices.store.throttle.type": "merge",
      "indices.store.throttle.max_bytes_per_sec": "20mb"
    },
    "templates": {
      "logstash": {
        "order": 0,
        "template": "logstash-*",
        "settings": {
          "index.refresh_interval": "5s"
        },
        "mappings": [
          {
            "_default_": {
              "_all": {
                "enabled": true,
                "omit_norms": true
              },
              "dynamic_templates": [
                {
                  "message_field": {
                    "match": "message",
                    "match_mapping_type": "string",
                    "mapping": {
                      "type": "string",
                      "index": "analyzed",
                      "omit_norms": true
                    }
                  }
                },
                {
                  "string_fields": {
                    "match": "*",
                    "match_mapping_type": "string",
                    "mapping": {
                      "type": "string",
                      "index": "analyzed",
                      "omit_norms": true,
                      "fields": {
                        "raw": {
                          "type": "string",
                          "index": "not_analyzed",
                          "ignore_above": 256
                        }
                      }
                    }
                  }
                }
              ],
              "properties": {
                "@version": {
                  "type": "string",
                  "index": "not_analyzed"
                },
                "geoip": {
                  "type": "object",
                  "dynamic": true,
                  "properties": {
                    "location": {
                      "type": "geo_point"
                    }
                  }
                }
              }
            }
          }
        ],
        "aliases": {}
      }
    }
  }
}

确实看到了如下两个配置项:

    "settings": {
      "indices.store.throttle.type": "merge",
      "indices.store.throttle.max_bytes_per_sec": "20mb"
    },

另外,还有一个持久性的模板设置:

    "templates": {
      "logstash": {
        "order": 0,
        "template": "logstash-*",
        "settings": {
          "index.refresh_interval": "5s"
        },

这个就是logstash在ES中增加的模板配置,可以看到其中关于message字段的配置,其它字符串型字段都会默认增加一个子字段raw,用于保存原字段不经analyzed的原始值。PUT _template/logstash的模板也是保存在这个文件中的。

Every time cluster state changes, all master-eligible nodes store the new version of the file, so during cluster restart the node that starts first and elects itself as a master will have the newest version of the cluster state. What you are describing could be possible if you updated the settings when one of your master-eligible nodes was not part of the cluster (and therefore couldn’t store the latest version with your settings) and after the restart this node became the cluster master and propagated its obsolete settings to all other nodes.

### Elasticsearch 常用 API 请求示例 以下是几种常见的 Elasticsearch API 请求及其用途: #### 1. 创建索引 通过 `PUT` 方法可以创建一个新的索引,并定义其映射结构。以下是一个创建名为 `blog` 的索引并指定 `article` 类型的示例[^4]。 ```json PUT /blog { "mappings": { "properties": { "id": { "type": "long", "store": true, "index": true }, "title": { "type": "text", "store": true, "index": true, "analyzer": "standard" }, "content": { "type": "text", "store": true, "index": true, "analyzer": "standard" } } } } ``` --- #### 2. 插入文档 可以通过 `POST` 或 `PUT` 方法向已存在的索引中插入文档。以下是在 `blog` 索引下插入一条记录的示例。 ```json POST /blog/_doc/1 { "id": 1, "title": "Introduction to Elasticsearch", "content": "This is an introduction to using Elasticsearch." } ``` --- #### 3. 查询文档 使用 `_search` 接口执行查询操作,支持多种查询方式。下面展示了基于布尔查询返回状态码为 `200` 的前 10 条记录的例子[^2]。 ```json GET /_search { "size": 10, "query": { "bool": { "must": [ { "term": { "status": 200 } } ] } } } ``` --- #### 4. 更新文档 更新现有文档的部分字段可使用 `POST _update/{id}` 接口。例如,更新 ID 为 `1` 的文档标题。 ```json POST /blog/_update/1 { "doc": { "title": "Updated Title of the Document" } } ``` --- #### 5. 删除文档 删除特定文档可通过 `DELETE` 方法实现。例如,删除 ID 为 `1` 的文档。 ```json DELETE /blog/_doc/1 ``` --- #### 6. 使用 DSL 进行复杂查询 对于更复杂的场景,可以利用 Elasticsearch 提供的 Query DSL 执行高级查询。以下例子演示了如何查找 `content` 字段包含关键词 `elastic` 同时满足条件 `(title 包含 lucene 或 solr)` 的文档[^3]。 ```json GET /_search { "query": { "query_string": { "default_field": "content", "query": "elastic AND (title:lucene OR title:solr)" } } } ``` --- #### 7. 获取集群健康状况 监控 Elasticsearch 集群的状态可用如下命令获取整体健康信息。 ```json GET /_cluster/health ``` --- #### 8. 设置集群配置 动态调整集群级别的参数也可以通过 RESTful API 实现。比如修改分片分配策略。 ```json PUT /_cluster/settings { "persistent": { "cluster.routing.allocation.enable": "all" } } ``` --- ### 总结 上述列举了几种常用的 Elasticsearch API 请求形式,涵盖了从基础的数据增删改查到较为深入的集群管理功能。每一种接口都提供了灵活多样的选项来适应不同的业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值