简体   繁体   English

Kibana:基于字段子字符串的饼图切片

[英]Kibana: pie chart slices based on substring of a field

I'm trying to create a pie chart visualization that will display the top 10 incoming requests. 我正在尝试创建一个饼图可视化,它将显示前10个传入请求。 I have a search query that filters only the incoming requests which have a field called messages which looks like the following: "Incoming request /api/someaction". 我有一个搜索查询,只过滤传入的请求,这些请求有一个名为messages的字段,如下所示:“Incoming request / api / someaction”。 How do I do the aggregation based on the /api/someaction part rather on the entire string (because then "Incoming" is counted as a term". 如何基于/ api / someaction部分而不是整个字符串进行聚合(因为那时“Incoming”被计为术语“。

Or...can I create custom field which are, for example, a substring of another field? 或者......我可以创建自定义字段,例如,另一个字段的子字符串吗?

Thanks 谢谢

As mentioned earlier in the comment, I've come up with a solution to my problem. 如前面评论中所述,我已经找到了解决问题的方法。 For me, I had values like foo bar baz and I needed to extract the first word. 对我来说,我有像foo bar baz这样的价值观,我需要提取第一个单词。 I was able to do this using the "Advanced → JSON" field, using the following script: 我可以使用“Advanced→JSON”字段,使用以下脚本执行此操作:

{
    "script": "( _value.indexOf(' ') > 0 ? _value.substring(0, _value.indexOf(' ')) : _value )"
}

So, in the Kibana interface this looks like this: 所以,在Kibana界面中,这看起来像这样:

具有脚本值的Kibana可视化配置


So, in your case, the script should probably be something like: 因此,在您的情况下,脚本可能应该是这样的:

{
    "script": "( _value.indexOf(' ') > 0 ? _value.substring(_value.lastIndexOf(' ')) : _value )"
}

Obviously, this assumes that the part of the message you want to extract follows the last space in the string. 显然,这假设您要提取的消息部分遵循字符串中的最后一个空格。 I've written a throwaway Java class to test the above: 我写了一个一次性的Java类来测试上面的内容:

public class Foo {

    public static void main(String[] args){
        String tester = "Incoming request /api/someaction";
        String result = tester.substring(tester.lastIndexOf(" "));
        System.out.println(result);
    }

}

As far as I can tell, you can use any Java code in the "script" key of the JSON field. 据我所知,您可以在JSON字段的“script”键中使用任何Java代码。 So you should also be able to use regexes using String.replaceAll or any other String method for that matter... 所以你也应该能够使用String.replaceAll或任何其他String方法使用正则表达式...

I haven't tested this though. 我没有测试过这个。 If someone has any information on this, feel free to leave a comment. 如果有人有任何相关信息,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM