ElasticSearch 实战:Elasticsearch的文档操作

Elasticsearch 中的文档操作主要包括文档的创建(索引)、读取(检索)、更新和删除(CRUD)。以下是一系列实战示例,演示如何通过 Elasticsearch 的 HTTP API 或 Kibana Dev Tools Console 进行这些操作:

一、文档创建(索引)

1. 使用 HTTP API 创建文档
curl -X POST "localhost:9200/my_index/_doc" -H 'Content-Type: application/json' -d'
{
  "title": "My First Blog Post",
  "content": "This is the content of my first blog post.",
  "author": "John Doe",
  "publish_date": "2024-04-03T12:00:00Z"
}'
2. 使用 Kibana Dev Tools Console 创建文档
POST my_index/_doc
{
  "title": "My First Blog Post",
  "content": "This is the content of my first blog post.",
  "author": "John Doe",
  "publish_date": "2024-04-03T12:00:00Z"
}

二、文档读取(检索)

1. 获取单个文档

通过文档ID查询:

curl -X GET "localhost:9200/my_index/_doc/1?pretty"

或在 Kibana Dev Tools Console:

GET my_index/_doc/1?pretty
2. 执行全文搜索

执行一个简单的全文搜索:

curl -X GET "localhost:9200/my_index/_search?q=title:blog"

或在 Kibana Dev Tools Console:

GET my_index/_search
{
  "query": {
    "match": {
      "title": "blog"
    }
  }
}

三、文档更新

1. 使用 HTTP API 更新文档

根据文档ID更新文档内容:

curl -X PUT "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "Updated Blog Post",
  "content": "This is the updated content of my blog post.",
  "author": "John Doe",
  "publish_date": "2024-04-03T12:00:00Z"
}'
2. 使用 Kibana Dev Tools Console 更新文档
PUT my_index/_doc/1
{
  "title": "Updated Blog Post",
  "content": "This is the updated content of my blog post.",
  "author": "John Doe",
  "publish_date": "2024-04-03T12:00:00Z"
}

四、文档删除

1. 使用 HTTP API 删除文档

删除指定ID的文档:

curl -X DELETE "localhost:9200/my_index/_doc/1"

或在 Kibana Dev Tools Console:

DELETE my_index/_doc/1

五、批量操作

1. 批量索引(创建或更新)文档
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/x-ndjson' --data-binary '
{ "index" : { "_index" : "my_index", "_id" : "1" } }
{ "title": "Blog Post 1", "content": "Content 1", "author": "Author 1" }
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "title": "Blog Post 2", "content": "Content 2", "author": "Author 2" }
'

或在 Kibana Dev Tools Console,确保启用 “Request Body” 标签并选择 “ND-JSON” 格式:

POST _bulk
{ "index" : { "_index" : "my_index", "_id" : "1" } }
{ "title": "Blog Post 1", "content": "Content 1", "author": "Author 1" }
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "title": "Blog Post 2", "content": "Content 2", "author": "Author 2" }
2. 批量删除文档
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/x-ndjson' --data-binary '
{ "delete" : { "_index" : "my_index", "_id" : "1" } }
{ "delete" : { "_index" : "my_index", "_id" : "2" } }
'

或在 Kibana Dev Tools Console:

POST _bulk
{ "delete" : { "_index" : "my_index", "_id" : "1" } }
{ "delete" : { "_index" : "my_index", "_id" : "2" } }

实战应用

  • 使用 POST 请求创建新文档,文档ID可由Elasticsearch自动生成或由用户指定。
  • 使用 GET 请求通过文档ID检索文档内容。
  • 使用 PUT 请求更新已存在文档,需提供完整的文档内容。
  • 使用 DELETE 请求删除指定ID的文档。
  • 利用批量操作API(_bulk)高效地进行大量文档的索引、更新或删除。

通过熟练掌握这些文档操作,您可以有效地管理Elasticsearch中的数据,满足各种增删改查的需求。在实际使用中,请始终参考Elasticsearch的官方文档以获取最新的API规范和最佳实践。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值