如果细心的读者阅读过我之前的文章 “Elasticsearch:aggregation 介绍”,就会发现在我之前的例子中,我有用到 Script 来生成一个 field,并对这个 field 进行聚合统计。当时是这样做的:
GET sports/_search
{
"size": 0,
"aggs": {
"age_range": {
"range": {
"script": {
"source":
"""
def dob = doc['birthdate'].value;
return params.now - dob.getYear()
"""
,
"params": {
"now": 2019
}
},
"ranges": [
{
"from": 30,
"to": 31
}
]
}
}
}
}
那么我们可以在 Kibana 中做同样的事吗?
答案是肯定的。在今天的文章中,我将来介绍如何在 Kibana 中使用 Script 来生产一个 field,并对这个 field 进行统计。
什么是 Scripted Field?
Scripted Field 用中文讲就是脚本字段。它是基于脚本即时计算值
- 在查询时计算的值,但未编制索引
- 可能会占用大量资源,并且会影响 Kibana 的效果
- 没有验证
- Buggy 脚本将生成异常
- 可以使用 “Painless” 或者 “Luncene 表达式” 来写脚本
我们可以参考如下的链接:
-
https://www.elastic.co/guide/en/elasticsearch/reference/current/ modules-scripting-painless.html
-
https://www.elastic.co/guide/en/elasticsearch/reference/current/ modules-scripting-expression.html
下面我们还是使用一个具体的例子来讲述 Scripted Fields。
准备数据
为了能够展现如何生产 Scripted Filed,我们先使用如下的一个基本的数据。在 Kibana 中输入如下的数据:
POST _bulk
{ "index" : { "_index" : "twitter", "_id": 1} }
{"user":"张三","message":"今儿天气不错啊,出去转转去","uid":2,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}, "DOB":"1980-12-01"}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"老刘","message":"出发,下一站云南!","uid":3,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}, "DOB":"1981-12-01"}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"李四","message":"happy birthday!","uid":4,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}, "DOB":"1982-12-01"}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"老贾","message":"123,gogogo","uid":5,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}, "DOB":"1983-12-01"}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"老王","message":"Happy BirthDay My Friend!","uid":6,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}, "DOB":"1984-12-01"}
{ "index" : { "_index" : "twitter", "_id": 6} }
{"user":"老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}, "DOB":"1985-12-01"}
这个 twitter 的 schema 是这样的:
{
"twitter" : {
"mappings" : {
"properties" : {
"DOB" : {
"type" : "date"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"location" : {
"properties" : {
"lat" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lon" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"uid" : {
"type" : "long"
},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
我们发现这个 Schema 里含有一个叫做 DOB (date of birth)的字段,但是它没有一个叫做 age 的字段,也就是年龄。我们知道这个年龄字段其实是根据每次查询的时间,会有不同。比如今年查询和明年查询,显然显示的年龄应该会有不同。我们的目标是每次搜索时会自动生产相应的 age。
我们先打开 Kibana,然后点击Management:
我们选中 Twitter index pattern。如果大家不知到什么是 Inxdex pattern和如何生产 Index Pattern,请参阅我之前的文章“Kibana: 如何使用 Search Bar”。我们点击 Scripted Field:
然后点击 Add scripted field:
如上所示,我们想创建一个叫做 age 的 scripted field。它的值是 2019-doc['DOB'].value.getYear(),也就是用我们当前的年份减去生日的年份。由于这个脚本没有验证,有可能出现错误,那么我们如何测试它呢?
我们可以选择 Preview Results 来进行查看我们的结果。如果没有错误,那么我们可以看到输出的值。加入我们把脚本改为 2019-doc['DOB].value.getYear() 的话,显然这个是一个错误的脚本,在 DOB 那里少了一个引号。我们重复刚才的动作,那么:
这次显然是有错误的信息出现。我们可以通过这样的方法来排查我们的错误。最后,我们选择 Create field 按钮:
这样就看到了一个生成的叫做 age 的 Scripted field。那么我们该如何使用这个字段呢?我们点击 Discover,并搜索我们想要的年龄:
显然,这个时候,我们可以通过 age 来搜索我们的 index。但是必须记住的,这个新生成的字段只限于 Kibana 使用,并不会写回 twitter 索引,并生产相应的 token。
依赖于刚生成的 age,我们甚至可以做出相应的可视化图: