操作场景
Elasticsearch 是一个开源的分布式搜索和分析引擎,日志服务兼容其部分接口。当您从 Elasticsearch 迁移到日志服务时,如果您之前使用的一些 Elasticsearch 接口在日志服务的兼容范围内,您可以继续使用这些接口,从而降低迁移的复杂度。
日志服务的兼容接口基于 Elasticsearch 7.10版本。
访问方式
外网地址:https://${region}.cls.tencentcs.com/elasticsearch/
内网地址:https://${region}.cls.tencentyun.com/elasticsearch/
说明:
{"version": "2.0","statement": [{"effect": "allow","action": ["cls:SearchLog"],"resource": ["*"]}]}
兼容接口
Search API
GET /${topic_id}/_searchPOST /${topic_id}/_search
其中
${topic_id}
为日志主题 ID,相当于 Elasticsearch 中的 index。兼容 DSL
说明:
按照日志时间范围查询数据时,请使用
__TIMSTAMP__
字段代替 Elasticsearch 中的@timestamp
字段。查询 DSL
term 及 terms:指定的字段需开启键值索引。
match_phrase:指定的字段需开启键值索引。
range:指定的字段需开启键值索引。
聚合 DSL
terms:指定的字段需添加键值索引并开启统计。
通用参数
分页与排序:from、size、sort,用作 sort 的字段需添加键值索引并开启统计。
结果控制:track_total_hits。
示例
查询最近30分钟
reqMethod
为GET
的日志。curl -X POST "https://ap-chongqing.cls.tencentcs.com/elasticsearch/58b9496c-c6c6-48b3-xxx-50824857a13c/_search" \\-H "Content-Type: application/json" \\-u AKIDBFvNbcOwLWTXXXXXXXXXXXXXXXXXXXXX:bx5zf6JI3wb18XXXXXXXXXXXXXXXXXXXXX \\-d '{"track_total_hits": true,"size": 10,"query": {"bool": {"filter": [{ "match": { "reqMethod": "GET" } },{"range": {"__TIMESTAMP__": {"gte": "now-30m","lte": "now"}}}]}}}'
查询最近30分钟日志全文包含关键词
USER
的日志,并按照resHttpCode
分组统计。curl -X POST "https://ap-chongqing.cls.tencentcs.com/elasticsearch/58b9496c-c6c6-48b3-xxxx-50824857a13c/_search" \\-H "Content-Type: application/json" \\-u AKIDBFvNbcOwLWTXXXXXXXXXXXXXXXXXXXXX:bx5zf6JI3wb18XXXXXXXXXXXXXXXXXXXXX \\-d '{"query": {"bool": {"filter": [{"multi_match": {"type": "best_fields","query": "USER","lenient": true}},{"range": {"__TIMESTAMP__": {"gte": "now-30m","lte": "now"}}}]}},"aggs": {"http_code_distribution": {"terms": {"field": "resHttpCode","size": 100,"min_doc_count": 1}}}}'