Skip to content

ElasticSearch和Kibana安装及简单使用

· 9 min · database / elastic / kibana

Start#

版本: ES 7.16.1, kibana 7.x

Environment#

  • MacOS Catalina 10.15.4
  • homebrew (v3.3.8)

Install#

Terminal window
To have launchd start elastic/tap/kibana-full now and restart at login:
brew services start elastic/tap/kibana-full
Or, if you don't want/need a background service you can just run:
kibana

Q.A#

  • tap方式安装会出现连接443 timeout的问题, 由于elastic/tap包在github上, 我这边没有做什么代理和科学上网方式, 多试了几次就成功了.

Config & Operation#

上述步骤结束后,可以按需设置一些参数以及简单的命令检查

ElasticSearch#

REST API#

  • POST/PUT/GET基础操作, POST和PUT在操作上没有区别
  • noted: REST API请参阅对应版本的官方文档, 会少走点弯路, 千万不要自己盲目去百度搜索, 网上博客资料很多es的版本都是不一致的. 官方传送门

新增索引#

Terminal window
DyMacBookPro:~ Dy$ curl -XPUT "http://localhost:9200/twitter"
{"acknowledged":true,"shards_acknowledged":true,"index":"twitter"}DyMacBookPro:~ Dy$

初始化mapping信息#

Terminal window
DyMacBookPro:~ Dy$ curl -XPUT "http://localhost:9200/twitter" -H 'Content-Type: application/json' -d'
> {
> "mappings": {
> "properties": {
> "name": { "type": "text" },
> "datetime": { "type": "date" },
> "email": { "type": "keyword" }
> }
> }
> }'
#返回结果
{"acknowledged":true,"shards_acknowledged":true,"index":"job"}DyMacBookPro:~ Dy$

noted: 在设置mapping的时候被坑了, 前面说到REST API不同版本的问题, 我开始参阅的是ES中文官方文档, 结果一直设置不成功, 后面在相关版本英文官方文档上找到了这个说明 Elasticsearch Mapping limit settings

新增单个mapping信息, 千万不要使用上面那个初始化的API,因为有可能出现exists error#

Terminal window
DyMacBookPro:~ Dy$ curl -XPUT "localhost:9200/twitter/_mappings?include_type_name=false&pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"desc": {
"type": "text"
}
}
}
'
#返回结果
{
"acknowledged" : true
}

设置mapping的分词属性#

Terminal window
DyMacBookPro:~ Dy$ curl -XPUT "localhost:9200/twitter/_mappings?include_type_name=false&pretty" -H 'Content-Type: application/json' -d'
> {
> "properties": {
> "message": {
> "type": "text",
> "analyzer": "ik_smart"
> }
> }
> }
> '
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "Failed to parse mapping [_doc]: analyzer [ik_max_word] has not been configured in mappings"
}
],
"type" : "mapper_parsing_exception",
"reason" : "Failed to parse mapping [_doc]: analyzer [ik_max_word] has not been configured in mappings",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "analyzer [ik_max_word] has not been configured in mappings"
}
},
"status" : 400
}

提示参数错误,那就说明需要安装分词plugin

查看已安装的plugin(空就是没有)#

Terminal window
DyMacBookPro:~ Dy$ curl -XGET localhost:9200/_cat/plugins
DyMacBookPro:~ Dy$

安装plugin#

这里我们安装一个中文ik分词插件 (需安装对应版本) 传送门

Terminal window
DyMacBookPro:~ Dy$ curl -XPUT "localhost:9200/twitter/_mappings?include_type_name=false&pretty" -H 'Content-Type: application/json' -d'
> {
> "properties": {
> "message": {
> "type": "text",
> "analyzer": "ik_smart"
> }
> }
> }
> '
#返回结果没有报错
{
"acknowledged" : true
}
#确认下,看到已经添加成功了
DyMacBookPro:~ Dy$ curl -X GET "localhost:9200/twitter?pretty"
{
"twitter" : {
"aliases" : { },
"mappings" : {
"properties" : {
"datetime" : {
"type" : "date"
},
"desc" : {
"type" : "text"
},
"email" : {
"type" : "keyword"
},
"message" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "twitter",
"creation_date" : "1639651383574",
"number_of_replicas" : "0",
"uuid" : "6qUBOpt0TXy8YupU5_TdbA",
"version" : {
"created" : "7160199"
}
}
}
}
}

noted: 目前mapping不支持删除,更新

新增文档#

Terminal window
# 根据最后/_doc/{_id}参数进行匹配, 如果库里有匹配的id值则更新, 没有则新增
DyMacBookPro:~ Dy$ curl -XPUT "http://localhost:9200/twitter/_doc/olsond" -H 'Content-Type: application/json' -d'
> {
> "name": "olsond",
> "datetime": "2021-12-16",
> "email": "olsond@foxmail.com"
> }'
#返回结果
{"_index":"twitter","_type":"_doc","_id":"olsond","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":3,"_primary_term":1}DyMacBookPro:~ Dy$

noted: 新版本下/_doc参数是固定的

查找单个索引#

Terminal window
DyMacBookPro:~ Dy$ curl -X GET "localhost:9200/twitter?pretty"
{
"twitter" : {
"aliases" : { },
"mappings" : {
"properties" : {
"datetime" : {
"type" : "date"
},
"desc" : {
"type" : "text"
},
"email" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "twitter",
"creation_date" : "1639651383574",
"number_of_replicas" : "0",
"uuid" : "6qUBOpt0TXy8YupU5_TdbA",
"version" : {
"created" : "7160199"
}
}
}
}
}

列出所有索引信息#

Terminal window
DyMacBookPro:~ Dy$ curl -X GET "http://localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases QH2rX52lTNC-RdHxuCWTgA 1 0 42 35 41.2mb 41.2mb
yellow open twitter 6qUBOpt0TXy8YupU5_TdbA 1 0 1 0 9.3kb 9.3kb
green open .apm-custom-link S0CeP8VnQF6JwD8jhPnTgg 1 0 0 0 226b 226b
green open .kibana_7.16.1_001 eMUehkSHThWFHIuOeYYw9Q 1 0 666 302 2.5mb 2.5mb
green open .kibana_task_manager_7.16.1_001 4VF3CL6UT2W_2OZVNEilwQ 1 0 17 11552 9mb 9mb
green open .apm-agent-configuration BBMUtWbGRASk11ilJoLHJA 1 0 0 0 226b 226b
green open .async-search _Li-ufp_TBeWYFKyPO_x9g 1 0 0 0 252b 252b
green open .tasks C2aJ2IeNQq2rDGHovFmxpg 1 0 10 0 50.2kb 50.2kb
Terminal window
curl -X PUT 'http://localhost:9200/_settings' -H 'Content-Type: application/json' -d '
{
"number_of_replicas": 0
}
'
#返回结果
{"acknowledged":true}DyMacBookPro:~ Dy$

Kibana#

kibana本地启动会自动去连接本地的elasticsearch服务不需要额外配置