使用 -POST 将文件的每一行导入 Elasticsearch

Importing every line of a file into Elasticsearch with -POST

当我 运行 这个命令时,它确实将 $line 发送到 Elasticsearch 而不是 file.txt

的行
cat file.txt | while IFS= read -r line; do curl -d '{ "field" : "$line" }' -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;

file.txt 包括:

This is line one.
This is line two.
This is line three.

这是 Centos 7 上的 Elasticsearch 7.2。

我尝试像 $line($line)"$line"
那样转义 $line 但它发布了 $line($line)"$line"

的实际值

期望放入 Elasticsearch 的数据:

{ "field" : "This is line one." }
{ "field" : "This is line two." }
{ "field" : "This is line three." }

实际数据放入 Elasticsearch
{ "field" : "$line" }

我发现它类似于这个问题: 并回答。谢谢@那个人和@chepner

cat file.txt | while IFS= read -r line; do jq -n --arg var "$line"  '.field = $var'| curl --data @- -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;