From 272b7a6fcd7fd06fe4a9da5bbcbca4e63df9d4b7 Mon Sep 17 00:00:00 2001 From: Haiyan Meng Date: Mon, 16 Dec 2019 12:39:13 -0800 Subject: [PATCH] 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. --- api/internal/crawl/index/elasticsearch.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/internal/crawl/index/elasticsearch.go b/api/internal/crawl/index/elasticsearch.go index 8893dc2e8..2696d8dfd 100644 --- a/api/internal/crawl/index/elasticsearch.go +++ b/api/internal/crawl/index/elasticsearch.go @@ -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,