Use UpdateRequest to insert/update a document

Currently, `IndexRequest` is used to insert/update a document, which
increases the version of the document every time IndexRequest.Do is
called.
This commit is contained in:
Haiyan Meng
2019-12-16 12:39:13 -08:00
parent 5598d35e4b
commit 272b7a6fcd

View File

@@ -180,12 +180,15 @@ func (idx *index) DeleteIndex() error {
// Insert or update the document by ID.
func (idx *index) Put(uniqueID string, doc interface{}) (string, error) {
body, err := json.Marshal(doc)
docBytes, err := json.Marshal(doc)
if err != nil {
return "", err
}
body := byteJoin(`{"doc":`, docBytes, `}`)
req := esapi.IndexRequest{
// Use `UpdateRequest` here instead of `IndexRequest`.
// For a document with a given id, every call of IndexRequest.Do will increase the version of a document.
req := esapi.UpdateRequest{
Index: idx.name,
Body: bytes.NewReader(body),
DocumentID: uniqueID,