首页 > 解决方案 > 是什么导致 Elasticsearch 批量加载失败?

问题描述

我不知道为什么我不能使用 JSON 批量加载 Elasticsearch。我以前做过,但这次我完全被难住了。

我已将一组 JSON 文档处理为弹性批量加载格式,并尝试批量加载我刚刚创建的索引(已验证创建,可以查询,并且为空)。

{"create": {"_id": "ef68e997-c616-4b0b-b08e-dfc09f8cb08f"}}
{"id": "ef68e997-c616-4b0b-b08e-dfc09f8cb08f", "title": "My document"}

... repeats for all records

我运行的命令使用 JSON 批量文件的路径列表和使用凭证将它们卷曲/发布到 Elastic 的循环:

while IFS= read -r "path" < "${DOC_LIST_PATH}"
do
    echo "Submitting Elastic formatted docs at ${path} to Elastic index 'docs' ..."
    
    curl \
    -X POST \
    -H "Content-Type: application/x-ndjson" \
    "https://${ES_USER}:${ES_PASSWD}@${ES_HOSTNAME}:${ES_PORT}/docs/_bulk" \
    --data-binary "@${path}" 
done

我以前做过这一切,它应该可以工作,但是......它没有。我收到了这个错误:

Submitting Elastic formatted docs at data/docs.json/part-00000.json to Elastic index 'docs' ...
Warning: Couldn't read data from file
Warning: "data/docs.json/part-00000.json",
Warning: this makes an empty POST.
{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}

... repeats for all files

我发现问题出在这个 bash 代码上,而不是数据或批量加载请求:

--data-binary "@${path}"

如果我用这个替换它,它会起作用:

--data-binary "@data/docs.json/part-00000.json"

为单个文件制作完整的工作命令:

curl -X POST -H "Content-Type: application/x-ndjson" "https://${ES_USER}:${ES_PASSWD}@${ES_HOSTNAME}:${ES_PORT}/docs/_bulk" --data-binary "@data/docs.json/part-00000.json"

但是我需要编写脚本,所以这仍然令人抓狂。请帮忙!

这个例子也在这里

标签: bashelasticsearchcurlsearchbulk-load

解决方案


推荐阅读