Elasticsearch 多个 JSON 批量插入

Elasticsearch multiple JSON insert bulk

我正在尝试在 Elastic 搜索中插入多个 JSON 文档。我已经完成了单个文档作为以下卷曲示例

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 requires the application/x-ndjson header and thus the payload to be a 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 实用程序可以为您生成此类有效负载。