Elasticsearch 多个 JSON 插入批量

时间:2021-02-08 14:39:43

标签: elasticsearch elasticsearch-bulk-api

我正在尝试在 Elastic 搜索中插入多个 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
}

1 个答案:

答案 0 :(得分:3)

Bulk API 需要 application/x-ndjson 标头,因此有效负载是 newline-delimited 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"}
'

顺便说一句,有一个名为 json-to-es-bulk 的 nodejs cmd 实用程序可以为您生成此类负载。

相关问题