2017-10-17 115 views
0

我有以下格式,希望能夠使用elasticsearch預批量預處理。如何批量更改彈性搜索的JSON格式

{"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}}, .' 

我不使用,因爲我想弄清楚,先that.I得到一個錯誤JQ錯誤的管道:無此文件或目錄。然後我用jq --version and get jq-1.5-1-a5b5cbe

任何幫助,非常感謝。

+0

不確定你需要什麼。 –

+0

感謝Hatim,希望能夠發現我的貓語句中是否存在問題,或者更好的解決方案,以便使用彈性搜索將格式更改爲準備使用批量api。 – ESLearner

+0

根據我的理解,您有一個json文件,並且您想使用批量API在Elasticsearch中對其進行索引。對? –

回答

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

+0

絕對會格式化。對於那個很抱歉。 Hatim以您輸入的格式使用以下命令: – ESLearner

+0

cat data.json | jq -c'。 | {「index」:{「_index」:「json」,「_type」:「json」}},。' | curl -XPOST localhost:9200/_bulk --data-binary @ - 我得到以下錯誤:jq:error:語法錯誤,意外$ end(unix shell引用問題)在,第1行 – ESLearner

+0

忘記curl部分。試試這個「cat data.json | jq -c'。| {」index「:{」_index「:」json「,」_type「:」json「}},。' 「並看看你得到了什麼輸出。 –