Elasticsearch Bulk API:无法发布多条记录

时间:2016-02-03 18:34:28

标签: elasticsearch elasticsearch-2.0

我正在尝试使用批量api发布以下内容。我有ES 2.2.0

{"index":{"_index":"junktest","_type":"test"}}
{"DocumentID":"555662","Tags":["B","C","D"],"Summary":"Summary Text","Status":"Review","Location":"HDFS","Error":"None","Author":"Abc Mnb","Sector":"Energy","Created Date":"2013-05-23"},
{"DocumentID":"555663","Tags":["A","B","C"],"Summary":"Summary Text","Status":"Review","Location":"HDFS","Error":"None","Author":"Abc Mnb","Sector":"Energy","Created Date":"2013-04-25"}

作为

curl -XPOST "http://localhost:9200/_bulk" --data-binary @post.json

但是我得到了

  {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Malformed
     action/metadata line [3], expected START_OBJECT or END_OBJECT but found [VALUE_
STRING]"}],"type":"illegal_argument_exception","reason":"Malformed action/metadata line [3], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]"},"status":400}

为什么},无效?我甚至没有使用逗号尝试过,但我仍然得到错误,即使我没有,

我的语法有什么问题?

修改

我能够通过

让它工作
{"index":{"_index":"junktest","_type":"test"}}
{"DocumentID":"555662","Tags":["B","C","D"],"Summary":"Summary Text","Status":"Review","Location":"HDFS","Error":"None","Author":"Abc Mnb","Sector":"Energy","Created Date":"2013-05-23"}
{"index":{"_index":"junktest","_type":"test"}}
{"DocumentID":"555663","Tags":["A","B","C"],"Summary":"Summary Text","Status":"Review","Location":"HDFS","Error":"None","Author":"Abc Mnb","Sector":"Energy","Created Date":"2013-04-25"}

这是使用批量api索引多个记录的唯一方法吗?

3 个答案:

答案 0 :(得分:3)

来自文档

  

REST API端点是/ _bulk,它需要以下JSON   结构:

INTERNET

action_and_meta_data\n optional_source\n action_and_meta_data\n optional_source\n .... action_and_meta_data\n optional_source\n 是可选的,但document source是强制性的,两者用新行分隔。 鉴于这些限制,您只能为每个操作指定一条记录。

同样在您提供的示例中,您没有在元数据中传递“_id”,这意味着“_id”是自动生成的。可能是故意的,但请记住,如果您打算更新文档,则无法使用action_meta_data

答案 1 :(得分:1)

最后一条记录之后的换行符对于让它正常工作非常重要。我通过在最后一条记录的末尾添加\ n(换行符)解决了类似的问题。

e.g。这不起作用:

" {" name":" Central School"," description":" CBSE Affiliation","街道":"哪敢"}"

但这将

" {" name":" Central School"," description":" CBSE Affiliation","街":" Nagan"} \ n "

答案 2 :(得分:0)

JSON结构中应该没有换行符,之前我正在使用此输入,它产生了上述错误,

{ 
"index" : { "_index" : "ecommerce", "_type" : "product", "_id" : "1002" 
} 
}
{ "id": 2}

现在它可以正常使用以下输入

{ "index" : { "_index" : "ecommerce", "_type" : "product", "_id" : "1002" } }
{ "id": 2}
{"index":{ "_index" : "ecommerce", "_type" : "product","_id":"1003"}}
{ "id": 3,"name":"Dot net"}