我们可以通过 CMD 命令行界面、Kibana、各种主流语言的客户端来操作和管理 Elasticsearch。

这里我们主要采用的是 Windows 中的 cmder(一个命令行工具,自带 curl 命令等)。

Windows 中使用 curl 命令的注意事项:

  • curl 的目标地址可以不用引号括起来,如果要用,只能用双引号。
  • header 头的写法:-H "Content-Type: application/json"
  • json 格式的数据必须用反斜线进行转义。
  • 如果需要换行继续输入,需在行尾用 ^ 符号。

Index 索引库

查看当前节点的所有 Index

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

返回结果:

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana OIGeqCZPRmCnysU5P0Ks8g   1   1          1            0      3.1kb          3.1kb

说明:

  • health:代表索引的健康情况。green 表示完全正常;yellow 表示功能正常,有些副本节点未分配(单节点的常见情况);red 表示有些数据不可用,功能不正常。
  • 如果返回结果只有第一行的标题行,说明集群中没有索引,可手动创建。

创建 Index

# 创建一个名称为 alibaba 的 Index(索引库)
curl -X PUT localhost:9200/alibaba

返回结果如下:

{"acknowledged":true,"shards_acknowledged":true}

删除 Index

curl -X DELETE localhost:9200/alibaba

说明: 删除 Index 就是删除索引库(包含里面的记录),切勿在正式环境中操作。

列出每个 Index 包含的 Type

curl "localhost:9200/_mapping?pretty=true"

Document 文档

新增记录

# 在 alibaba/user 中,新增一条记录(文档),记录的 id 为 1
curl -X PUT localhost:9200/alibaba/user/1 -d {\"name\":\"rose\"}

curl -X PUT localhost:9200/alibaba/user/1 -H "Content-Type: application/json" -d {\"name\":\"rose\"}

返回结果:

{                         
  "_index" : "alibaba",   
  "_type" : "user",       
  "_id" : "1",            
  "_version" : 1,         
  "result" : "created",   
  "_shards" : {           
    "total" : 2,          
    "successful" : 1,     
    "failed" : 0          
  },                      
  "created" : true        
}

上述示例中,alibaba 代表 Index,user 代表 Type,如果它们不存在,会自动创建。1 代表记录的 id。

新增记录时,也可以不指定 id,这时服务器会自动生成一个随机字符串作为该记录的 id。

curl -X POST localhost:9200/alibaba/user -d {\"name\":\"jack\"}

返回结果:

{"_index":"alibaba","_type":"user","_id":"AWvx2bkAtj-co4pkgSQO","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

查看记录

# 查看 alibaba/user/1 记录
curl localhost:9200/alibaba/user/1

# 查看 alibaba/user/1 记录(会对结果进行格式化)
curl localhost:9200/alibaba/user/1?pretty

说明:?pretty 表示对返回结果进行格式化,以便更易于阅读。

返回结果:

{                        
  "_index" : "alibaba",  
  "_type" : "user",      
  "_id" : "1",           
  "_version" : 1,        
  "found" : true,        
  "_source" : {          
    "name" : "rose"      
  }                      
}
# 查看 alibaba/user 中的所有记录
curl localhost:9200/alibaba/user/_search

更新记录

# 更新指定的记录,采用 PUT 请求,当然也可以用 POST
curl -X PUT localhost:9200/alibaba/user/1?pretty -d {\"name\":\"rose_1\"}

返回结果:

{                       
  "_index" : "alibaba", 
  "_type" : "user",     
  "_id" : "1",          
  "_version" : 2,       
  "result" : "updated", 
  "_shards" : {         
    "total" : 2,        
    "successful" : 1,   
    "failed" : 0        
  },                    
  "created" : false     
}

可以发现,version(版本)变为了2,result的值变为了 updated,created 的值为 false。

删除记录

curl -X DELETE localhost:9200/alibaba/user/1?pretty

返回结果:

{
  "found" : true,
  "_index" : "alibaba",
  "_type" : "user",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

总结

虽然,我们可以直接用 CMD 命令行来操作 Elasticsearch,但是,总的来说,它还是不大方便。因此,强烈推荐使用 Kibana 来操作 Elasticsearch。