Kibana 是在 Elasticsearch 有了相當多的數據之后,進行分析這些數據用的工具。Kibana 里面有一個叫做 Dev Tools 的工具,可以很方便地以 Restful 風格向 Elasticsearch 服務器提交請求。類似於使用Navicat工具連接MySQL這種關系型數據庫,對數據庫做操作。
啟動
- 選擇合適版本安裝:https://www.elastic.co/cn/downloads/past-releases#kibana
- 在kibana的解壓文件中啟動kibana.bat
- 打開管理站點:http://localhost:5601/app/kibana#/dev_tools/console?_g=()
- 運行測試(查看服務器狀態健康度,green表示一切OK):GET /_cat/health?v
Elasticsearch和kibana版本對應關系
Kibana版本 | ES版本 |
---|---|
4.1 | 1.4.4 + |
4.2 | 2.0 + |
4.3 | 2.1 + |
4.4 | 2.2 + |
4.5 | 2.3 + |
4.6 | 2.4 + |
5 | 5 + |
... | ... |
使用簡介
- ?pretty格式化返回json數據
索引管理
-- 增加索引
PUT /letbingo?pretty
-- 查詢所有索引
GET /_cat/indices?v
-- 刪除索引
DELETE /letbingo?pretty
中文分詞器
GET _analyze
{
"analyzer":"ik_max_word",
"text":"趵突泉遭遇停噴危機"
}
文檔管理
-- 增加文檔
-- person在elasticsearch里是type的概念,相當於數據庫里的表,這里就相當於向person表里插入了一條數據
PUT /letbingo/person/1?pretty
{
"name": "lego"
}
-- 獲取文檔
GET /letbingo/person/1?pretty
-- 修改文檔
-- 修改后版本號會發生變化
POST /letbingo/person/1/_update?pretty
{
"doc": { "name": "letbingo" }
}
-- 刪除文檔
DELETE /letbingo/person/1?pretty
批量導入
簡單的多條記錄同時導入
這種方式能夠插入的上限較小
POST _bulk
{"index":{"_index":"letbingo","_type":"person","_id":1}}
{"sex":"male","age":18,"name":"lego","place":"武漢","descrption":"javer"}
{"index":{"_index":"letbingo","_type":"person","_id":2}}
{"sex":"female","age":20,"name":"lisa","place":"上海","descrption":"ui"}
使用curl工具批量導入
curl是一個工具,可以模擬瀏覽器向服務器提交數據。
資源鏈接:https://pan.baidu.com/s/1HCN4nM1dSVvYrCeQnM15Cg 提取碼:526r
- 按照第一種方法中的格式把記錄寫入json文件中
- 把curl.exe和json文件放在同一個目錄下
- 在cmd中,運行如下命令:
cd C:\Downloads\curl
curl -H "Content-Type: application/json" -XPOST "localhost:9200/letbingo/person/_bulk?refresh" --data-binary "@persons.json"
查詢操作
- 查詢所有
GET /letbingo/_search
{
"query": { "match_all": {} }
}
- 根據id倒序排列
GET /letbingo/_search
{
"query": { "match_all": {} },
"sort": [
{ "_id": "desc" }
]
}
- 只返回指定字段
GET /letbingo/_search
{
"query": { "match_all": {} },
"_source": ["name","age"]
}
- 條件查詢
GET /letbingo/_search
{
"query": { "match": { "name": "lisa" } }
}
- 根據時間段查詢
GET /letbingo/position/_search
{
"query": {
"range" : {
"time" : {
"gte": "15810971331",
"lte": "15830971331"
}
}
}
}
- 分頁查詢
GET /letbingo/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 3,
"sort": { "_id": { "order": "desc" } }
}
- 聚合查詢(類似傳統數據庫中的聚合查詢)
統計數據,第一個size:0表示不用顯示每條數據,第二個size:3表示分組數據顯示3條。
GET /letbingo/_search
{
"size": 0,
"aggs": {
"group_by_place": {
"terms": {
"field": "place.keyword",
"size": 3
}
}
}
}
相當於Sql語句:select count(*),place from product group by place limit 0,3
可能遇到的問題
- Fielddata is disabled on text fields by default. Set fielddata=true on [id] in order to load fielddata in memory by uninverting the inverted index
原因:因為加載Fielddata是一個昂貴的過程,可能會導致用戶遇到延遲命中。所以默認禁用了Fielddata。解決方法是在聚合前發送指令開啟Fielddata:
PUT /my_index/_mapping
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
- Elasticsearch health status顯示為yellow
解決方法:設置所有副本(rep)個數為0
curl -XPUT "http://localhost:9200/_settings" -H 'Content-Type: application/json' -d'
{
"index" : {
"number_of_replicas" : 0
}
}