考点:queries
考试目标:构建terms、数字、日期、模糊匹配及符合查询语句
初始化步骤:
建议docker-compose文件:1e1k_l
kibana_sample_data_logs
index kibana_sample_data_logs
索引中运行下面的搜索语句docker-compose -f 1e1k_l up -d --build
Add Data to Kibana
(中文大概是“为Kibana添加数据”,没有中文版可以测试,具体的要看具体翻译)Sample data
(中文大概是“样例数据”)Add data
GET _cat/indices
kibana_sample_data_flights
和 kibana_sample_data_logs
,代表添加成功green open kibana_sample_data_flights TxLrY4R4RB2wRcNsw5bQ9Q 1 0 13059 0 6.5mb 6.5mb
green open kibana_sample_data_ecommerce -BmN-n3MRgOdtIrINDeufw 1 0 4675 0 4.9mb 4.9mb
green open kibana_sample_data_logs CfNuYq1kTvelLLVG0T6biA 1 0 14074 0 11.8mb 11.8mb
response
field greater or equal to 400 and less than 500 response
大于等于400而且小于等于500的文档referer
field matching “” referer
字段需要匹配 “”referer
field that starts by “” referer
字段以 “” 开头的文档request
field that starts by “/people” request
字段以 “/people” 开头的文档memory
field containing any indexed value memory
字段的文档memory
field not containing any indexed value memory
字段的文档agent
field containing the string “Windows” and the url
field containing the string “name:john” agent
字段包含 “Windows” 而且 url
字段包含 “name:john” 的文档phpmemory
field containing any indexed value phpmemory
存在的文档response
field greater or equal to 400 or the tags
field having the string “error” response
字段大于等于 400 或者 tags
字段包含 “error” 的文档tags
field that does not contain any of the following strings: “warning”, “error”, “info” tags
不包含 “warning”, “error”, “info” 这三个任意一个字符串的文档timestamp
field containing a date between today and one week ago timestamp
包含的日期在1周以前到现在的时间区间里筛选 response
满足 [400, 500] 的文档
POST kibana_sample_data_logs/_search
{"query": {"bool": {"filter": {"range": {"response": {"gte": 400,"lte": 500}}}}}
}
{"took" : 6,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 801,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
上一个query里加上referer
字段需要匹配 ""的筛选
POST kibana_sample_data_logs/_search
{"query": {"bool": {"filter": [{"range": {"response": {"gte": 400,"lte": 500}}},{"match": {"referer": ""}}]}}
}
{"took" : 7,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
筛选 referer
以 “” 开头
POST kibana_sample_data_logs/_search
{"query": {"bool": {"filter": {"prefix": {"referer": ""}}}}
}
{"took" : 13,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3584,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
筛选 request
以 “/people” 开头
POST kibana_sample_data_logs/_search
{"query": {"bool": {"filter": {"prefix": {"request.keyword": "/people"}}}}
}
{"took" : 4,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 452,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
筛选包含 memory
的文档
POST kibana_sample_data_logs/_search
{"query": {"bool": {"filter": {"exists": {"field": "memory"}}}}
}
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 552,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
筛选不包含 memory
的文档
POST kibana_sample_data_logs/_search
{"query": {"bool": {"must_not": {"exists": {"field": "memory"}}}}
}
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : 0.0,"hits" : []}
}
搜索 agent
包含 “Windows” 而且 url
字段包含 “name:john”
POST kibana_sample_data_logs/_search
{"query": {"bool": {"must": [{"match": {"agent": "Windows"}},{"match": {"url": "name:john"}}]}}
}
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 7.5268917,"hits" : []}
}
接上,但是过滤出 phpmemory
存在的文档
POST kibana_sample_data_logs/_search
{"query": {"bool": {"must": [{"match": {"agent": "Windows"}},{"match": {"url": "name:john"}}],"filter": {"exists": {"field": "phpmemory"}}}}
}
{"took" : 6,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 7.5268917,"hits" : []}
}
搜索 response
大于等于 400 或者 tags
包含 “error”
POST kibana_sample_data_logs/_search
{"query": {"bool": {"should": [{"range": {"response": {"gte": 400}}},{"match": {"tags": "error"}}]}}
}
{"took" : 10,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2003,"relation" : "eq"},"max_score" : 3.8313324,"hits" : []}
}
搜索 tags
不包含 “warning”, “error”, “info”
POST kibana_sample_data_logs/_search
{"query": {"bool": {"must_not": [{"match": {"tags": "warning"}},{"match": {"tags": "error"}},{"match": {"tags": "info"}}]}}
}
或者
POST kibana_sample_data_logs/_search
{"query": {"bool": {"must_not": [{"terms": {"tags": ["warning", "error", "info"]}}]}}
}
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2927,"relation" : "eq"},"max_score" : 0.0,"hits" : []}
}
筛选出 timestamp
包含的日期在1周以前到现在的时间区间里
POST kibana_sample_data_logs/_search
{"query": {"range": {"timestamp": {"gte": "now-7d/d","lte": "now/d"}}}
}
{"took" : 4,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1840,"relation" : "eq"},"max_score" : 1.0,"hits" : []}
}
bool
配合 filter
,must
,should
,must_not
等关键字进行多检索条件的逻辑计算召回,以及 prefix
,exists
,match
,range
,terms
等搜索关键字的使用 prefix
关键字只能作用在 keyword
字段上,所以上面的题解里会有 prefix: referer
和 prefix: request.keyword
的区别exists
而判断字段的值存不存在可能需要搭配 script 对字段值长度进行判断,或者通过 null_value
等方式POST kibana_sample_data_logs/_search
{"query": {"bool": {"must": {"script": {"script": {"source": "String message = doc['message.keyword'].value; return (null != message && 0 < message.length())"}}}}}
}
kibana_sample_data_flights
index kibana_sample_data_flights
索引上执行OriginCityName
or the DestCityName
fields matching the string “Sydney” OriginCityName
或者 DestCityName
字段里包含 “Sydney” 的文档Levenshtein Edit Distance
)设为2。测试一下当query是"Sydney", “Sidney” 和 “Sidnei” 时返回的结果条数一样。筛选OriginCityName
或 DestCityName
包含 “Sydney”
POST kibana_sample_data_flights/_search
{"query": {"bool": {"should": [{"match": {"OriginCityName": "Sydney"}},{"match": {"DestCityName": "Sydney"}}]}}
}
加模糊匹配,调整编辑距离
Sydney
POST kibana_sample_data_flights/_search
{"query": {"bool": {"should": [{"fuzzy": {"OriginCityName": {"value": "Sydney","fuzziness": "2"} }},{"fuzzy": {"DestCityName": {"value": "Sydney","fuzziness": "2"} }}]}}
}
{"took" : 200,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 405,"relation" : "eq"},"max_score" : 8.344088,"hits" : []}
}
Sidney
POST kibana_sample_data_flights/_search
{"query": {"bool": {"should": [{"fuzzy": {"OriginCityName": {"value": "Sidney","fuzziness": "2"} }},{"fuzzy": {"DestCityName": {"value": "Sidney","fuzziness": "2"} }}]}}
}
{"took" : 11,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 405,"relation" : "eq"},"max_score" : 6.9534063,"hits" : []}
}
Sidnei
POST kibana_sample_data_flights/_search
{"query": {"bool": {"should": [{"fuzzy": {"OriginCityName": {"value": "Sidnei","fuzziness": "2"} }},{"fuzzy": {"DestCityName": {"value": "Sidnei","fuzziness": "2"} }}]}}
}
{"took" : 5,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 405,"relation" : "eq"},"max_score" : 5.5627246,"hits" : []}
}
fuzzy
的使用 fuzzy
里ES会尝试对原query 进行一定的改写以尝试对可能的错误拼写进行模糊匹配(类似纠错功能)fuzzy
中可以对 编辑距离、改写长度等进行限制,从而平衡召回率和准确率本文发布于:2024-02-04 04:57:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170699561952255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |