操作场景
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
兼容 DSL
说明:
按照日志时间范围查询数据时,请使用
__TIMESTAMP__ 字段代替 Elasticsearch 中的@timestamp 字段。查询 DSL
term 及 terms:指定的字段需开启键值索引。
match_phrase:指定的字段需开启键值索引。
range:指定的字段需开启键值索引。
聚合 DSL
terms:指定的字段需添加键值索引并开启统计。
通用参数
分页与排序:from、size、sort,用作 sort 的字段需添加键值索引并开启统计。
结果控制:track_total_hits。
示例
说明:
示例中,
__TIMESTAMP__为日志时间,为毫秒单位的 Unix 时间戳,使用 range 进行过滤,代表查询日志的时间范围。查询 2026-03-31 10:26:35 ~ 2026-03-31 10:41:35 时间范围中,
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": 1774923995000,"lte": 1774924895000}}}]}}}'
查询 2026-03-31 10:26:35 ~ 2026-03-31 10:41:35 时间范围中日志全文包含关键词
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": 1774923995000,"lte": 1774924895000}}}]}},"aggs": {"http_code_distribution": {"terms": {"field": "resHttpCode","size": 100,"min_doc_count": 1}}}}'