1. 首页 > ElasticSearch教程 > 正文

ElasticSearch教程FG009-ElasticSearch文档增删改查操作实战

Part01-基础概念与理论知识

1.1 文档概念

文档是ElasticSearch中的基本数据单元,类似于关系型数据库中的行。文档以JSON格式存储,包含多个字段和值。更多视频教程www.fgedu.net.cn

文档的特点:

  • JSON格式
  • 具有唯一ID
  • 可以包含嵌套结构
  • 支持版本控制

1.2 文档ID

文档ID是文档的唯一标识符,可以由用户指定或由ElasticSearch自动生成。

  • 用户指定:使用PUT请求并在URL中指定ID
  • 自动生成:使用POST请求,ElasticSearch会生成一个UUID

文档版本控制

ElasticSearch使用版本号来控制文档的并发修改,确保数据一致性。

  • 每次修改文档,版本号递增
  • 可以使用version参数指定版本号进行乐观并发控制
  • 支持external版本控制模式

学习交流加群风哥微信: itpux-com

Part02-生产环境规划与建议

2.1 文档设计原则

文档设计应遵循以下原则:

  • 扁平化结构:避免过深的嵌套
  • 合理字段类型:选择合适的数据类型
  • 按需存储:只存储必要的字段
  • 考虑查询模式:优化字段结构以提高查询性能

2.2 批量操作策略

批量操作可以提高性能,减少网络开销:

  • 使用_bulk API进行批量操作
  • 合理设置批量大小(通常1000-5000条)
  • 监控批量操作的性能
  • 处理批量操作中的错误

2.3 生产环境最佳实践

生产环境中,文档操作应注意:

  • 使用批量操作减少网络往返
  • 合理设置refresh_interval以平衡实时性和性能
  • 使用乐观并发控制避免冲突
  • 监控文档操作的性能

学习交流加群风哥QQ113257174

Part03-生产环境项目实施方案

3.1 文档创建实战

创建文档的两种方式:

# 方式1:指定ID创建文档
curl -X PUT “http://192.168.1.10:9200/fgedu-products/_doc/1” -H “Content-Type: application/json” -d ‘{
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 89.9,
“category”: “技术书籍”,
“stock”: 100,
“created_at”: “2024-01-01 10:00:00”
}’

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 0,
“_primary_term”: 1
}

# 方式2:自动生成ID创建文档
curl -X POST “http://192.168.1.10:9200/fgedu-products/_doc” -H “Content-Type: application/json” -d ‘{
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}’

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “_bQH044BMQz8e7k9eJ6-“,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 1,
“_primary_term”: 1
}

3.2 文档读取实战

读取文档的操作:

# 根据ID读取文档
curl -X GET “http://192.168.1.10:9200/fgedu-products/_doc/1”

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 1,
“_seq_no”: 0,
“_primary_term”: 1,
“found”: true,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 89.9,
“category”: “技术书籍”,
“stock”: 100,
“created_at”: “2024-01-01 10:00:00”
}
}

# 读取多个文档
curl -X GET “http://192.168.1.10:9200/fgedu-products/_mget” -H “Content-Type: application/json” -d ‘{
“ids”: [“1”, “_bQH044BMQz8e7k9eJ6-“]
}’

# 执行
# 输出日志
{
“docs”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 1,
“_seq_no”: 0,
“_primary_term”: 1,
“found”: true,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 89.9,
“category”: “技术书籍”,
“stock”: 100,
“created_at”: “2024-01-01 10:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “_bQH044BMQz8e7k9eJ6-“,
“_version”: 1,
“_seq_no”: 1,
“_primary_term”: 1,
“found”: true,
“_source”: {
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}
}
]
}

3.3 文档更新实战

更新文档的操作:

# 全量更新文档
curl -X PUT “http://192.168.1.10:9200/fgedu-products/_doc/1” -H “Content-Type: application/json” -d ‘{
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南(第二版)”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 90,
“created_at”: “2024-01-01 10:00:00”
}’

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 2,
“result”: “updated”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 2,
“_primary_term”: 1
}

# 部分更新文档
curl -X POST “http://192.168.1.10:9200/fgedu-products/_update/1” -H “Content-Type: application/json” -d ‘{
“doc”: {
“price”: 109.9,
“stock”: 85
}
}’

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 3,
“result”: “updated”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 3,
“_primary_term”: 1
}

3.4 文档删除实战

删除文档的操作:

# 删除文档
curl -X DELETE “http://192.168.1.10:9200/fgedu-products/_doc/1”

# 执行
# 输出日志
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 4,
“result”: “deleted”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 4,
“_primary_term”: 1
}

# 检查文档是否存在
curl -X HEAD “http://192.168.1.10:9200/fgedu-products/_doc/1”

# 执行
# 输出日志
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 0

更多学习教程公众号风哥教程itpux_com

Part04-生产案例与实战讲解

