diff --git a/service/book.go b/service/book.go index 686cb95..ae975e6 100644 --- a/service/book.go +++ b/service/book.go @@ -527,13 +527,30 @@ func (svc *BookService) UpdateBookCatIdByISBNHandler(request *request.BookUpdate return nil, fmt.Errorf("没有有效的更新字段") } + // 构建更新脚本,只更新传入的字段,保留现有字段 + var scriptParts []string + params := make(map[string]interface{}) + + // 转换为 map 以便访问 + newCatId, ok := catIdValue.(map[string]interface{}) + if ok { + for key, value := range newCatId { + scriptParts = append(scriptParts, fmt.Sprintf("ctx._source.cat_id.%s = params.%s;", key, key)) + params[key] = value + } + } + + if len(scriptParts) == 0 { + return nil, fmt.Errorf("没有有效的 cat_id 字段") + } + + script := strings.Join(scriptParts, " ") + body := map[string]interface{}{ "script": map[string]interface{}{ - "source": "ctx._source.cat_id = params.cat_id;", + "source": script, "lang": "painless", - "params": map[string]interface{}{ - "cat_id": catIdValue, - }, + "params": params, }, "query": map[string]interface{}{ "term": map[string]interface{}{