diff --git a/service/book.go b/service/book.go index 91e2d16..b6824a5 100644 --- a/service/book.go +++ b/service/book.go @@ -301,7 +301,6 @@ func (svc *BookService) UpdateBookFieldsByISBN(request *request.BookUpdateReques }, } payload, _ := json.Marshal(body) - res, err := svc.esClient.Client.UpdateByQuery( []string{es.ESIndex}, svc.esClient.Client.UpdateByQuery.WithBody(bytes.NewReader(payload)), @@ -1233,15 +1232,50 @@ func (svc *BookService) buildNumericRangeConditions(builder *ESQueryBuilder, req } esField := esFields[fieldName] parts := strings.Split(value, ",") + fmt.Println("ppp", fieldName, parts) if len(parts) == 2 { minVal, _ := strconv.Atoi(parts[0]) maxVal, _ := strconv.Atoi(parts[1]) - builder.AddQuery(&QueryCondition{ - Field: esField, - Type: "range", - GTE: minVal, - LTE: maxVal, - }) + // 如果查询范围包含 0(0-999999),需要同时匹配 null 值 + if minVal == 0 { + // 使用 bool should 查询:匹配范围内值 OR 字段为 null/不存在 + rangeQuery := map[string]interface{}{ + "range": map[string]interface{}{ + esField: map[string]interface{}{ + "gte": minVal, + "lte": maxVal, + }, + }, + } + + nullQuery := map[string]interface{}{ + "bool": map[string]interface{}{ + "must_not": []map[string]interface{}{ + { + "exists": map[string]interface{}{ + "field": esField, + }, + }, + }, + }, + } + + builder.AddBoolQuery("should", []map[string]interface{}{rangeQuery, nullQuery}) + } else { + // 普通范围查询,不包含 null 值 + builder.AddQuery(&QueryCondition{ + Field: esField, + Type: "range", + GTE: minVal, + LTE: maxVal, + }) + } + //builder.AddQuery(&QueryCondition{ + // Field: esField, + // Type: "range", + // GTE: minVal, + // LTE: maxVal, + //}) } } }