我有以下jq语句:
cat main.json | jq .hits.hits[] | jq -c '{"index":{"_index": ._index, "_type": ._type, "_id": ._id}}'
这给了我类似的东西
{"index":{"_index":"main","_type":"default","_id":"64"}}
{"index":{"_index":"main","_type":"default","_id":"65"}}
还有另一个jq语句:
cat main.json | jq .hits.hits[] | jq -c '._source'
这给了我类似的东西
{"mainContentOfPage":"hidden","informationType":"Product","url":"hidden","_id":"64"}
{"mainContentOfPage":"hidden","informationType":"Product","url":"hidden","_id":"65"}
现在,我想合并这些语句,以便在连续的行中获得它们的结果,如下所示:
{"index":{"_index":"main","_type":"default","_id":"64"}}
{"mainContentOfPage":"hidden","informationType":"Product","url":"hidden","_id":"64"}
{"index":{"_index":"main","_type":"default","_id":"65"}}
{"mainContentOfPage":"hidden","informationType":"Product","url":"hidden","_id":"65"}
这将允许我使用Elasticsearch's Bulk API将它们编入索引。
如何将这两个语句结合起来以得到想要的结果?
答案 0 :(得分:1)
使用逗号(此外,我还简化了键值选择器以使用速记语法):
jq -c '.hits.hits[] | { index: { _index, _type, _id } }, ._source' main.json