Elasticsearch 长工单分类怎么做?用 Percolator 对照整篇与分段
2026-11-26 3471917
2026-11-26 0
用户关闭页面或应用先行超时时,先确认中止信号能传到 Elasticsearch 的 HTTP 连接。服务端仍在运行的普通搜索可以通过 GET /_tasks 定位,再按完整任务 ID 调用 POST /_tasks/{task_id}/_cancel。如果请求用的是 _async_search,保存响应返回的异步搜索 ID,再用删除接口处理。
普通搜索绑定发起它的 HTTP 连接。Elasticsearch 检测到这条连接关闭时会请求取消搜索。如果浏览器连接的是网关,网关收到下游取消后也要中止上游请求。检查 SDK 的超时实现和代理的断开传播,因为页面关闭、调用方停止等待和 Elasticsearch 看到连接关闭是几个独立的事件。
以下命令针对隔离的 Elasticsearch 8.19.0 单节点,使用 Bash、curl 7.76 及以上、jq 和 Python 3。先把 ES_URL 指向本地测试节点,在一个空的测试索引上执行下面的初始化。示例写入三分片、15 万条合成日志,目的是给短暂运行的聚合留出观察窗口。
: "${ES_URL:?先设置 ES_URL}"
export ES_URL
curl -fsS "$ES_URL/" | jq -r '"Elasticsearch " + .version.number'
curl -fsS -X PUT "$ES_URL/search-cancel-demo" -H 'Content-Type: application/json' -d '
{"settings":{"number_of_shards":3,"number_of_replicas":0},
"mappings":{"properties":{"service":{"type":"keyword"},"latency_ms":{"type":"long"},"event_id":{"type":"keyword"}}}}' | jq -e '.acknowledged'
python3 - <<'PY'
import json, os, urllib.request
url = os.environ["ES_URL"] + "/search-cancel-demo/_bulk"
for begin in range(0, 150000, 5000):
lines = []
for i in range(begin, begin + 5000):
lines += ['{"index":{}}', json.dumps({"service": "catalog", "latency_ms": (i * 137) % 9000, "event_id": f"event-{i:06d}"})]
req = urllib.request.Request(url, data=("\n".join(lines)+"\n").encode(),
headers={"Content-Type": "application/x-ndjson"})
with urllib.request.urlopen(req) as res:
assert not json.load(res)["errors"]
PY
curl -fsS -X POST "$ES_URL/search-cancel-demo/_refresh" > /dev/null
curl -fsS "$ES_URL/search-cancel-demo/_count" | jq -e '.count == 150000'在第一个终端执行查询。查询对同一批 catalog 日志按事件聚合,并为每桶计算平均耗时。保持该终端运行,在第二个终端用同一个 ES_URL 列任务。两个终端都要先设置环境变量。
: "${ES_URL:?先设置 ES_URL}"
curl --fail-with-body -sS -H 'Content-Type: application/json' \
-H 'X-Opaque-Id: catalog-demo' \
"$ES_URL/search-cancel-demo/_search?request_cache=false" -d '
{
"size": 0,
"query": {"term": {"service": "catalog"}},
"aggs": {
"slow_events": {
"terms": {"field": "event_id", "size": 1000},
"aggs": {"avg_ms": {"avg": {"field": "latency_ms"}}}
}
}
}': "${ES_URL:?先设置 ES_URL}"
curl -fsS "$ES_URL/_tasks?actions=indices:data/read/search*&detailed=true&group_by=none" |
jq '[.tasks[]? |
select(.action == "indices:data/read/search"
and .headers["X-Opaque-Id"] == "catalog-demo"
and .cancellable == true) |
{task_id: (.node + ":" + (.id | tostring)), description,
start_time_in_millis, cancelled}]'复制完整的 节点 ID:数字 ID 前,对照索引、发起时刻和查询描述。X-Opaque-Id 适合作为调用方的有限集合标签,同一调用方的请求可能同时命中它。出现多个候选时,结合应用日志或链路记录确认目标。
这里用精确的 action 选出测试查询的协调任务,避免混入带不同后缀的分片子任务。若任务列表为空,查询可能已经完成,检查终端输出、时机和 node_failures。在测试节点可以重新发起一次查询并快速列任务。线上查看任务通常需要 monitor 集群权限。
在第二个终端将经核对的 ID 赋给 TASK_ID,再执行下列命令。本文使用的 8.19 任务取消 API 要求 manage 集群权限。
: "${ES_URL:?先设置 ES_URL}"
: "${TASK_ID:?先手动填入已核对的 node_id:task_id}"
curl -fsS -X POST "$ES_URL/_tasks/$TASK_ID/_cancel?group_by=none" |
jq '{tasks: [.tasks[]? | {id, cancelled}],
node_failures: (.node_failures // []),
task_failures: (.task_failures // [])}'目标返回 cancelled: true 且两类失败为空时,说明该任务已收到取消请求。取消是协作式的,任务可以在安全停止前短暂留在列表里,因此需要用上一节的列表命令复查同一个 ID。如果取消返回任务不存在,查询可能恰好先跑完了,结合原请求响应和再次列出的任务判断。

异步搜索可以在提交响应返回后继续运行。提交时保存响应里的 id,用户放弃后调用 DELETE /_async_search/{id}。进行中的搜索会被取消,已完成的搜索会删除保存的结果。启用安全功能后,由原提交身份或拥有 cancel_task 集群权限的身份执行。
: "${ES_URL:?先设置 ES_URL}"
reply=$(curl -fsS -H 'Content-Type: application/json' \
"$ES_URL/search-cancel-demo/_async_search?wait_for_completion_timeout=0ms&keep_on_completion=true&keep_alive=1m" \
-d '{"size":0,"query":{"term":{"service":"catalog"}}}')
jq -e '{has_id: (.id != null), is_running, is_partial}' <<< "$reply"
async_id=$(jq -er '.id | select(length > 0) | @uri' <<< "$reply")
curl -fsS -X DELETE "$ES_URL/_async_search/$async_id" |
jq -e '.acknowledged'
status=$(curl -sS -o /dev/null -w '%{http_code}' "$ES_URL/_async_search/$async_id")
printf 'GET after DELETE: %s\n' "$status"
test "$status" = 404示例在同一索引上提交异步查询,wait_for_completion_timeout=0ms 让提交尽早返回,keep_on_completion=true 让快速完成时也能保留 ID。实测提交时 is_running 为 true,删除返回 acknowledged: true,之后按相同 ID 读取返回 404。生产代码还应分别处理首次读取时已经完成、过期和并发删除等情况。
用户取消能传到出站 HTTP 请求时,在应用里完成传播。已经运行的同步搜索可以由值班人员精确定位后终止,并用任务列表验证退出。需要轮询结果的业务保留异步搜索 ID,取消时直接删除对应搜索。