ElasticSearch ——(二)基本操作 Getting Started

本文介绍Elasticsearch提供的强大REST API,包括如何使用API进行集群、节点及索引管理,执行CRUD操作及高级搜索等功能。还介绍了如何通过API检查集群健康状况、管理数据和元数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加粗样式

The REST API

Elasticsearch provides a very comprehensive and powerful REST API that you can use to interact with your cluster. Among the few things that can be done with the API are as follows:

Check your cluster, node, and index health, status, and statistics
Administer your cluster, node, and index data and metadata
Perform CRUD (Create, Read, Update, and Delete) and search operations against your indexes
Execute advanced search operations such as paging, sorting, filtering, scripting, aggregations, and many others

ES提供RestAPI,可以让用户与之互动。通过API可以做下列的事情:

  • 检查集群、节点、索引的健康、状态、数字统计
  • 管理集群、节点、索引的数据和元数据
  • CRUD操作
  • 执行高级查询,如分页、过滤、聚合等

集群的健康

检查集群的健康,使用下列命令

GET /_cat/health?v

返回结果

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

可以看到cluster下面的值为elasticsearch,这是集群的名称。
还能看出有一个节点,0个分片,

集群健康的几种状态:

  • Green - everything is good (cluster is fully functional)
  • Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional) 所有的数据都可用,但是分片没有被分配出去
  • Red - some data is not available for whatever reason (cluster is partially functional) 因为某种原因,一些数据不可用。如果出现这种情况你需要ASAP尽可能修复。

查看节点信息

GET /_cat/nodes?v

返回结果

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           10           5   5    4.46                        mdi      *      PB2SGZY

索引操作

创建索引

其中customer是索引名。

PUT /customer?pretty
列出索引
GET /_cat/indices?v

返回结果

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer 95SQ4TSUT7mWBT7VNHH67A   5   1          0            0       260b           260b

从中可以看到,健康状态为黄色,为什么出现黄色呢?原因是一个index的副本,不能跟该index在同一个节点上。如果index和该index对应的副本分别在不同的节点,则集群健康状态为Green。

索引和查询一个文档

添加文档

添加文档到索引中。

PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}

返回

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

It is important to note that Elasticsearch does not require you to explicitly create an index first before you can index documents into it. In the previous example, Elasticsearch will automatically create the customer index if it didn’t already exist beforehand.

不需要在添加文档之前先手动创建索引。事实上,添加文档时,如果索引不存在,ES就会帮你加了。

查询文档
GET /customer/_doc/1?pretty

返回

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 25,
  "_primary_term" : 1,
  "found" : true,
  "_source" : { "name": "John Doe" }
}

删除索引

DELETE /customer?pretty

ES操作数据的模式

<HTTP Verb> /<Index>/<Type>/<ID>

修改数据

Elasticsearch provides data manipulation and search capabilities in near real time. By default, you can expect a one second delay (refresh interval) from the time you index/update/delete your data until the time that it appears in your search results. This is an important distinction from other platforms like SQL wherein data is immediately available after a transaction is completed.

ES提供数据操作,和近实时的查询能力,能在低延时下返回查询结果。这是不同于SQL的地方。因为SQL需要等一个事务完成后才会响应。

索引和替换文档

增加文档

PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}

增加文档

PUT /customer/_doc/1?pretty
{
  "name": "Jane Doe"
}

如果ID相同,则后出现的会覆盖前面的文档,version数会增加。

{
  "_index" : "customer1",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 12,
  "_primary_term" : 1
}

更新文档

POST /customer/_doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}
POST /customer/_doc/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}

返回结果

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Jane Doe",
    "age" : 25
  }
}

像SQL一样更新数据,见Update By Query API

删除文档

DELETE /customer/_doc/2?pretty

更多的操作见Delete By Query API

批量处理

POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

Bulk API

参考文档

ES官网-快速开始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值