使用任务 Webhook
本指南将教你如何使用 Webhook 在 Meilisearch 完成一个任务时通知某个 URL。
要求
- 命令行控制台
- 自托管的 Meilisearch 实例
- 配置为接收带有 ndjson 负载的
POST
请求的服务器
配置 Webhook URL
重启 Meilisearch 实例,并将 Webhook URL 提供给 --task-webhook-URL
meilisearch --task-webhook-url http://localhost:8000
您也可以使用环境变量或在配置文件中使用 MEILI_TASK_WEBHOOK_URL
来定义 Webhook URL。
可选:配置授权头
根据您的设置,您可能需要提供授权头。将其提供给 task-webhook-authorization-header
meilisearch --task-webhook-url http://localhost:8000 --task-webhook-authorization-header Bearer aSampleMasterKey
测试 Webhook
常见的异步操作是向索引中添加或更新文档。以下示例向我们的 books
索引添加测试文档
curl \
-X POST 'http://localhost:7700/indexes/books/documents' \
-H 'Content-Type: application/json' \
--data-binary '[
{
"id": 1,
"title": "Nuestra parte de noche",
"author": "Mariana Enríquez"
}
]'
当 Meilisearch 完成索引此文档时,它会向您使用 --task-webhook-url
配置的 URL 发送 POST
请求。请求主体将是一个或多个任务对象,以ndjson 格式
{"uid":4,"indexUid":"books","status":"succeeded","type":"documentAdditionOrUpdate","canceledBy":null,"details.receivedDocuments":1,"details.indexedDocuments":1,"duration":"PT0.001192S","enqueuedAt":"2022-08-04T12:28:15.159167Z","startedAt":"2022-08-04T12:28:15.161996Z","finishedAt":"2022-08-04T12:28:15.163188Z"}
如果 Meilisearch 已将多个任务批处理,它只会在一批中的所有任务都完成时触发 Webhook。在这种情况下,响应有效负载将包含所有任务,每个任务之间用换行符隔开。
{"uid":4,"indexUid":"books","status":"succeeded","type":"documentAdditionOrUpdate","canceledBy":null,"details.receivedDocuments":1,"details.indexedDocuments":1,"duration":"PT0.001192S","enqueuedAt":"2022-08-04T12:28:15.159167Z","startedAt":"2022-08-04T12:28:15.161996Z","finishedAt":"2022-08-04T12:28:15.163188Z"}
{"uid":5,"indexUid":"books","status":"succeeded","type":"documentAdditionOrUpdate","canceledBy":null,"details.receivedDocuments":1,"details.indexedDocuments":1,"duration":"PT0.001192S","enqueuedAt":"2022-08-04T12:28:15.159167Z","startedAt":"2022-08-04T12:28:15.161996Z","finishedAt":"2022-08-04T12:28:15.163188Z"}
{"uid":6,"indexUid":"books","status":"succeeded","type":"documentAdditionOrUpdate","canceledBy":null,"details.receivedDocuments":1,"details.indexedDocuments":1,"duration":"PT0.001192S","enqueuedAt":"2022-08-04T12:28:15.159167Z","startedAt":"2022-08-04T12:28:15.161996Z","finishedAt":"2022-08-04T12:28:15.163188Z"}