我试图在弹性搜索中插入多个JSON文档。我已经完成了一个文档,如下面的curl示例
curl --request POST
--url 'http://localhost:9200/articles/_doc/?pretty='
--header 'Content-Type: application/json'
--data '{
"topic":"python",
"title": "python tuples",
"description": "practical operations with python tuples",
"author": "test",
"date": "1-1-2019",
"views" : "100"
}'
当我试图插入一个批量JSON数组如下CURL
curl --request POST
--url 'http://localhost:9200/articles/_bulk/?pretty='
--header 'Content-Type: application/json'
--data '[{
"topic":"python",
"title": "python tuples",
"description": "practical operations with python tuples",
"author": "test",
"date": "1-1-2019",
"views" : "100"
},
{
"topic":"python",
"title": "python tuples",
"description": "practical operations with python tuples",
"author": "test2",
"date": "1-1-2019",
"views" : "100"
}]'
我得到以下错误
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected START_OBJECT but found [START_ARRAY]"
}
],
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected START_OBJECT but found [START_ARRAY]"
},
"status": 400
}
Bulk API需要application/x-ndjson
标头,因此有效负载是换行分隔的JSON。所以用这个代替:
curl -X POST "localhost:9200/articles/_bulk?pretty" -H 'Content-Type: application/x-ndjson' -d'
{ "index" : { } }
{"topic":"python","title":"python tuples","description":"practical operations with python tuples","author":"test","date":"1-1-2019","views":"100"}
{ "index" : { } }
{"topic":"python","title":"python tuples","description":"practical operations with python tuples","author":"test2","date":"1-1-2019","views":"100"}
'
顺便说一句,有一个nodejs cmd实用程序叫做json-to-es-bulk
,它会为你生成这样的有效负载。