daShangDao_centerBook/es/es_config.go

172 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package es
// ESFieldConfig ES 字段配置
type ESFieldConfig struct {
// AllowUpdate 允许更新的字段列表
AllowUpdate map[string]bool
// AllowAdd 允许新增时填充的字段列表
AllowAdd map[string]bool
// FieldMappings 字段映射Go 结构体字段名 -> ES 字段名)
FieldMappings map[string]string
}
// GetESFieldConfig 获取 ES 字段配置
func GetESFieldConfig() *ESFieldConfig {
return &ESFieldConfig{
AllowUpdate: map[string]bool{
"book_name": true,
"book_pic": true,
"book_pic_s": true,
"book_pic_b": true,
"book_pic_w": true,
"book_def_pic": true,
"isbn": true,
"author": true,
"category": true,
"publisher": true,
"publication_time": true,
"binding_layout": true,
"fix_price": true,
"content": true,
"is_suit": true,
//"day_sale_7": true,
//"day_sale_15": true,
//"day_sale_30": true,
//"day_sale_60": true,
//"day_sale_90": true,
//"day_sale_180": true,
//"day_sale_365": true,
//"this_year_sale": true,
//"last_year_sale": true,
//"total_sale": true,
//"buy_counts": true,
//"sell_counts": true,
"is_illegal": true,
"is_return": true,
"is_filter": true,
"update_time": true,
"page_count": true,
"word_count": true,
//"book_format": true,
"cat_id": true,
"other": true, // 允许更新 other 字段
},
AllowAdd: map[string]bool{
"id": true,
"book_name": true,
"book_pic": true,
"book_pic_s": true,
"book_pic_b": true,
"book_pic_w": true,
"isbn": true,
"author": true,
"category": true,
"publisher": true,
"publication_time": true,
"binding_layout": true,
"fix_price": true,
"content": true,
"is_suit": true,
"day_sale_7": true,
"day_sale_15": true,
"day_sale_30": true,
"day_sale_60": true,
"day_sale_90": true,
"day_sale_180": true,
"day_sale_365": true,
"this_year_sale": true,
"last_year_sale": true,
"total_sale": true,
"buy_counts": true,
"sell_counts": true,
"book_pic_obj": true,
"book_pic_obj_s": true,
"update_time": true,
"is_illegal": true,
"is_return": true,
"is_filter": true,
"page_count": true,
"word_count": true,
"book_format": true,
"other": true, // 允许新增 other 字段
},
FieldMappings: map[string]string{
"ID": "id",
"BookName": "book_name",
"BookPic": "book_pic",
"BookPicS": "book_pic_s",
"BookPicB": "book_pic_b",
"BookPicW": "book_pic_w",
"ISBN": "isbn",
"Author": "author",
"Category": "category",
"Publisher": "publisher",
"PublicationTime": "publication_time",
"BindingLayout": "binding_layout",
"FixPrice": "fix_price",
"Content": "content",
"IsSuit": "is_suit",
"DaySale7": "day_sale_7",
"DaySale15": "day_sale_15",
"DaySale30": "day_sale_30",
"DaySale60": "day_sale_60",
"DaySale90": "day_sale_90",
"DaySale180": "day_sale_180",
"DaySale365": "day_sale_365",
"ThisYearSale": "this_year_sale",
"LastYearSale": "last_year_sale",
"TotalSale": "total_sale",
"BuyCounts": "buy_counts",
"SellCounts": "sell_counts",
"BookPicObj": "book_pic_obj",
"BookPicObjS": "book_pic_obj_s",
"UpdateTime": "update_time",
"IsIllegal": "is_illegal",
"IsReturn": "is_return",
"IsFilter": "is_filter",
"PageCount": "page_count",
"WordCount": "word_count",
"BookFormat": "book_format",
"Other": "other",
},
}
}
// IsAllowUpdate 检查字段是否允许更新
func (cfg *ESFieldConfig) IsAllowUpdate(field string) bool {
return cfg.AllowUpdate[field]
}
// IsAllowAdd 检查字段是否允许新增
func (cfg *ESFieldConfig) IsAllowAdd(field string) bool {
return cfg.AllowAdd[field]
}
// GetESFieldName 获取 ES 字段名
func (cfg *ESFieldConfig) GetESFieldName(goFieldName string) string {
if esName, ok := cfg.FieldMappings[goFieldName]; ok {
return esName
}
return goFieldName
}
// BuildESDocument 构建 ES 文档
func (cfg *ESFieldConfig) BuildESDocument(data map[string]interface{}) map[string]interface{} {
doc := make(map[string]interface{})
for goField, value := range data {
// 获取 ES 字段名
esField := cfg.GetESFieldName(goField)
// 检查是否允许添加
if !cfg.IsAllowAdd(esField) {
continue
}
doc[esField] = value
}
return doc
}