ElasticSearch上的批量请求返回消息“批量请求必须以换行符终止”

时间:2019-04-04 08:38:23

标签: json rest elasticsearch

我知道StackOverflow上已经存在类似的问题,但是没有一个可以解决我当前的问题。

我正在使用ElasticSearch 6.7.0,目前正在研究其教程。目前,我被困在this step上,这要求我download存储在文件accounts.json中的一些示例数据。这是该文件的一小段:

{"index":{"_id":"988"}}
{"account_number":988,"balance":17803,"firstname":"Lucy","lastname":"Castro","age":34,"gender":"F","address":"425 Fleet Walk","employer":"Geekfarm","email":"lucycastro@geekfarm.com","city":"Mulino","state":"VA"}
{"index":{"_id":"990"}}
{"account_number":990,"balance":44456,"firstname":"Kelly","lastname":"Steele","age":35,"gender":"M","address":"809 Hoyt Street","employer":"Eschoir","email":"kellysteele@eschoir.com","city":"Stewartville","state":"ID"}
{"index":{"_id":"995"}}
{"account_number":995,"balance":21153,"firstname":"Phelps","lastname":"Parrish","age":25,"gender":"M","address":"666 Miller Place","employer":"Pearlessa","email":"phelpsparrish@pearlessa.com","city":"Brecon","state":"ME"}

文件保存在路径C:\Users\Me\Desktop\accounts.json内。

我按照教程中的说明运行了以下命令

  

PS C:\ Users \ Me \ Downloads \ curl-7.64.1-win64-mingw \ bin>。\ curl.exe -H   “内容类型:应用主机:9200 / bank / _doc / _bulk?pretty&refresh”   --data-binary“ C:\ Users \ Me \ Desktop \ accounts.json”

我收到此错误消息:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "The bulk request must be terminated by a newline [\n]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "The bulk request must be terminated by a newline [\n]"
  },
  "status" : 400
}

要解决此问题,我转到了文件末尾,并按Enter插入了新行。但是,当我再次运行命令时,出现了相同的错误消息。

如何解决此问题?

3 个答案:

答案 0 :(得分:1)

我将accounts.json文件移到了我运行curl.exe的目录中,并执行了以下命令:

  

。\ curl.exe -H“内容类型:applicati   lhost:9200 / bank / _doc / _bulk?pretty&refresh“ --data-binary   “ @ accounts.json”

这次,批量请求成功。

对于遇到类似问题的用户,请记住按Enter在文件末尾插入新行。如果这样不起作用,请尝试将文件移动到另一个目录。错误消息“批量请求必须以换行符终止”可能会误导-您的文件可能根本没有格式错误;也许由于各种原因根本无法定位或访问它。

答案 1 :(得分:1)

使用“ @C:\ Users \ Me \ Desktop \ accounts.json”代替“ C:\ Users \ Me \ Desktop \ accounts.json”

GET /logs-of-year-2019.03.*/_search
{
  "size":0
  "sort": [
    {
       "@timestamp": "desc"
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "Request.advertiserId": [1695, 1252]
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-5h/d",
              "lte": "now/d"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "byAdvertiser": {
        "terms":{
            "field":"Request.advertiserId"
        },
        "aggs":{
            "by_top_hit":{"top_hits":{"size":10}}
        }
    }
  }
} 

答案 2 :(得分:0)

尝试更改文件的EOL字符和编码,如下所述: https://stackoverflow.com/a/59791215/2726844