ES-2 基本操作

上一节最后讲到,我们可以通过一些丰富的Rest API来对elasticsearch进行操作。
这些RestAPI的格式如下:

<HTTP Verb> /<Index>/<Endpoint>/<ID>

本节就进行一些演示,主要有如下一些操作:

一、查看健康状况

我们可以通过restapi来查看整个集群中节点的健康状况:

(一) 显示整个集群整体健康状态

curl -X GET “localhost:9200/_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
1558560046 21:20:46  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

这里的status显示的是"green",实际上,可能出现如下三个参数:

  • green:一切都很好(集群功能齐全)
  • yellow:所有数据都可用,但尚未分配一些副本(群集功能齐全)
  • red:某些数据由于某种原因不可用(群集部分功能)

注意:当群集为红色时,它将继续提供来自可用分片的搜索请求,但您可能需要尽快修复它,因为存在未分配的分片。

(二)显示各个节点的检查状况列表

curl -X GET “localhost:9200/_cat/nodes?v”
效果如下:

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           13          96   1    0.02    0.04     0.05 mdi       *      www.disconf.com

可以看到,这里显示了我们各个节点的名字,如这里是默认的 www.disconf.com

二、显示所有的索引

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

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

刚安装的时候,没有任何索引。

三、创建索引

  1. 创建一个索引customer
    curl -X PUT “localhost:9200/customer?pretty”
  2. 再次查看所有索引
    curl -X GET “localhost:9200/_cat/indices?v”
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer BjeAF7BbRYaMQAK7VX_yqA   1   1          0            0       230b           230b

会发现,有一个索引,名字叫做customer,这个索引有1个分片,1个副本,但是没有任何文档。
另外,索引标记了黄色运行状况。回想一下我们之前的讨论,黄色表示某些副本尚未(尚未)分配。此索引发生这种情况的原因是因为默认情况下Elasticsearch为此索引创建了一个副本。由于我们目前只有一个节点在运行,因此在另一个节点加入集群的稍后时间点之前,尚无法分配一个副本(用于高可用性)。将该副本分配到第二个节点后,此索引的运行状况将变为绿色。

四、文档操作

(一)给索引添加文档

curl -X PUT “localhost:9200/customer/_doc/1?pretty” -H ‘Content-Type: application/json’ -d’
{
“name”: “John Doe”
}

显示结果如下:

[2019-05-23T05:58:32,384][INFO ][o.e.c.m.MetaDataMappingService] [www.disconf.com] [customer/BjeAF7BbRYaMQAK7VX_yqA] create_mapping [_doc]
{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

这代表已经在索引customer下面创建成功了一个文档,他的id是1,使我们在建立索引的时候指定的。
注意:Elasticsearch并不需要在将文档编入索引之前先显式创建索引。在前面的示例中,如果客户索引事先尚未存在,则Elasticsearch将自动创建客户索引。

上面再插入文档的时候,指定了文档id,实际上,我们可以不指定id,如:
curl -X POST “localhost:9200/customer/_doc?pretty” -H ‘Content-Type: application/json’ -d’
{
“name”: “zhangsan”
}

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

可以看到,这里随机生成了一个文档id为"FSnz4WoBuS8YEwKRSe-g"

(二)根据ID查找文档

curl -X GET “localhost:9200/customer/_doc/1?pretty”

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

可以看到,我们查找到了这个文档,内容存放在_source属性里面。

(三)根据ID更换文档

当我们使用PUT或者是POST插入数据的时候,如果id对应的文档已经存在,那么旧的文档会被覆盖
curl -X PUT “localhost:9200/customer/_doc/1?pretty” -H ‘Content-Type: application/json’ -d’
{
“name”: “lisi”
}

再次查看:curl -X GET “localhost:9200/customer/_doc/1?pretty”

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "lisi"
  }
}
[

需要注意的是:这里会把整个文档进行更新,说白了,就是删除了原始文档的内容,而换成了新的文档内容

(四)根据ID更新文档

除了想(三)里面一样更换整个文档之外,还可以使用下面这种方式更新文档:
curl -X POST “localhost:9200/customer/_update/1?pretty” -H ‘Content-Type: application/json’ -d’
{
“doc”: { “name”: “zhaoqi”,“age”:100 }
}

不一定的地方在于:

  • url上面多了一个_update;
  • {}里面多了一个"doc"

此外,我们还可以通过脚本进行更新:
curl -X POST “localhost:9200/customer/_update/1?pretty” -H ‘Content-Type: application/json’ -d’
{
“script” : “ctx._source.age += 5”
}

这里的ctx._source.age指向了文档的age字段
当然,ES也提供了同时更新多个文档的功能,参看:
https://www.elastic.co/guide/en/elasticsearch/reference/7.1/docs-update-by-query.html
注意:这种更新的话,对于没有处理的属性,会保持原来的值

(五)根据ID删除文档

删除文档的操作很简单:
curl -X DELETE “localhost:9200/customer/_doc/1?pretty”

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

再次查找id为1的文档:curl -X GET “localhost:9200/customer/_doc/1?pretty”

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "found" : false
}

(六)批量操作

  1. 批量插入文档
    curl -X POST “localhost:9200/customer/_bulk?pretty” -H ‘Content-Type: application/json’ -d’
    {“index”:{"_id":“1”}}
    {“name”: “wukong”,“age”:10000,“sex”:“男” }
    {“index”:{"_id":“2”}}
    {“name”: “bajie” }
  2. 批量插入或者删除文档
    curl -X POST “localhost:9200/customer/_bulk?pretty” -H ‘Content-Type: application/json’ -d’
    {“update”:{"_id":“1”}}
    {“doc”: { “name”: “qitiandasheng” } }
    {“delete”:{"_id":“2”}}

(七)导入文档

这里模拟出1000个文档到json文件中,然后从文件中导入到es中。

  1. 产生json文件,可以在www.json-generator.com自己产生json文件
    也可以使用es官网提供的示例文件:accounts.json
  2. 把accounts.json文件放在服务器当前目录
  3. 执行导入操作:
    curl -H “Content-Type: application/json” -XPOST “localhost:9200/bank/_bulk?pretty&refresh” --data-binary “@accounts.json”
  4. 查看文档信息:
    curl “localhost:9200/_cat/indices?v”
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   bank     TmeDSLXnTKG4h2eiw-gUYA   1   1       1000            0    414.2kb        414.2kb
yellow open   customer OlNjOQgRSwu2zUQNpMConA   1   1          1            1      4.8kb          4.8kb

可以看到,有1000个文档被导入到bank索引。

五、删除索引

curl -X DELETE “localhost:9200/customer?pretty”

[2019-05-23T06:09:43,618][INFO ][o.e.c.m.MetaDataDeleteIndexService] [www.disconf.com] [customer/BjeAF7BbRYaMQAK7VX_yqA] deleting index
{
  "acknowledged" : true
}

再次查看所有的索引
curl -X GET “localhost:9200/_cat/indices?v”

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

发现不再有任何索引。

猜你喜欢

转载自blog.csdn.net/mail_liuxing/article/details/91827004
今日推荐