4.1 电商商品文档管理

电商商品文档的完整管理流程:

# 创建商品索引
curl -X PUT “http://192.168.1.10:9200/fgedu-products” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 5,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“product_id”: {
“type”: “keyword”
},
“product_name”: {
“type”: “text”,
“analyzer”: “ik_max_word”
},
“price”: {
“type”: “double”
},
“category”: {
“type”: “keyword”
},
“stock”: {
“type”: “integer”
},
“created_at”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss”
}
}
}
}’

# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-products”
}

# 批量创建商品文档
curl -X POST “http://192.168.1.10:9200/fgedu-products/_bulk” -H “Content-Type: application/json” -d ‘{
“index”: {“_id”: “1”}
}
{
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 89.9,
“category”: “技术书籍”,
“stock”: 100,
“created_at”: “2024-01-01 10:00:00”
}
{
“index”: {“_id”: “2”}
}
{
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}
{
“index”: {“_id”: “3”}
}
{
“product_id”: “P003”,
“product_name”: “Logstash入门与实践”,
“price”: 69.9,
“category”: “技术书籍”,
“stock”: 60,
“created_at”: “2024-01-01 11:00:00”
}

# 执行
# 输出日志
{
“took”: 123,
“errors”: false,
“items”: [
{
“index”: {
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 0,
“_primary_term”: 1
}
},
{
“index”: {
“_index”: “fgedu-products”,
“_id”: “2”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 1,
“_primary_term”: 1
}
},
{
“index”: {
“_index”: “fgedu-products”,
“_id”: “3”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 2,
“_primary_term”: 1
}
}
]
}

4.2 日志文档管理

日志文档的管理:

# 创建日志索引
curl -X PUT “http://192.168.1.10:9200/fgedu-logs-2024.01” -H “Content-Type: application/json” -d ‘{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1
},
“mappings”: {
“properties”: {
“timestamp”: {
“type”: “date”,
“format”: “yyyy-MM-dd HH:mm:ss.SSS”
},
“level”: {
“type”: “keyword”
},
“message”: {
“type”: “text”
},
“service”: {
“type”: “keyword”
}
}
}
}’

# 执行
# 输出日志
{
“acknowledged”: true,
“shards_acknowledged”: true,
“index”: “fgedu-logs-2024.01”
}

# 插入日志文档
curl -X POST “http://192.168.1.10:9200/fgedu-logs-2024.01/_doc” -H “Content-Type: application/json” -d ‘{
“timestamp”: “2024-01-01 10:00:00.123”,
“level”: “INFO”,
“message”: “Application started successfully”,
“service”: “fgedu-api”
}’

# 执行
# 输出日志
{
“_index”: “fgedu-logs-2024.01”,
“_id”: “_cQH044BMQz8e7k9eJ6-“,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 0,
“_primary_term”: 1
}

4.3 批量操作实战

批量操作的高级用法:

# 批量混合操作(创建、更新、删除)
curl -X POST “http://192.168.1.10:9200/fgedu-products/_bulk” -H “Content-Type: application/json” -d ‘{
“index”: {“_id”: “4”}
}
{
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”
}
{
“update”: {“_id”: “1”}
}
{
“doc”: {
“price”: 99.9
}
}
{
“delete”: {“_id”: “3”}
}

# 执行
# 输出日志
{
“took”: 156,
“errors”: false,
“items”: [
{
“index”: {
“_index”: “fgedu-products”,
“_id”: “4”,
“_version”: 1,
“result”: “created”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 3,
“_primary_term”: 1
}
},
{
“update”: {
“_index”: “fgedu-products”,
“_id”: “1”,
“_version”: 2,
“result”: “updated”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 4,
“_primary_term”: 1
}
},
{
“delete”: {
“_index”: “fgedu-products”,
“_id”: “3”,
“_version”: 2,
“result”: “deleted”,
“_shards”: {
“total”: 2,
“successful”: 1,
“failed”: 0
},
“_seq_no”: 5,
“_primary_term”: 1
}
}
]
}

风哥提示:批量操作时,建议将操作数控制在合理范围内,以避免内存溢出

Part05-风哥经验总结与分享

5.1 文档操作最佳实践

  • 使用批量操作提高性能
  • 合理设计文档结构
  • 使用版本控制避免冲突
  • 设置合理的refresh_interval
  • 监控文档操作的性能指标

5.2 常见问题与解决方案

  • 文档冲突:使用乐观并发控制
  • 性能问题:优化批量操作大小
  • 内存溢出:控制批量操作规模
  • 索引膨胀:定期清理过期数据
  • 查询性能:合理设计文档结构

5.3 性能优化建议

  • 使用批量API减少网络开销
  • 合理设置索引的refresh_interval
  • 使用异步操作提高吞吐量
  • 监控和调优JVM内存
  • 使用合适的硬件资源

from ElasticSearch视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息