--- title: "更新索引设置" date: 2026-01-22 lastmod: 2026-01-22 description: "更新或获取索引的设置配置" tags: ["索引管理", "索引设置"] summary: "更新或获取索引的设置配置。 API # GET /{index}/_settings GET /_all/_settings PUT /{index}/_settings PUT /_settings API 的作用 # GET /{index}/_settings # 获取一个或多个索引的当前设置,包括: 索引的分片和副本配置 索引的刷新间隔 分析器配置 其他索引级别的设置 PUT /{index}/_settings # 更新一个或多个索引的设置。可以动态修改的设置包括: 索引的刷新间隔 副本数量 分析器配置 搜索相关的设置 注意:某些设置(如分片数量)只能在创建索引时设置,不能动态修改。 API 的参数 # 路由参数 # 参数 类型 是否必填 描述 {index} 字符串 否 索引名称。支持:单个索引、逗号分隔的多个索引、通配符表达式、_all(所有索引) Query String 参数 # GET 请求参数 # 参数 类型 是否必填 默认值 描述 include_defaults 布尔值 否 false 是否包含默认值 flat_settings 布尔值 否 false 是否使用扁平化设置格式 local 布尔值 否 false 是否只返回本地节点信息,不联系主节点 master_timeout 时间值 否 30s 等待主节点操作的超时时间 human 布尔值 否 false 是否使用人类可读的格式 name 字符串 否 所有设置 要获取的特定设置名称(支持通配符) PUT 请求参数 # 参数 类型 是否必填 默认值 描述 timeout 时间值 否 默认超时 等待操作完成超时时间 master_timeout 时间值 否 30s 等待主节点操作的超时时间 preserve_existing 布尔值 否 false 是否保留现有设置(不覆盖) flat_settings 布尔值 否 false 是否使用扁平化设置格式 请求体参数(PUT 请求) # 请求体包含要更新的设置,格式为 JSON:" --- 更新或获取索引的设置配置。 ## API ``` GET /{index}/_settings GET /_all/_settings PUT /{index}/_settings PUT /_settings ``` ## API 的作用 ### GET /{index}/_settings 获取一个或多个索引的当前设置,包括: - 索引的分片和副本配置 - 索引的刷新间隔 - 分析器配置 - 其他索引级别的设置 ### PUT /{index}/_settings 更新一个或多个索引的设置。可以动态修改的设置包括: - 索引的刷新间隔 - 副本数量 - 分析器配置 - 搜索相关的设置 > **注意**:某些设置(如分片数量)只能在创建索引时设置,不能动态修改。 ## API 的参数 ### 路由参数 | 参数 | 类型 | 是否必填 | 描述 | |------|------|----------|------| | `{index}` | 字符串 | 否 | 索引名称。支持:单个索引、逗号分隔的多个索引、通配符表达式、`_all`(所有索引) | ### Query String 参数 #### GET 请求参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `include_defaults` | 布尔值 | 否 | false | 是否包含默认值 | | `flat_settings` | 布尔值 | 否 | false | 是否使用扁平化设置格式 | | `local` | 布尔值 | 否 | false | 是否只返回本地节点信息,不联系主节点 | | `master_timeout` | 时间值 | 否 | 30s | 等待主节点操作的超时时间 | | `human` | 布尔值 | 否 | false | 是否使用人类可读的格式 | | `name` | 字符串 | 否 | 所有设置 | 要获取的特定设置名称(支持通配符) | #### PUT 请求参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `timeout` | 时间值 | 否 | 默认超时 | 等待操作完成超时时间 | | `master_timeout` | 时间值 | 否 | 30s | 等待主节点操作的超时时间 | | `preserve_existing` | 布尔值 | 否 | false | 是否保留现有设置(不覆盖) | | `flat_settings` | 布尔值 | 否 | false | 是否使用扁平化设置格式 | ### 请求体参数(PUT 请求) 请求体包含要更新的设置,格式为 JSON: ```json { "index.number_of_replicas": 2, "index.refresh_interval": "5s" } ``` 或嵌套格式: ```json { "index": { "number_of_replicas": 2, "refresh_interval": "5s" } } ``` ## 示例 ### 获取索引设置 ```bash GET /my_index/_settings ``` **响应示例:** ```json { "my_index": { "settings": { "index": { "number_of_shards": "1", "number_of_replicas": "1", "refresh_interval": "1s", "creation_date": "1641234567890" } } } } ``` ### 获取多个索引的设置 ```bash GET /index1,index2/_settings ``` ### 获取特定设置 ```bash GET /my_index/_settings?index.number_of_replicas ``` ### 包含默认值 ```bash GET /my_index/_settings?include_defaults=true ``` ### 使用扁平化格式 ```bash GET /my_index/_settings?flat_settings=true ``` **响应示例:** ```json { "my_index": { "index.number_of_shards": "1", "index.number_of_replicas": "1", "index.refresh_interval": "1s" } } ``` ### 更新副本数量 ```bash PUT /my_index/_settings { "index.number_of_replicas": 2 } ``` ### 更新刷新间隔 ```bash PUT /my_index/_settings { "index.refresh_interval": "30s" } ``` ### 禁用刷新 ```bash PUT /my_index/_settings { "index.refresh_interval": "-1" } ``` ### 保留现有设置 ```bash PUT /my_index/_settings?preserve_existing=true { "index.refresh_interval": "5s" } ``` ### 更新多个索引的设置 ```bash PUT /index1,index2/_settings { "index.number_of_replicas": 1 } ``` ### 更新所有索引的设置 ```bash PUT /_all/_settings { "index.query.default_field": ["title", "description"] } ``` ### 更新分析器配置 ```bash PUT /my_index/_settings { "index.analysis.analyzer.my_analyzer.type": "standard" } ``` ## 可动态修改的设置 以下是一些常见的可以动态修改的设置: | 设置 | 描述 | |------|------| | `index.number_of_replicas` | 副本数量 | | `index.refresh_interval` | 刷新间隔 | | `index.max_result_window` | 最大结果窗口大小 | | `index.query.default_field` | 默认搜索字段 | | `index.analysis.*` | 分析器配置 | ## 不可动态修改的设置 以下设置只能在创建索引时指定: | 设置 | 描述 | |------|------| | `index.number_of_shards` | 主分片数量 | | `index.indexing.slowlog.threshold.index.warn` | 慢日志阈值(部分需要重启) | ## 注意事项 1. **不可逆操作**:某些设置修改后可能无法恢复 2. **副本数量**:增加副本数量会增加集群负载 3. **刷新间隔**:设置为 -1 会完全禁用刷新 4. **性能影响**:某些设置变更可能导致索引重新打开,影响性能 5. **集群状态**:大量设置变更会增加集群状态大小