Commit 4a7ef484 by mushishixian

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

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