2016-06-09 92 views
4

如果Elastic Bulk API在一个或多个操作上失败,我找不到任何有关文档的文档。例如,对于以下请求,假设已经有一个ID为“3”的文档,因此“创建”应该失败 - 是否会失败所有其他操作NodeJs-ElasticSearch批量API错误处理

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } } 
{ "field1" : "value1" } 
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } } 
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } } 
{ "field1" : "value3" } 
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} } 
{ "doc" : {"field2" : "value2"} } 
  • 我使用的是弹性的NodeJS模块。

回答

4

一个动作没有失败不会影响其他动作。

从elasticsearch散装API的documentation

到批量操作的响应是一个大的JSON结构进行每个动作的 单个结果。 单一操作的失败不会影响其余的操作。

在从elasticsearch客户端的响应有status响应对应于各动作以确定它是否是一个失败或不

实施例:

client.bulk({ 
     body: [ 
     // action description 
     { index: { _index: 'test', _type: 'test', _id: 1 } }, 
     // the document to index 
     { title: 'foo' }, 
     // action description 
     { update: { _index: 'test', _type: 'test', _id: 332 } }, 
     // the document to update 
     { doc: { title: 'foo' } }, 
     // action description 
     { delete: { _index: 'test', _type: 'test', _id: 33 } }, 
     // no document needed for this delete 
     ] 
    }, function (err, resp) { 
     if(resp.errors) { 
      console.log(JSON.stringify(resp, null, '\t')); 
     } 
    }); 

响应:

{ 
     "took": 13, 
     "errors": true, 
     "items": [ 
       { 
         "index": { 
           "_index": "test", 
           "_type": "test", 
           "_id": "1", 
           "_version": 20, 
           "_shards": { 
             "total": 2, 
             "successful": 1, 
             "failed": 0 
           }, 
           "status": 200 
         } 
       }, 
       { 
         "update": { 
           "_index": "test", 
           "_type": "test", 
           "_id": "332", 
           "status": 404, 
           "error": { 
             "type": "document_missing_exception", 
             "reason": "[test][332]: document missing", 
             "shard": "-1", 
             "index": "test" 
           } 
         } 
       }, 
       { 
         "delete": { 
           "_index": "test", 
           "_type": "test", 
           "_id": "33", 
           "_version": 2, 
           "_shards": { 
             "total": 2, 
             "successful": 1, 
             "failed": 0 
           }, 
           "status": 404, 
           "found": false 
         } 
       } 
     ] 
} 
+0

当resp.errors ===真 - 我可以指望resp.items按照我发送的正文顺序排列批量请求? –

+1

是的,它会以相同的顺序 – keety