kibana中查询elasticsearch数据的方法

1.  Lucene查询语法

Kibana查询语言基于Lucene查询语法。

  • 为了执行一个文本搜索,可以简单的输入一个文本字符串。例如,如果你想搜索web服务器的日志,你可以输入关键字"safari",这样你就可以搜索到所有有关"safari"的字段
  • 为了搜索一个特定字段的特定值,可以用字段的名称作为前缀。例如,你输入"status:200",将会找到所有status字段的值是200的文档
  • 为了搜索一个范围值,你可以用括号范围语法,[START_VALUE TO END_VALUE]。例如,为了找到状态码是4xx的文档,你可以输入status:[400 TO 499]
  • 为了指定更改复杂的查询条件,你可以用布尔操作符 AND , OR , 和 NOT。例如,为了找到状态码是4xx并且extension字段是php或者html的文档,你可以输入status:[400 TO 499] AND (extension:php OR extension:html)

 双引号括起来表示一个整体,需要完全匹配,不进行分词。

2.  Kibana查询语法增强

基本原理保持不变,我们只是简单地改进了一些东西,使查询语言更易于使用。

  • response:200 将匹配response字段的值是200的文档
  • 用引号引起来的一段字符串叫短语搜索。例如,message:"Quick brown fox"  将在message字段中搜索"quick brown fox"这个短语。如果没有引号,将会匹配到包含这些词的所有文档,而不管它们的顺序如何。
  • 查询解析器将不再基于空格进行分割。多个搜索项必须由明确的布尔运算符分隔。注意,布尔运算符不区分大小写。
  • 在Lucene中,response:200 extension:php 等价于 response:200 and extension:php。这将匹配response字段值匹配200并且extenion字段值匹配php的文档。
  • 如果我们把中间换成or,那么response:200 or extension:php将匹配response字段匹配200 或者 extension字段匹配php的文档。
  • 默认情况下,and 比 or 具有更高优先级。
  • 括号可以改变这种优先级
  • not response:200 将匹配response不是200的文档
  • 范围检索和Lucene有一点点不同,代替 byte:>1000,我们用byte > 1000
  • >, >=, <, <= 都是有效的操作符
  • response:*  将匹配所有存在response字段的文档;
  • 通配符查询也是可以的。machine.os:win* 将匹配machine.os字段以win开头的文档;
  • 通配符也允许我们一次搜索多个字段。假设我们有machine.os和machine.os.keyword两个字段,我们想要搜索这两个字段都有"windows 10",那么我们可以这样写"machine.os*:windows 10";

3.  Kibana查询语法增强举例

datetime:"2020-08-26"

查询语句表示,查询datetime中包含“2020-08-26”字符串的记录;

message: "请联系管理员" and datetime: "2021-09-29" and node:"node3"

Kibana Query Language | Kibana Guide [7.4] | Elastic

response:200 will match documents where the response field matches the value 200

message:"Quick brown fox" will search for the phrase "quick brown fox" in the message field

response:200 extension:php would become response:200 or extension:php in KQL

response:200 and extension:php will match documents where response matches 200 and extension matches php.

response:200 and extension:php or extension:css will match documents where response is 200 and extension is php OR documents where extension is css and response is anything.

response:200 and (extension:php or extension:css) will match documents where response is 200 and extension is either php or css.

Instead of bytes:>1000, we omit the colon: bytes > 1000.

>, >=, <, <= are all valid range operators.

Exist queries are simple and do not require a special operator. response:* will find all docs where the response field exists.

Wildcard queries are available. machine.os:win* would match docs where the machine.os field starts with "win", which would match values like "windows 7" and "windows 10".

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