【问题标题】:Elasticsearch multiple JSON insert bulkElasticsearch 多个 JSON 插入批量
【发布时间】:2021-05-12 04:21:43
【问题描述】:

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

【问题讨论】:

    标签: elasticsearch elasticsearch-bulk-api


    【解决方案1】:

    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 实用程序将为您生成此类有效负载。

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 2015-08-18
      • 2022-08-10
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多