文章目录
本文档主要是跟着官方文档操作的
语法规则
es提供restful API风格的查询
curl -X <动作> ‘< protocol >://:/
-X指定动作,比如PUT等,-d后面跟一个json字符串
另外,我们可以使用es-head插件或者postman会更方便
查看集群的健康状态
curl -X GET "10.10.31.8:9200/_cat/health?v&pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/health?v&pretty"
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1590648220 06:43:40 my-es green 3 3 0 0 0 0 0 0 - 100.0%
查看集群节点
curl -X GET "10.10.31.8:9200/_cat/nodes?pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/nodes?pretty"
10.10.31.8 23 91 6 0.11 0.12 0.09 mdi - node-1
10.10.31.28 9 93 0 0.00 0.02 0.05 mdi * node-2
10.10.31.26 12 96 0 0.00 0.01 0.05 mdi - node-3
列出所有的指数
curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
这表示集群中尚没有索引
创建一个索引
创建索引的语句:- X后面使用动作PUT
curl -X PUT "10.10.31.8:9200/customer?pretty&pretty"
实操:创建并查看索引
[root@lvs ~]# curl -X PUT "10.10.31.8:9200/customer?pretty&pretty"
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "customer"
}
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open customer eNXdpm_KQdq5V1Q7gPvrBQ 5 1 0 0 1.2kb 631b
向这个索引中添加一个document
curl -X PUT "10.10.31.8:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
[root@lvs ~]# curl -X PUT "10.10.31.8:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 2
}
从上面可以看到,在customer索引中成功创建了一个新的客户文档。该文档的内部ID为1,这是我们在索引时指定的。
重要的是要注意,Elasticsearch不需要您首先显式创建索引,然后才能将文档建立索引。在上一个示例中,Elasticsearch将自动创建事先不存在的客户索引。
现在,让我们检索刚刚索引的文档:
DSL语句:
curl -X GET "localhost:9200/customer/doc/1?pretty&pretty"
[root@lvs ~]# curl -X GET "localhost:9200/customer/doc/1?pretty&pretty"
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 2,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
删除索引
现在,我们删除索引,然后再次列出所有的索引
curl -X DELETE "localhost:9200/customer?pretty&pretty"
curl -X GET "localhost:9200/_cat/indices?v&pretty"
[root@lvs ~]# curl -X DELETE "localhost:9200/customer?pretty&pretty"
{
"acknowledged" : true
}
[root@lvs ~]# curl -X GET "localhost:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.stor