我是 golang 的新手,我正在努力在 Elasticsearch 中插入具有相同 ID 的新檔案。到現在為止,我可以在索引中正確插入檔案,但是如果檔案的 id 重復,時間戳會更新(因為是唯一改變的東西)。但是,我要做的不是更新檔案,換句話說,什么都不做。
在 Python 的情況下,為了實作這一點,我做了以下事情:
es = Elasticsearch(
[elastic_url],
http_auth=(elastic_user, elastic_password),
verify_certs=True,
)
return es.create(index=index_name, id=document_id, body=data, ignore=409)
在 golang 中,我使用以下代碼:
req := esapi.IndexRequest{
Index: "test_index",
DocumentID: "2",
Body: strings.NewReader(data),
Refresh: "false",
}
res, err := req.Do(ctx, es)
這將是很棒的一些幫助!先感謝您!
uj5u.com熱心網友回復:
有一個 API 可esapi用于檢查特定索引中是否存在檔案 ID。
ExistRequest - 通過檢查此請求的回應的狀態代碼,我們可以確認該檔案 ID 是否存在(如果存在則為 200,如果不存在則為 404)
// prepare existence checking request
req := esapi.ExistsRequest{
Index: index,
DocumentID: id,
}
// send existence checking request
// client here is *elastic.Client
ctx := context.Background()
resp, err := req.Do(ctx, client)
if err != nil {
// handle error
}
status := resp.StatusCode
if status == 200 {
fmt.Println("Exist")
} else if status == 404 {
fmt.Println("Not found")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473039.html
上一篇:Go泛型中什么時候不需要波浪號?
