Kibana對索引動態加字段顯示


本文介紹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. 最后

動態新加的字段在顯示時不影響原字段,在制作一些圖表時相當有用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM