ElasticSearch 实战:索引的操作

Elasticsearch 中的索引操作是日常使用中非常关键的部分,涵盖了创建、查看、更新、删除索引以及对索引映射、设置和数据的操作。以下是索引操作的一些实战示例:

一、创建索引

1. 使用 HTTP API 创建索引

通过发送一个 PUT 请求到 Elasticsearch 服务器的相应索引名来创建索引。同时,可以在请求体中定义索引的映射(mapping)和其他设置。例如,使用 curl 或 Postman 等工具:

curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "author": { "type": "keyword" },
      "publish_date": { "type": "date" }
    }
  }
}'

这里创建了一个名为 my_index 的索引,设置了3个主分片和2个副本分片。映射中定义了四个字段:title(文本类型)、content(文本类型)、author(关键字类型)和 publish_date(日期类型)。

2. 使用 Kibana Dev Tools Console 创建索引

如果已经安装了 Kibana,可以利用其内置的 Dev Tools Console 来创建索引:

PUT my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "content": { "type": "text" },
      "author": { "type": "keyword" },
      "publish_date": { "type": "date" }
    }
  }
}

复制上述 JSON 请求体,粘贴到 Dev Tools Console 中,然后点击 Execute 或按 Ctrl + Enter 执行。

二、查看索引

1. 列出所有索引

使用以下 HTTP 请求获取集群中所有索引的列表:

curl -X GET "localhost:9200/_cat/indices?v"

或者在 Kibana Dev Tools Console 中:

GET _cat/indices?v

这将返回一个表格,包含每个索引的名称、状态、分片数量、文档数量等信息。

2. 查看单个索引详情

查询特定索引的详细信息,包括其映射和设置:

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

或在 Kibana Dev Tools Console:

GET my_index?pretty

三、更新索引

1. 更新索引设置

修改 my_indexnumber_of_replicas

curl -X PUT "localhost:9200/my_index/_settings" -H 'Content-Type: application/json' -d'
{
  "number_of_replicas": 1
}'

或在 Kibana Dev Tools Console:

PUT my_index/_settings
{
  "number_of_replicas": 1
}
2. 更新映射

若需添加或修改字段映射,需使用 PUT 请求重新提交整个映射定义。注意,映射更新可能导致需要重新索引已有数据以应用新映射。

四、删除索引

删除名为 my_index 的索引:

curl -X DELETE "localhost:9200/my_index"

或在 Kibana Dev Tools Console:

DELETE my_index

五、索引数据操作

1. 添加文档

my_index 中添加一个文档:

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"
}'

或在 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"
}
2. 查询文档

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

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

或在 Kibana Dev Tools Console:

GET my_index/_search
{
  "query": {
    "match": {
      "title": "blog"
    }
  }
}
3. 更新文档

根据文档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"
}'

或在 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"
}
4. 删除文档

删除指定ID的文档:

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

或在 Kibana Dev Tools Console:

DELETE my_index/_doc/1

以上就是Elasticsearch索引操作的一些实战示例。在实际使用中,您可能还会涉及到索引模板、别名、重建索引、分页查询、聚合分析等多种更复杂的功能。这些操作同样可以通过Elasticsearch的HTTP API或Kibana Dev Tools Console进行。务必参照官方文档了解详细语法和最佳实践。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值