From d52c24edf55b8beea8c0b159e2d4947deb9b1208 Mon Sep 17 00:00:00 2001 From: 97694731 <97694731@qq.com> Date: Fri, 8 May 2026 16:49:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E7=9B=AE=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=EF=BC=8C=E4=B8=89=E4=B8=AA=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=90=8C=E6=97=B6=E6=9B=B4=E6=96=B0-->=E5=8F=AA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BC=A0=E5=85=A5=E7=9A=84=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/book.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) 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{}{