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")

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

报告相同问题?