【问题标题】:Elasticsearch bulk index api via rest endpoint通过休息端点的 Elasticsearch 批量索引 api
【发布时间】:2015-12-28 01:31:19
【问题描述】:

这是我的要求:

POST /_bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{"firstname":"first_name1","lastname":"last_name1"},
{"firstname":"first_name2","lastname":"last_name2"},
{"firstname":"first_name3","lastname":"last_name3"}}

这是错误:

{    "error": "IllegalArgumentException[Malformed action/metadata line [3], expected START_OBJECT or END_OBJECT but found

[VALUE_STRING]]", "状态": 500 }

基本上,每个文档都是 {"firstname": ___, "lastname": ____} 我不想将它们包装到父字段中。我从根本上缺少什么?

【问题讨论】:

标签: elasticsearch elasticsearch-plugin


【解决方案1】:

您只是缺少第二个和第三个文档的操作行,请尝试如下:

POST /_bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{"firstname":"first_name1","lastname":"last_name1"}
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{"firstname":"first_name2","lastname":"last_name2"}
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{"firstname":"first_name3","lastname":"last_name3"}

【讨论】:

  • Don't Repeat Yourself 部分提出了一种避免一直重复 _index_type 值的方法。但是,请注意,仍然必须为每个文档指定 action
  • 当然,如果可能的话,最好直接在 URL 中指定索引和类型。这只是为了说明 _bulk 操作
  • 是的。早些时候,我在阅读文档时产生了一个错误的印象,并认为仅在文件开头提供 action 可以很好地达到目的。我了解到这并非难事。
  • 有没有办法在同一个索引中上传多个 json 数据并自动增加 id ? @samyak-bhuta
  • 如何在上述批量请求中添加超时时间?
【解决方案2】:

正如 Samyak 在评论中所说,“不要重复自己”。这种语法更简洁。

post /test/_type/_bulk
{ "index": {}}
{"firstname":"first_name1","lastname":"last_name1"}
{ "index": { }}
{ "name": "Test2", "data": "This is my test data2" }

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2016-04-06
    • 2019-07-13
    相关资源
    最近更新 更多