代码之家  ›  专栏  ›  技术社区  ›  Carasel

包含带cURL的新行的批量索引文本字段

  •  0
  • Carasel  · 技术社区  · 6 年前

    我正在尝试将以下格式的文件批量索引到我的elasticsearch索引中:

    {"index":{"_index":"articles","_type":"_doc"}}
    {"title":"My Article Title","text":"My article text. \nNext paragraph here."}
    

    使用此命令:

    curl -s -XPOST -H 'Content-Type: application/x-ndjson'  http://localhost:9200/_bulk --data-binary @/data.json
    

    问题是我的文档中的文章文本可能包含新行字符 \n ,这会破坏cURL批量索引的格式,因此我得到以下错误:

    {"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}
    

    0 回复  |  直到 6 年前
        1
  •  1
  •   dejanmarich    6 年前

    我已经设法做到了 Elasticsearch 7.3 Red Hat Enterprise Linux 7 (7.7) .

    1) 将.json改为.txt,在最后一行后按回车键,保存并上传到服务器上

    [root@host tmp]$ mv data.json data.txt

    [root@host tmp]$ echo '-w "\n"' >> ~/.curlrc
    

    [root@host tmp]$ curl -s -XPOST -H 'Content-Type: application/x-ndjson'  https://localhost:9200/_bulk -k -u user:pass --data-binary @data.json
    {"took":4,"errors":false,"items":[{"index":{"_index":"articles","_type":"_doc","_id":"QdsosG0B3nqkAGly3E6t","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":1,"_primary_term":1,"status":201}}]}
    

    4) 结果:

    [root@host tmp]$ curl -XGET -H 'Content-Type: application/x-ndjson'  https://localhost:9200/articles/_search?pretty -k -u user:pass
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "QdsosG0B3nqkAGly3E6t",
            "_score" : 1.0,
            "_source" : {
              "title" : "My Article Title",
              "text" : "My article text. \nNext paragraph here."
            }
          }
        ]
      }
    }