下载
1、下载ES
跟公司的保持一致,所以,我只能下载7.14.0
下载地址:
https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-14-0
2、下载Kibana
下载地址:
https://www.elastic.co/cn/downloads/past-releases/kibana-7-14-0
解压和运行
运行ES
解压:C:\MyProgramFiles\es\elasticsearch-7.14.0\bin
运行:elasticsearch.bat
访问地址:
http://localhost:9200/
用CURL检查一下效果
curl -X GET "http://localhost:9200/dev_index"
ES生效了,不过还没有创建索引
运行kibana
C:\MyProgramFiles\es\kibana-7.14.0\bin
kibana.bat
访问:
http://localhost:5601/
http://localhost:5601/app/dev_tools#/console
创建索引:
PUT zx_study_index
{
"settings": {
"number_of_shards": 3, // 设置主分片数量为3
"number_of_replicas": 1 // 设置每个主分片的副本数量为1
},
"mappings": {
"properties": {
"title": {
"type": "text" // 定义title字段为文本类型
},
"price": {
"type": "float" // 定义price字段为浮点数类型
},
"published_date": {
"type": "date" // 定义published_date字段为日期类型
}
}
}
}
查看索引映射信息
GET zx_study_index/_mapping
索引mapping
{
"zx_study_index" : {
"mappings" : {
"properties" : {
"price" : {
"type" : "float"
},
"published_date" : {
"type" : "date"
},
"title" : {
"type" : "text"
}
}
}
}
}
写入值:
POST /zx_study_index/_doc
{
"title": "Kibana可视化操作指南",
"price": 45.5,
"published_date": "2024-03-20 14:30:00" # 日期支持带时分秒格式
}
报错
{
"error" : {
"root_cause" : [
{
"type" : "cluster_block_exception",
"reason" : "index [zx_study_index] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block];"
}
],
"type" : "cluster_block_exception",
"reason" : "index [zx_study_index] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block];"
},
"status" : 429
}
因为磁盘空间不足
这个错误提示表明 Elasticsearch 因为磁盘空间不足,自动将索引 zx_study_index 设置为了 只读模式(read-only-allow-delete),导致无法写入新数据。具体解决步骤如下:
1. 首先检查磁盘空间
Elasticsearch 有磁盘水位线保护机制,当磁盘 使用率超过 95%(默认的 flood-stage 水位线)时,会自动阻止写入操作以保护集群。
- 清理磁盘空间,删除不必要的文件,确保磁盘使用率降至 95% 以下。
- 若在本地开发环境(如虚拟机、Docker),可扩展磁盘容量。
2. 解除索引的只读限制
即使清理了磁盘,索引的只读状态不会自动解除,需要手动执行命令:
在 Kibana Dev Tools 中运行:
bash
# 解除单个索引的只读限制
PUT /zx_study_index/_settings
{
"index.blocks.read_only_allow_delete": null
}
# 如果多个索引受影响,可批量解除(谨慎使用)
PUT /_all/_settings
{
"index.blocks.read_only_allow_delete": null
}
3. (可选)调整磁盘水位线阈值
如果需要临时放宽限制(不建议生产环境),可修改 Elasticsearch 配置文件 elasticsearch.yml,添加:
yaml
# 调整洪水水位线为 98%(默认95%)
cluster.routing.allocation.disk.watermark.flood_stage: 98%
# 可选:调整低水位线和高水位线
cluster.routing.allocation.disk.watermark.low: 85% # 默认85%,低于此值允许分配分片
cluster.routing.allocation.disk.watermark.high: 90% # 默认90%,高于此值尝试迁移分片
修改后需重启 Elasticsearch 生效。
4. 验证是否恢复正常
重新执行写入操作,或查询索引设置确认限制已解除:
bash
# 检查索引设置
GET /zx_study_index/_settings
若返回结果中没有
index.blocks.read_only_allow_delete 字段,说明限制已解除,此时可以正常写入数据了。
GET /zx_study_index/_settings
返回效果:
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security.
{
"zx_study_index" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "3",
"blocks" : {
"read_only_allow_delete" : "true"
},
"provided_name" : "zx_study_index",
"creation_date" : "1756602820719",
"number_of_replicas" : "1",
"uuid" : "AAeZ0G4pTGqsiOj3AAfyXQ",
"version" : {
"created" : "7140099"
}
}
}
}
}
我还没清理磁盘
电脑空间小
清理空间和重启电脑后
重新写入:
POST /zx_study_index/_doc
{
"title": "Kibana可视化操作指南",
"price": 45.5,
"published_date": "2024-03-20 14:30:00" # 日期支持带时分秒格式
}
报错
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [published_date] of type [date] in document with id 'qT3r_ZgB3orvm1-cRBZ0'. Preview of field's value: '2024-03-20 14:30:00'"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [published_date] of type [date] in document with id 'qT3r_ZgB3orvm1-cRBZ0'. Preview of field's value: '2024-03-20 14:30:00'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "failed to parse date field [2024-03-20 14:30:00] with format [strict_date_optional_time||epoch_millis]",
"caused_by" : {
"type" : "date_time_parse_exception",
"reason" : "Failed to parse with all enclosed parsers"
}
}
},
"status" : 400
}
用T代替空格就成功了
POST /zx_study_index/_doc
{
"title": "Kibana可视化操作指南",
"price": 41.5,
"published_date": "2024-03-20T14:30:00"
}
输出结果
{
"_index" : "zx_study_index",
"_type" : "_doc",
"_id" : "qz3v_ZgB3orvm1-cShYw",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 2
}
看了AI指导,索引没有建好的缘故
如果索引这样建,就可以约定时间格式
// 先创建索引并设置映射
PUT /zx_study_index
{
"mappings": {
"properties": {
"title": {"type": "text"},
"price": {"type": "float"},
"published_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss" // 明确指定支持的格式
}
}
}
}
// 再插入数据
POST /zx_study_index/_doc
{
"title": "Kibana可视化操作指南",
"price": 41.5,
"published_date": "2024-03-20 14:30:00" // 现在这个格式会被正确解析
}
获得记录
GET /zx_study_index/_doc/qz3v_ZgB3orvm1-cShYw
{
"_index" : "zx_study_index",
"_type" : "_doc",
"_id" : "qz3v_ZgB3orvm1-cShYw",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 2,
"found" : true,
"_source" : {
"title" : "Kibana可视化操作指南",
"price" : 41.5,
"published_date" : "2024-03-20T14:30:00"
}
}
更新记录
全量替换文档(覆盖更新)
PUT /zx_study_index/_doc/qz3v_ZgB3orvm1-cShYw
{
"title": "更新后的标题",
"price": 55.9,
"published_date": "2024-03-20T17:30:00"
}
部分字段更新(推荐)
POST /zx_study_index/_update/qz3v_ZgB3orvm1-cShYw
{
"doc": {
"price": 49.9
}
}
基于脚本的更新
POST /zx_study_index/_update/qz3v_ZgB3orvm1-cShYw
{
"script": {
"source": "ctx._source.price += params.increment",
"params": {
"increment": 5 // 价格增加5
}
}
}
删除指定的标签记录
DELETE /zx_study_index/_doc/qz3v_ZgB3orvm1-cShYw