To update the PageRank score for an existing document, we need to construct an update request payload that the go-elastic client will submit to the Elasticsearch cluster via an HTTP POST request. The update payload includes a map with the fields names and values that need to be updated.
To facilitate document updates, the go-elastic client exposes an Update method that expects the following set of arguments:
- The name of the index that contains the document to be updated
- The ID of the document to be updated
- The document update payload encoded as JSON
The following code snippet illustrates how the update request is assembled and passed to the Update method:
var buf bytes.Buffer update := map[string]interface{}{ "doc": map[string]interface{}{ "LinkID": linkID.String(), "PageRank": score, }, "doc_as_upsert": true, } if err := json.NewEncoder(&buf).Encode(update); err != nil { return xerrors.Errorf("update score: %w", err) }
If the caller of the UpdateScore method provides a document link ID that does not exist, we want to be able to create a placeholder document containing just the LinkID and PageRank scores. This is facilitated by including the doc_as_upsert flag to our update payload.