Appearance
Kibana 使用
通过Kibana的
Dev Tools功能,我们可以操作Elasticsearch;使用如下命令查看集群健康状态;
GET /_cat/health?v- 查看节点状态;
GET /_cat/nodes?vip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 16 64 34 cdfhilmrstw * DESKTOP-K1F7O7Q- 查看所有索引信息;
GET /_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open pms 6cEV5X3FSYWlGEbLCsMpmg 1 0 57 0 24.3kb 24.3kb
green open .kibana_7.17.3_001 hrS91kWhQkajmrhF92zboQ 1 0 327 327 4.8mb 4.8mb
green open .tasks _4RSAkwvRwK-j8zDLAM6MA 1 0 12 0 22.5kb 22.5kb索引操作
- 创建索引并查看;
PUT /customer
GET /_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open pms 6cEV5X3FSYWlGEbLCsMpmg 1 0 57 0 24.3kb 24.3kb
yellow open customer mU0uITAkSaeEie8fypLnFw 1 1 0 0 226b 226b
green open .kibana_7.17.3_001 hrS91kWhQkajmrhF92zboQ 1 0 329 336 4.8mb 4.8mb- 删除索引并查看;
DELETE /customer
GET /_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open pms 6cEV5X3FSYWlGEbLCsMpmg 1 0 57 0 24.3kb 24.3kb
green open .kibana_7.17.3_001 hrS91kWhQkajmrhF92zboQ 1 0 329 336 4.8mb 4.8mb类型操作
查看文档的类型,需要完成数据搜索部分的导入才可以查看。
GET /bank/_mapping{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}文档操作(数据操作)
- 在索引中添加文档;
PUT /customer/doc/1
{
"name": "John Doe"
}{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}- 查看索引中的文档;
GET /customer/doc/1{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}- 修改索引中的文档:
POST /customer/doc/1/_update
{
"doc": { "name": "Jane Doe" }
}{
"_index": "customer",
"_type": "doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}- 删除索引中的文档;
DELETE /customer/doc/1{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}- 对索引中的文档执行批量操作;
POST /customer/doc/_bulk
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }{
"took" : 9,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 3,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1,
"status" : 201
}
}
]
}数据搜索(查询)
查询表达式(Query DSL)是一种非常灵活又富有表现力的查询语言,Elasticsearch使用它可以以简单的JSON接口来实现丰富的搜索功能,下面的搜索操作都将使用它。
数据导入
- 首先我们需要导入一定量的数据用于搜索,使用的是银行账户表的例子,数据结构如下:
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}- 我们先复制下需要导入的数据,数据地址: https://github.com/macrozheng/mall-learning/blob/teach/document/json/accounts.json
- 然后直接使用批量操作来导入数据,注意本文所有操作都在Kibana的
Dev Tools中进行;
POST /bank/account/_bulk
{
"index": {
"_id": "1"
}
}
{
"account_number": 1,
"balance": 39225,
"firstname": "Amber",
"lastname": "Duke",
"age": 32,
"gender": "M",
"address": "880 Holmes Lane",
"employer": "Pyrami",
"email": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
......省略若干条数据- 导入完成后查看索引信息,可以发现
bank索引中已经创建了1000条文档。
GET /_cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank ycOSgiWjQomwzdygGwqOrQ 1 1 1000 0 374.5kb 374.5kb