Elasticsearch の 教你学会基础API

作者: Kooola大数据 | 来源:发表于2018-10-12 20:30 被阅读12次
elasticsearchelasticsearch

集群健康

curl 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
1535619893 17:04:53  elasticsearch yellow          1         1     10  10    0    0       10             0                  -                 50.0%

可以看到:

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

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

列出所有索引

curl 'localhost:9200/_cat/indices?v'
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   weather NjoTuy-yTzihSe4UKq81Ug   5   1          0            0      1.2kb          1.2kb
yellow open   cbm     PFM_LEfKRbyhhpGjUex0Fg   5   1         20            0     97.6kb         97.6kb

可以看到:

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

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

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

创建一个索引

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

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

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   weather NjoTuy-yTzihSe4UKq81Ug   5   1          0            0      1.2kb          1.2kb
yellow open   blog    EsUfJEW2TF2RySj8Rdq8jw   5   1          0            0       460b           460b
yellow open   cbm     PFM_LEfKRbyhhpGjUex0Fg   5   1         20            0     97.6kb         97.6kb

创建文档

curl -XPUT 'localhost:9200/blog/user/2?pretty' -d '
{
  "name": "march"
}'
{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}

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,修改请求如下:

curl -XPUT -H "Content-Type: application/json" 'localhost:9200/blog/user/2?pretty' -d '
{
"name": "march kooola"
}'

请求返回如下:

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

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

1、blog为索引(index)

2、user为类型(type)

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

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

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

curl -XPUT -H "Content-Type: application/json" 'localhost:9200/blog/user/2?pretty' -d '
{
"name": "ZWQ"
}'

可以看到请求返回如下:

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

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

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

查询一个文档

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

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

{
  "_index" : "blog",
  "_type" : "user",
  "_id" : "2",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "ZWQ"
  }
}

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

删除文档

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

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

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

{
  "_index" : "blog",
  "_type" : "user",
  "_id" : "2",
  "found" : false
}

更新文档

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

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

curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '
{
  "doc": { "name": "zhangweiqing" }
}'

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

curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '
{
  "doc": { "name": "mouse", "age": 28 }
}'

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

curl -XPOST -H "Content-Type: application/json" 'localhost:9200/blog/user/2/_update?pretty' -d '
{
  "script" : "ctx._source.age += 1"
}'

相关文章

网友评论

    本文标题:Elasticsearch の 教你学会基础API

    本文链接:https://www.haomeiwen.com/subject/jkxnaftx.html