Kibana對索引動態加欄位顯示
阿新 • • 發佈:2020-09-20
本文介紹Kibana對索引動態加欄位顯示。在實際業務資料存入Elasticsearch索引時會有一些列舉值,這些列舉值的意義不直觀,也沒必要在存索引時特意加一個用於顯示的欄位。這種場景只需在Kibana對查出的所有做一個指令碼對映,新生成一個欄位,不影響原Elasticsearch索引。
本文使用的Elasticsearch和Kibana版本都是7.9.0,Docker部署。先在Elasticsearch中存入一批資料,索引是一個簡化過的訂單資料,例子如下
{ "_index":"es-syc-demo-order-2020.09", "_type":"_doc", "_id":"2020091822382704930", "_version":1, "_score":1, "_source":{ "_class":"com.mingo.es.sync.document.OrderEntity", "id":"2020091822382704930", "tradeNo":"2020091822382704930", "buyerId":9527, "sellerId":18899, "type":1, "status":1, "amount":1, "discountAmount":0, "originAmount":1, "createTime":1600439907049, "lines":[ { "tradeNo":"2020091822382704930", "lineNo":"1", "itemCode":"6352678819", "itemName":"泡椒鳳爪", "unitCode":"DAI", "unitName":"袋", "type":1, "itemPrice":1, "price":1, "discountPrice":0, "itemQty":1, "totalPrice":1, "paidPrice":1, "createTime":1600439907049 } ] } }
1. 建立索引匹配
也就是Kibana中“Create index pattern”,也就是在Kibana中建立一個ES查詢入口,所有圖表的製作都是基於該pattern。
建立
建立好的pattern
在Discover中檢視
2. 在pattern直接對映原欄位
這種方式修改了欄位的“Format”,在索引展示時會覆蓋原值展示,只是做了一個展示對映。
將“Format”改為“Static lookup”,就可以在下發寫入對映值。
在Discover中檢視
3. 在pattern中使用指令碼動態新增新欄位
新增新欄位“order-type”
編輯指令碼
def map = ['t1': '官方商城', 't2': '傳統零售']; def key = 't' + doc['type'].value; def type = map[key]; if (type == null) { return "其他"; } return type;
在Discover中可以看到,查詢時多了一個“order-type”欄位
同理,還可以新增其他欄位,比如再新增一個“order-status”欄位
def map = ['t1': '待支付', 't3': '待發貨', 't5': '待收貨', 't7': '已收貨'];
def key = 't' + doc['status'].value;
def status = map[key];
if (status == null) { return "其他"; }
return status;
在Discover中用“order-type”欄位過濾,這裡最終查詢過濾時會轉化為“type”原值搜尋
4. 最後
動態新加的欄位在顯示時不影響原欄位,在製作一些圖表時相當有用。