douken1726 2018-07-30 08:48
浏览 152
已采纳

同步从Elasticsearch删除文档

I am using olivere/elastic to work with elasticsearch in Go. Here is my code:

// (1) delete document 
_, err := e.client.Delete().Index(index).Type("entity").
    Id(id).Do(e.ctx)

if err != nil {
    fmt.Println(err.Error())
}

// (2) get all documents 
result, err := e.client.Search().Index(index).From(1).Size(100).Do(e.ctx)
if err != nil {
    log.Println(err)
}

fmt.Println(result.TotalHits())

// (3) wait
time.Sleep(3 * time.Second)

// (4) get all documents
result, err = e.client.Search().Index(index).From(1).Size(100).Do(e.ctx)
if err != nil {
    log.Println(err)
}

fmt.Println(result.TotalHits())

Here I am:

  • delete document
  • get all documents from collection
  • wait 3 seconds
  • get the same documents from collection

Now on the step (2) I get one more document than step (4). Looks like the document is deleted in Elastic with some delay and I am searching the way to delete the document synchronously.

  • 写回答

1条回答 默认 最新

  • doushichi3678 2018-07-30 15:46
    关注
    _, err := e.client.Delete().Index(index).Type("entity").
        Id(id).Refresh("true").Do(e.ctx)
    

    Refresh("true")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!