Elasticsearch の 教你学会基础API

216 阅读4分钟
原文链接: mp.weixin.qq.com

集群健康

  1. curl localhost:9200/_cat/health?v

  1. epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent

  2. 1535619893 17:04:53  elasticsearch yellow          1         1     10  10    0    0       10             0                  -                 50.0%

可以看到:

1、集群名称为默认的"elasticsearch"

2、集群状态为"yellow",总共有三种状态:"green","yellow","red"

列出所有索引

  1. curl 'localhost:9200/_cat/indices?v'

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

  2. yellow open   weather NjoTuy-yTzihSe4UKq81Ug   5   1          0            0      1.2kb          1.2kb

  3. yellow open   cbm     PFM_LEfKRbyhhpGjUex0Fg   5   1         20            0     97.6kb         97.6kb

可以看到:

1、集群中已经有两个索引,分别是:weather和cbm。

2、每个索引有5个主分片和一个复制(默认)。

3、其中cbm的索引下又20个文档。

创建一个索引

  1. curl -XPUT 'localhost:9200/blog?pretty'

然后列出所有索引,就会发现索引已经创建好

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

  2. yellow open   weather NjoTuy-yTzihSe4UKq81Ug   5   1          0            0      1.2kb          1.2kb

  3. yellow open   blog    EsUfJEW2TF2RySj8Rdq8jw   5   1          0            0       460b           460b

  4. yellow open   cbm     PFM_LEfKRbyhhpGjUex0Fg   5   1         20            0     97.6kb         97.6kb

创建文档

  1. curl -XPUT 'localhost:9200/blog/user/2?pretty' -d '

  2. {

  3.  "name": "march"

  4. }'

  1. {

  2.  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",

  3.  "status" : 406

  4. }

Starting from Elasticsearch 6.0, all REST requests that include a body must also provide the correct content-type for that body. 自6.0版本之后需要做content-type检查(如果不加就会报以上406错误),所以请求是加上content-type,修改请求如下:

  1. curl -XPUT -H "Content-Type: application/json" 'localhost:9200/blog/user/2?pretty' -d '

  2. {

  3. "name": "march kooola"

  4. }'

请求返回如下:

  1. {

  2.  "_index" : "blog",

  3.  "_type" : "user",

  4.  "_id" : "2",

  5.  "_version" : 1,

  6.  "result" : "created",

  7.  "_shards" : {

  8.    "total" : 2,

  9.    "successful" : 1,

  10.    "failed" : 0

  11.  },

  12.  "_seq_no" : 0,

  13.  "_primary_term" : 1

  14. }

解释下这个"localhost:9200/blog/user/2?pretty" :

1、blog为索引(index)

2、user为类型(type)

3、数字2表示文档的id,这里指定了该文档的id为2

4、请求返回中可以看到,版本号为1,result为新建(create)

接下来再把刚才的请求重新请求,修改下name

  1. curl -XPUT -H "Content-Type: application/json" 'localhost:9200/blog/user/2?pretty' -d '

  2. {

  3. "name": "ZWQ"

  4. }'

可以看到请求返回如下:

  1. {

  2.  "_index" : "blog",

  3.  "_type" : "user",

  4.  "_id" : "2",

  5.  "_version" : 2,

  6.  "result" : "updated",

  7.  "_shards" : {

  8.    "total" : 2,

  9.    "successful" : 1,

  10.    "failed" : 0

  11.  },

  12.  "_seq_no" : 1,

  13.  "_primary_term" : 1

  14. }

id还是2,版本号变成2,result类型变成更新(update)

在索引的时候,ID部分是可选的。如果不指定,Elasticsearch将产生一个随机的ID来索引这个文档。Elasticsearch 生成的ID会作为索引API调用的一部分被返回。

查询一个文档

  1. curl -XGET 'localhost:9200/blog/user/2?pretty'

该请求查询索引为blog,类型为user,文档id为2的文档 请求返回如下:

  1. {

  2.  "_index" : "blog",

  3.  "_type" : "user",

  4.  "_id" : "2",

  5.  "_version" : 2,

  6.  "found" : true,

  7.  "_source" : {

  8.    "name" : "ZWQ"

  9.  }

  10. }

可以看到,查询到的文档name为ZWQ(更新后的值)

删除文档

  1. curl -XDELETE 'localhost:9200/blog/user/2?pretty'

以上命令删除索引为blog,类型为user,文档id为2的文档 此时再查询该文档,返回就会告诉我们该文档找不到(found为false)

  1. curl -XGET 'localhost:9200/blog/user/2?pretty'

  2. {

  3.  "_index" : "blog",

  4.  "_type" : "user",

  5.  "_id" : "2",

  6.  "found" : false

  7. }

更新文档

除了可以索引、替换文档之外,我们也可以更新一个文档。但要注意,Elasticsearch底层并不支持原地更新。在我们想要做一次更新的时候,Elasticsearch先删除旧文档,然后再索引更新的新文档。

下面的例子展示了怎样将ID为2的文档的name字段改成“zhangweiqing”:

  1. curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '

  2. {

  3.  "doc": { "name": "zhangweiqing" }

  4. }'

下面的例子展示了怎样将ID为2的文档的name字段改成“mouse”的同时,给它加上age字段:

  1. curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '

  2. {

  3.  "doc": { "name": "mouse", "age": 28 }

  4. }'

更新也可以通过使用简单的脚本来进行。这个例子使用一个脚本将age加1(ctx._source指向当前被更新的文档):

  1. curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '

  2. {

  3.  "script" : "ctx._source.age += 1"

  4. }'