1、确保9200端口号可用:
Kibana’s Console: `GET /_cat/health?v`
curl: `curl -XGET "127.0.0.1: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
1559043852 19:44:12 my-application yellow 1 1 31 31 0 0 31 0 - 50.0%
2、_cat查询当前es集群中所有的节点列表
curl -XGET "http://localhost:9200/_cat/nodes?v"
GET /_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 19 90 8 1.25 mdi * ES-1
3、_cat查询当前es集群中所有的indices索引
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank o6u9CX5mTkqN-NB7V8M3fw 5 1 1000 0 640.7kb 640.7kb
yellow open .kibana p9gEcKDST5eXh1OdFtTInw 1 1 6 0 36.5kb 36.5kb
yellow open logstash-2015.05.20 a8oXcZqZSO2s7W4OPIIIDQ 5 1 4750 0 29.6mb 29.6mb
yellow open logstash-2015.05.19 sD2v9JsuQJCHDbPi05x-SQ 5 1 4624 0 28.6mb 28.6mb
yellow open shakespeare SHXAuucKScSCN3m4cIWJRw 5 1 111396 0 28.1mb 28.1mb
yellow open wisestar aduJf_1lSuS-A3LTBuve4w 5 1 0 0 810b 810b
yellow open logstash-2015.05.18 0M_Y6DF5SJCTDLvpIW403Q 5 1 4631 0 29.7mb 29.7mb
pri:主分片
rep:复制分片
docs:文档
黄色意味着某些复制没有(或者还未)被分配。这个索引之所以这样,是因为Elasticsearch默认为这个索引创建一份复制。由于现在我们只有一个节点在运行,那一份复制就分配不了了(为了高可用),直到当另外一个节点加入到这个集群后,才能分配。一旦那份复制在第二个节点上被复制,这个节点的健康状态就会变成绿色。
模糊查询:
GET /_cat/indices/app_index_common_*
4、创建索引:customer
PUT /customer?pretty
out:
{
"acknowledged": true,
"shards_acknowledged": true
}
将pretty附加到调用的尾部,使其以美观的形式打印出JSON响应
5、将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID是1,操作如下:
PUT /customer/external/1?pretty
{
"name":"Johne Doe"
}
out:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
Elasticsearch在你想将文档索引到某个索引的时候,并不强制要求这个索引被显式地创建。在前面这个例子中,如果customer索引不存在,Elasticsearch将会自动地创建这个索引。
6、查询
取出id为1的客户资料
GET /customer/external/1?pretty
out:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "Johne Doe"
}
}
查询所有以cust开头的索引
GET /cust*/_search?pretty
out:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "customer",
"_type": "external",
"_id": "2",
"_score": 1,
"_source": {
"name": "Johne Doe2"
}
},
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_score": 1,
"_source": {
"name": "Johne Doe"
}
},
{
"_index": "customer",
"_type": "external",
"_id": "3",
"_score": 1,
"_source": {
"name": "Johne Doe2"
}
}
]
}
}
7、删除索引:
DELETE /customer?pretty
out:
{
"acknowledged": true
}
也可以一次删除符合某个查询条件的多个文档,比如删除名字中包含“John”的所有的客户:
DELETE /customer/external/_query?pretty
{
"query":{"match": {
"name": "John"
}}
}
8、更新doc:
将id为2的doc更新:
POST /customer/external/2/_update?pretty
{
"doc":{
"name":"tina2",
"age":20
}
}
out:
{
"_index": "customer",
"_type": "external",
"_id": "2",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
更新也可以通过使用简单的脚本来进行。这个例子使用一个脚本将age加5:
POST /customer/external/2/_update?pretty
{
"script":"ctx._source.age += 5"
}
out:
{
"_index": "customer",
"_type": "external",
"_id": "2",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
ctx._source指向当前要被更新的文档。
9、载入样本数据
将json文本加载到我们的集群里:
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
- 1 需要在accounts.json所在的目录运行curl命令。
- 2 localhost:9200是ES得访问地址和端口
- 3 bank是索引的名称
- 4 account是类型的名称
- 5 索引和类型的名称在文件中如果有定义,可以省略;如果没有则必须要指定
- 6 _bulk是rest得命令,可以批量执行多个操作(操作是在json文件中定义的,原理可以参考之前的翻译)
- 7 pretty是将返回的信息以可读的JSON形式返回。