MCPcopy
hub / github.com/zincsearch/zincsearch / Update

Function Update

pkg/handlers/document/update.go:41–76  ·  view source on GitHub ↗

@Id UpdateDocument @Summary Update document with id @security BasicAuth @Tags Document @Accept json @Produce json @Param index path string true "Index" @Param id path string true "ID" @Param document body map[string]interface{} true "Document" @Success 200 {object} meta.HTTP

(c *gin.Context)

Source from the content-addressed store, hash-verified

39// @Failure 500 {object} meta.HTTPResponseError
40// @Router /api/{index}/_update/{id} [post]
41func Update(c *gin.Context) {
42 indexName := c.Param("target")
43 docID := c.Param("id")
44 insert := c.Query("insert") // true or false
45 insertBool, _ := zutils.ToBool(insert)
46
47 var err error
48 var doc map[string]interface{}
49 if err = zutils.GinBindJSON(c, &doc); err != nil {
50 zutils.GinRenderJSON(c, http.StatusBadRequest, meta.HTTPResponseError{Error: err.Error()})
51 return
52 }
53
54 // If id field is present then use it, else create a new UUID and use it
55 if id, ok := doc["_id"]; ok {
56 docID = id.(string)
57 }
58 if docID == "" {
59 zutils.GinRenderJSON(c, http.StatusBadRequest, meta.HTTPResponseError{Error: "id is empty"})
60 return
61 }
62
63 // If the index does not exist, then create it
64 index, _, err := core.GetOrCreateIndex(indexName, "", 0)
65 if err != nil {
66 zutils.GinRenderJSON(c, http.StatusInternalServerError, meta.HTTPResponseError{Error: err.Error()})
67 return
68 }
69
70 err = index.UpdateDocument(docID, doc, insertBool)
71 if err != nil {
72 zutils.GinRenderJSON(c, http.StatusInternalServerError, meta.HTTPResponseError{Error: err.Error()})
73 return
74 }
75 zutils.GinRenderJSON(c, http.StatusOK, meta.HTTPResponseID{Message: "ok", ID: docID})
76}

Callers 1

TestUpdateFunction · 0.85

Calls 6

ToBoolFunction · 0.92
GinBindJSONFunction · 0.92
GinRenderJSONFunction · 0.92
GetOrCreateIndexFunction · 0.92
ErrorMethod · 0.80
UpdateDocumentMethod · 0.80

Tested by 1

TestUpdateFunction · 0.68