如何批量更改弹性搜索的JSON格式

时间:2017-10-17 16:28:39

标签: json elasticsearch jq elasticsearch-bulk-api

我有以下格式,并希望使用elasticsearch批量预处理。

{"title":"April","url":"https://simple.wikipedia.org/wiki/April", "abstract":"April is the 4th month of the year, and comes between March and May. It is one of four months to have 30 days.","sections":["The Month","April in poetry","Events in April","Fixed Events","Moveable Events","Selection of Historical Events","Trivia","References"]}
{"title":"August","url":"https://simple.wikipedia.org/wiki/August", "abstract":"August (Aug.) is the 8th month of the year in the Gregorian calendar, coming between July and September.","sections":["The Month","August observances","Fixed observances and events","Moveable and Monthlong events","Selection of Historical Events","Trivia","References"]}

我正在尝试添加索引,在每行之前键入行。

{"index":{"_index":"myindex","_type":"wiki","_id":"1"}}

在阅读之前的帖子时,我正在使用Kevin Marsh's post,如下所示:

cat file.json jq -c '.[] | {"index": {"_index": "myindex", "_type": "wiki", "_id": .id}}, .' 

我没有使用管道,因为我试图弄清楚之前的错误。我得到一个错误jq:没有这样的文件或目录。然后我使用了jq --version and get jq-1.5-1-a5b5cbe

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

你走了。这对我有用。如果这有帮助,请告诉我。

cat data.json | jq -c '. | {"index": {"_index": "json", "_type": "json"}}, .'  | curl -XPOST localhost:9200/_bulk --data-binary @-

详细了解jq : a lightweight and flexible command-line JSON processor

答案 1 :(得分:0)

我们发现有必要在curl标头中指定Content-Type;建议的解决方案应采用以下形式:

cat data.json | jq -c '. | {"index": {"_index": "json", "_type": "json"}}, .' | curl -H "Content-Type: application/json" -XPOST localhost:9200/_bulk --data-binary @-