兼容统计字段为空
This commit is contained in:
parent
9b733a1923
commit
48b4d89510
@ -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,9 +1232,37 @@ 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])
|
||||
// 如果查询范围包含 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",
|
||||
@ -1243,6 +1270,13 @@ func (svc *BookService) buildNumericRangeConditions(builder *ESQueryBuilder, req
|
||||
LTE: maxVal,
|
||||
})
|
||||
}
|
||||
//builder.AddQuery(&QueryCondition{
|
||||
// Field: esField,
|
||||
// Type: "range",
|
||||
// GTE: minVal,
|
||||
// LTE: maxVal,
|
||||
//})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user