使用任务 Webhook

    本指南教你如何使用 Webhook 在 Meilisearch 完成任务时通知 URL。

    要求

    配置 Webhook URL

    重启 Meilisearch 实例,并将 Webhook URL 提供给 --task-webhook-URL

    meilisearch --task-webhook-url https://127.0.0.1:8000
    

    你也可以使用环境变量或配置文件中的 MEILI_TASK_WEBHOOK_URL 定义 Webhook URL。

    可选:配置授权标头

    根据你的设置,你可能需要提供授权标头。将其提供给 task-webhook-authorization-header

    meilisearch --task-webhook-url https://127.0.0.1:8000 --task-webhook-authorization-header Bearer aSampleMasterKey
    

    测试 Webhook

    一个常见的异步操作是向索引添加或更新文档。以下示例向我们的 books 索引添加一个测试文档

    curl \
      -X POST 'https://127.0.0.1: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"}