Commit 4a7ef484 by mushishixian

添加参数优先级以及库存优先匹配参数

parent e6bfc3e1
...@@ -23,7 +23,7 @@ func init() { ...@@ -23,7 +23,7 @@ func init() {
//} //}
// //
////str := `{"bom_id":666,"delivery_type":1,"sort":1}` ////str := `{"bom_id":666,"delivery_type":1,"sort":1}`
//str := `{"bom_id":684,"delivery_type":1,"sort":1}` //str := `{"bom_id":717,"delivery_type":1,"sort":1}`
//rabbitmq.Send(queueExchange, str) //rabbitmq.Send(queueExchange, str)
} }
......
...@@ -56,11 +56,17 @@ func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, er ...@@ -56,11 +56,17 @@ func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, er
res, _ := hit.Source.MarshalJSON() res, _ := hit.Source.MarshalJSON()
if bomItems[key].GoodsName == "" || (bomItems[key].Attrs == "" && bomItems[key].GoodsName != "") { if bomItems[key].GoodsName == "" || (bomItems[key].Attrs == "" && bomItems[key].GoodsName != "") {
bomItems[key].GoodsName = gjson.Get(string(res), "goods_name").String() bomItems[key].GoodsName = gjson.Get(string(res), "goods_name").String()
bomItems[key].IsGoodsNameByAttrs = true
//还要打上是匹配到参数的标志
} }
} }
} }
} }
result = bomItems result = bomItems
//for _,item:=range bomItems {
// fmt.Println(item.GoodsName)
//}
return return
} }
...@@ -68,7 +74,7 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e ...@@ -68,7 +74,7 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e
//先去切割参数得到参数列表 //先去切割参数得到参数列表
var attrs []string var attrs []string
if bomItem.GoodsName != "" && bomItem.Attrs == "" { if bomItem.GoodsName != "" && bomItem.Attrs == "" {
attrs = splitAttrs(bomItem.GoodsName) attrs = splitAttrs(strings.Trim(bomItem.GoodsName, " "))
} else { } else {
attrs = splitAttrs(bomItem.Attrs) attrs = splitAttrs(bomItem.Attrs)
} }
...@@ -98,8 +104,7 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e ...@@ -98,8 +104,7 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e
attrsSlice = append(attrsSlice, attr) attrsSlice = append(attrsSlice, attr)
} }
} }
//fmt.Println(attrsSlice) query, shouldQueryNumber, mustQueryNumber := getQuery(attrsSlice)
query := getQuery(attrsSlice)
//单独针对封装进行转换 //单独针对封装进行转换
if bomItem.Encap != "" { if bomItem.Encap != "" {
//先提取出纯数字 //先提取出纯数字
...@@ -111,11 +116,13 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e ...@@ -111,11 +116,13 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e
subQuery := es.NewTermQuery("attrs.attr_value", attrValue) subQuery := es.NewTermQuery("attrs.attr_value", attrValue)
nestedQuery := es.NewNestedQuery("attrs", subQuery) nestedQuery := es.NewNestedQuery("attrs", subQuery)
query.Must(nestedQuery) query.Must(nestedQuery)
mustQueryNumber++
} }
} }
//如果ZyBrandId不为空,则代表匹配到了映射id //如果ZyBrandId不为空,则代表匹配到了映射id
if bomItem.ZyBrandId != "" && len(attrsSlice) > 0 { if bomItem.ZyBrandId != "" && len(attrsSlice) > 0 {
query.Should(es.NewTermQuery("brand_name", bomItem.ZyBrandName)) query.Should(es.NewTermQuery("brand_name", bomItem.ZyBrandName))
shouldQueryNumber++
} else { } else {
if bomItem.BrandName != "" && len(attrsSlice) > 0 { if bomItem.BrandName != "" && len(attrsSlice) > 0 {
brandName := bomItem.BrandName brandName := bomItem.BrandName
...@@ -130,11 +137,14 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e ...@@ -130,11 +137,14 @@ func searchAttr(bomItem model.BomItem, search *es.MultiSearchService) (result *e
r1, _ := regexp.Compile(`/[^A-Za-z0-9]+/`) r1, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
brandName = r1.ReplaceAllString(brandName, "") brandName = r1.ReplaceAllString(brandName, "")
query.Should(es.NewTermQuery("brand_name", brandName)) query.Should(es.NewTermQuery("brand_name", brandName))
shouldQueryNumber++
} }
} }
shouldMatchNumber := getShouldMatchNumber(shouldQueryNumber, mustQueryNumber)
query.MinimumNumberShouldMatch(shouldMatchNumber)
source := es.NewSearchSource().Query(query) source := es.NewSearchSource().Query(query)
source.Sort("_score", false) source.Sort("_score", false)
source.Sort("stock", false)
source.Sort("brand_sort", true) source.Sort("brand_sort", true)
source = source.From(0).Size(1) source = source.From(0).Size(1)
searchRequest := es.NewSearchRequest().Source(source).Preference("_primary") searchRequest := es.NewSearchRequest().Source(source).Preference("_primary")
...@@ -172,6 +182,9 @@ func extractAttr(attr string) []string { ...@@ -172,6 +182,9 @@ func extractAttr(attr string) []string {
regexpUnit := strings.Join(getNeedCheckUnit(), "|") regexpUnit := strings.Join(getNeedCheckUnit(), "|")
r, _ := regexp.Compile("(\\d+[\\/\\d. ]*|\\d)(" + regexpUnit + ")" + "") r, _ := regexp.Compile("(\\d+[\\/\\d. ]*|\\d)(" + regexpUnit + ")" + "")
res := r.FindAllString(attr, 100) res := r.FindAllString(attr, 100)
if len(res) == 0 {
return []string{attr}
}
return res return res
} }
...@@ -193,6 +206,11 @@ func changeKeyword(attr string) (result string) { ...@@ -193,6 +206,11 @@ func changeKeyword(attr string) (result string) {
if strings.Contains(attr, "Ω") { if strings.Contains(attr, "Ω") {
break break
} }
//先去判断这个属性值是不是属于某个封装或者温漂系数
attrName, _ := redis.String(gredis.HGet("sku_map2", attr))
if attrName != "" {
return attr
}
compile, _ := regexp.Compile(regexpStr) compile, _ := regexp.Compile(regexpStr)
attr = compile.ReplaceAllString(attr, regular) attr = compile.ReplaceAllString(attr, regular)
} }
...@@ -208,7 +226,7 @@ func TransformESParam(attr string) (result string) { ...@@ -208,7 +226,7 @@ func TransformESParam(attr string) (result string) {
//根据参数单位或者参数值获取对应的重要属性,组成最后查询的字符串 //根据参数单位或者参数值获取对应的重要属性,组成最后查询的字符串
//± //±
func getAttrValueByAttr(attr string) (attrValue string) { func getAttrValueByAttr(attr string) (attrValue string) {
//先找出单位,通过将数字替换成 //先找出单位,通过将数字替换成,第一个字符不是数字的,跳过
r, _ := regexp.Compile(mapping.GetAttrUnitRegular) r, _ := regexp.Compile(mapping.GetAttrUnitRegular)
attrUnit := strings.Trim(r.ReplaceAllString(attr, ""), " ") attrUnit := strings.Trim(r.ReplaceAllString(attr, ""), " ")
//如果单位是W,则要进行除法计算 //如果单位是W,则要进行除法计算
...@@ -256,20 +274,17 @@ func getAttrValueByAttr(attr string) (attrValue string) { ...@@ -256,20 +274,17 @@ func getAttrValueByAttr(attr string) (attrValue string) {
} }
//就算是提取出所谓的单位,也有可能是封装里面乱写的,比如 C0402R //就算是提取出所谓的单位,也有可能是封装里面乱写的,比如 C0402R
//所以还是要去判断是否是封装 //所以还是要去判断是否是封装
} else { }
//再去找没有单位的对应属性 //再去找没有单位的对应属性
attrName, _ = redis.String(gredis.HGet("sku_map2", attr)) attrName, _ = redis.String(gredis.HGet("sku_map2", attr))
if attrName != "" { if attrName != "" {
attrValue = attrName + "€" + attr attrValue = attrName + "€" + attr
} else {
attrValue = attr
}
} }
return attrValue return attrValue
} }
//获取匹配的条件 //获取匹配的条件
func getQuery(attrs []string) (query *es.BoolQuery) { func getQuery(attrs []string) (query *es.BoolQuery, shouldQueryNumber, mustQueryNumber int) {
//如果attrs为空,也要构建一个结果为空的查询,因为要使结果和bomItems数量对应起来 //如果attrs为空,也要构建一个结果为空的查询,因为要使结果和bomItems数量对应起来
if len(attrs) == 0 { if len(attrs) == 0 {
attrs = []string{"€_€"} attrs = []string{"€_€"}
...@@ -277,7 +292,6 @@ func getQuery(attrs []string) (query *es.BoolQuery) { ...@@ -277,7 +292,6 @@ func getQuery(attrs []string) (query *es.BoolQuery) {
var subQuery *es.TermQuery var subQuery *es.TermQuery
var nestedQuery *es.NestedQuery var nestedQuery *es.NestedQuery
query = es.NewBoolQuery() query = es.NewBoolQuery()
hasEncap := false
for _, attr := range attrs { for _, attr := range attrs {
//还要判断是不是一个单位对应多个属性的 //还要判断是不是一个单位对应多个属性的
if strings.Contains(attr, "|") { if strings.Contains(attr, "|") {
...@@ -287,36 +301,32 @@ func getQuery(attrs []string) (query *es.BoolQuery) { ...@@ -287,36 +301,32 @@ func getQuery(attrs []string) (query *es.BoolQuery) {
for _, temp := range multiAttr { for _, temp := range multiAttr {
subQuery = es.NewTermQuery("attrs.attr_value", temp+"€"+value) subQuery = es.NewTermQuery("attrs.attr_value", temp+"€"+value)
nestedQuery = es.NewNestedQuery("attrs", subQuery) nestedQuery = es.NewNestedQuery("attrs", subQuery)
//目前一个单位对应多个属性的都是非必要属性
query.Should(nestedQuery) query.Should(nestedQuery)
shouldQueryNumber++
} }
} else { } else {
subQuery = es.NewTermQuery("attrs.attr_value", attr) subQuery = es.NewTermQuery("attrs.attr_value", attr)
nestedQuery = es.NewNestedQuery("attrs", subQuery) nestedQuery = es.NewNestedQuery("attrs", subQuery)
//封装是必须要完全满足 //封装是必须要完全满足
if strings.Contains(attr, "封装") { //构建必须要满足条件的列表
hasEncap = true hasMustQuery := false
query.Must(nestedQuery) mustQueryUnitKeyWords := []string{"阻值(欧姆)", "容值", "电感", "封装"}
} else { splitWords := strings.Split(attr, "€")
if len(splitWords) > 0 {
if php2go.InArray(splitWords[0], mustQueryUnitKeyWords) {
hasMustQuery = true
query.Must(nestedQuery)
mustQueryNumber++
}
}
if !hasMustQuery {
shouldQueryNumber++
query.Should(nestedQuery) query.Should(nestedQuery)
} }
} }
} }
var shouldMatchNumber int return
shouldMatchNumber = 2
//判断在封装存在的条件下需要满足的筛选条数
attrsLen := len(attrs)
if hasEncap {
attrsLen = attrsLen - 1
}
if attrsLen > 4 {
shouldMatchNumber = 3
} else if attrsLen > 2 {
shouldMatchNumber = 2
} else {
shouldMatchNumber = 1
}
query.MinimumNumberShouldMatch(shouldMatchNumber)
return query
} }
func getNeedCheckUnit() (unitSlice []string) { func getNeedCheckUnit() (unitSlice []string) {
...@@ -326,3 +336,32 @@ func getNeedCheckUnit() (unitSlice []string) { ...@@ -326,3 +336,32 @@ func getNeedCheckUnit() (unitSlice []string) {
} }
return return
} }
func getShouldMatchNumber(shouldQueryNumber, mustQueryNumber int) (shouldMatchNumber int) {
//到底要符合多少个should,要根据shouldQueryNumber和mustQueryNumber来决定
//当必须条件有两个或者两个以上了(一般是封装+某个重要参数),should那边就可以放款限制,should可以为0
if mustQueryNumber >= 2 {
return
}
//必须条件只有一个的前提下
if mustQueryNumber == 1 {
if shouldQueryNumber >= 6 {
shouldMatchNumber = 3
} else if shouldQueryNumber >= 4 {
shouldMatchNumber = 2
} else {
shouldMatchNumber = 1
}
}
//不存在必须条件
if mustQueryNumber == 0 {
if shouldQueryNumber >= 5 {
shouldMatchNumber = 3
} else if shouldQueryNumber >= 3 {
shouldMatchNumber = 2
} else {
shouldMatchNumber = 1
}
}
return
}
...@@ -89,8 +89,15 @@ func UpdateBomItem(bomId, bomItemId int) (err error) { ...@@ -89,8 +89,15 @@ func UpdateBomItem(bomId, bomItemId int) (err error) {
bomItems, err = GetBrandMap(bomItems) bomItems, err = GetBrandMap(bomItems)
//匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充 //匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充
bomItems, err = MatchGoodsNameByAttrs(bomItems) bomItems, err = MatchGoodsNameByAttrs(bomItems)
//直接模糊匹配 //第一次去精确匹配,没有再去模糊匹配
goodsMapList, err := getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, true) var goodsMapList []GoodsMap
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, true)
if len(goodsMapList) == 0 {
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, false)
if err != nil {
return
}
}
//fmt.Println(goodsMapList) //fmt.Println(goodsMapList)
if err != nil { if err != nil {
return return
......
...@@ -239,8 +239,15 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor ...@@ -239,8 +239,15 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
} else if bom.GoodsName == "" && bom.Attrs != "" { } else if bom.GoodsName == "" && bom.Attrs != "" {
bom.GoodsName = bom.Attrs bom.GoodsName = bom.Attrs
} }
//模糊匹配之前要先去看这个bomItem的商品名称是否是由参数获取到的
//如果是的话,就不需要模糊匹配了,因为参数获取到的型号才是最符合参数的,如果再去模糊匹配,就不符合参数了
var paramsRawSearch bool
paramsRawSearch = rawSearch
if bom.IsGoodsNameByAttrs && rawSearch == false {
paramsRawSearch = true
}
//构建一个goods_name对应的bomItems列表 //构建一个goods_name对应的bomItems列表
searchRequest := getSearchParams(index, bom, sort, rawSearch) searchRequest := getSearchParams(index, bom, sort, paramsRawSearch)
searchFlag = true searchFlag = true
search.Add(searchRequest) search.Add(searchRequest)
} }
...@@ -317,8 +324,12 @@ func getTermQuery(index string, bomItem model.BomItem, sort int, flag bool) (que ...@@ -317,8 +324,12 @@ func getTermQuery(index string, bomItem model.BomItem, sort int, flag bool) (que
replace, _ := regexp.Compile("[^A-Za-z0-9]+") replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName := replace.ReplaceAllString(bomItem.GoodsName, "") goodsName := replace.ReplaceAllString(bomItem.GoodsName, "")
goodsName = strings.ToUpper(goodsName) goodsName = strings.ToUpper(goodsName)
//搜索商品名称 //商品名称太短的,限制搜索
query = query.Filter(es.NewTermQuery(field, goodsName)) if len(goodsName) <= 3 {
query = query.Filter(es.NewTermQuery(field, "€_€"))
} else {
query = query.Filter(es.NewTermQuery(field, goodsName))
}
//判断是否存在brandName并且匹配不到对应的标准品牌 //判断是否存在brandName并且匹配不到对应的标准品牌
if bomItem.BrandName != "" { if bomItem.BrandName != "" {
bomItem.BrandName = strings.ToUpper(bomItem.BrandName) bomItem.BrandName = strings.ToUpper(bomItem.BrandName)
......
...@@ -13,6 +13,7 @@ var KeywordRegular = map[string]string{ ...@@ -13,6 +13,7 @@ var KeywordRegular = map[string]string{
`(Pf|PF|pf)$`: "pF", `(Pf|PF|pf)$`: "pF",
`(Mf|MF|mf)$`: "mF", `(Mf|MF|mf)$`: "mF",
`(Uh|uh|μh|uH|UH)$`: "μH", `(Uh|uh|μh|uH|UH)$`: "μH",
`(Mh|mh|mH|MH)$`: "mH",
`(K)$`: "nF", `(K)$`: "nF",
`(V|v)`: "V", `(V|v)`: "V",
} }
...@@ -22,9 +23,10 @@ var GetAttrUnitRegular = `[\d.]|±|\+_|\+-|/|\(.*\)|\+/-|` ...@@ -22,9 +23,10 @@ var GetAttrUnitRegular = `[\d.]|±|\+_|\+-|/|\(.*\)|\+/-|`
//属性单位对应属性 //属性单位对应属性
var UnitAttrMapping = map[string]string{ var UnitAttrMapping = map[string]string{
"r": "阻值(欧姆)|直流电阻(内阻)", "r": "阻值(欧姆)|直流电阻(内阻)",
"Ω": "阻值(欧姆)|直流电阻(内阻)", "Ω": "阻值(欧姆)",
"OHM": "阻值(欧姆)|直流电阻(内阻)", "OHM": "阻值(欧姆)|直流电阻(内阻)",
"mh": "电感", "mh": "电感",
"H": "电感",
"F": "容值", "F": "容值",
"w": "功率", "w": "功率",
"W": "功率", "W": "功率",
......
...@@ -28,6 +28,8 @@ type BomItem struct { ...@@ -28,6 +28,8 @@ type BomItem struct {
LyBrandIds []string `json:"-"` LyBrandIds []string `json:"-"`
//映射后的品牌名称(自营品牌名称) //映射后的品牌名称(自营品牌名称)
ZyBrandName string `json:"-"` ZyBrandName string `json:"-"`
//是否是根据参数匹配到的商品名称
IsGoodsNameByAttrs bool `json:"-"`
Matching BomItemMatching `json:"matching"` Matching BomItemMatching `json:"matching"`
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment