Commit 01ee5f70 by mushishixian

基础的参数匹配

parent 28618b14
...@@ -14,7 +14,6 @@ type RecvPro struct { ...@@ -14,7 +14,6 @@ type RecvPro struct {
} }
func init() { func init() {
<<<<<<< HEAD
queueExchange := rabbitmq.QueueExchange{ queueExchange := rabbitmq.QueueExchange{
"bom_match", "bom_match",
"bom_match", "bom_match",
...@@ -23,7 +22,7 @@ func init() { ...@@ -23,7 +22,7 @@ func init() {
"amqp://huntadmin:jy2y2900@192.168.1.237:5672/", "amqp://huntadmin:jy2y2900@192.168.1.237:5672/",
} }
str := `{"bom_id":369,"delivery_type":1,"sort":1}` str := `{"bom_id":382,"delivery_type":1,"sort":1}`
rabbitmq.Send(queueExchange, str) rabbitmq.Send(queueExchange, str)
} }
......
...@@ -19,31 +19,29 @@ import ( ...@@ -19,31 +19,29 @@ import (
//根据参数去匹配商品 //根据参数去匹配商品
func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, err error) { func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, err error) {
fmt.Println("开始匹配参数")
client, err := es.NewClient(es.SetURL(configs.ESSetting.Url)) client, err := es.NewClient(es.SetURL(configs.ESSetting.Url))
if err != nil { if err != nil {
return return
} }
defer client.Stop() defer client.Stop()
search := client.MultiSearch().Index("goods_map_test") search := client.MultiSearch().Index("goods_map_test")
searchFlag := false
//先根据参数去构建批量查询条件 //先根据参数去构建批量查询条件
for _, item := range bomItems { for _, item := range bomItems {
//如果有型号,但是型号有可能是参数,所以先去匹配下参数,有的话转成对应的型号
if item.GoodsName != "" {
search = searchAttr(item.GoodsName, search)
searchFlag = true
}
//如果没有型号,但是有参数,那就去匹配参数
if item.GoodsName == "" && item.Attrs != "" { if item.GoodsName == "" && item.Attrs != "" {
//先去切割参数得到参数列表 search = searchAttr(item.Attrs, search)
attrs := splitAttrs(item.Attrs) searchFlag = true
var attrsSlice []string
//去转换每一个参数,得到去查询es的标准格式
for _, attr := range attrs {
attr = TransformESParam(attr)
attrsSlice = append(attrsSlice, attr)
}
query := getQuery(attrsSlice)
source := es.NewSearchSource().Query(query)
source = source.From(0).Size(1)
searchRequest := es.NewSearchRequest().Source(source)
search.Add(searchRequest)
} }
} }
if !searchFlag {
return bomItems, nil
}
res, err := search.Do(context.Background()) res, err := search.Do(context.Background())
if err != nil { if err != nil {
return return
...@@ -58,12 +56,32 @@ func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, er ...@@ -58,12 +56,32 @@ func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, er
for _, hit := range responses.Hits.Hits { for _, hit := range responses.Hits.Hits {
res, _ := hit.Source.MarshalJSON() res, _ := hit.Source.MarshalJSON()
bomItems[key].GoodsName = gjson.Get(string(res), "goods_name").String() bomItems[key].GoodsName = gjson.Get(string(res), "goods_name").String()
fmt.Println(gjson.Get(string(res), "goods_name").String())
} }
} }
} }
result = bomItems
return return
} }
func searchAttr(attrOrigin string, search *es.MultiSearchService) (result *es.MultiSearchService) {
//先去切割参数得到参数列表
attrs := splitAttrs(attrOrigin)
var attrsSlice []string
fmt.Println(attrs)
//去转换每一个参数,得到去查询es的标准格式
for _, attr := range attrs {
attr = TransformESParam(attr)
attrsSlice = append(attrsSlice, attr)
}
query := getQuery(attrsSlice)
source := es.NewSearchSource().Query(query)
source = source.From(0).Size(1)
searchRequest := es.NewSearchRequest().Source(source)
fmt.Println(searchRequest.Body())
return search.Add(searchRequest)
}
//切割参数 //切割参数
func splitAttrs(attrs string) (result []string) { func splitAttrs(attrs string) (result []string) {
result = strings.Split(attrs, " ") result = strings.Split(attrs, " ")
...@@ -82,6 +100,10 @@ func splitAttrs(attrs string) (result []string) { ...@@ -82,6 +100,10 @@ func splitAttrs(attrs string) (result []string) {
if len(result) > 1 { if len(result) > 1 {
return return
} }
result = strings.Split(attrs, "/")
if len(result) > 1 {
return
}
return return
} }
...@@ -92,6 +114,7 @@ func changeKeyword(attr string) (result string) { ...@@ -92,6 +114,7 @@ func changeKeyword(attr string) (result string) {
compile, _ := regexp.Compile(regexpStr) compile, _ := regexp.Compile(regexpStr)
attr = compile.ReplaceAllString(attr, regular) attr = compile.ReplaceAllString(attr, regular)
} }
fmt.Println(attr)
return attr return attr
} }
...@@ -101,7 +124,6 @@ func TransformESParam(attr string) (result string) { ...@@ -101,7 +124,6 @@ func TransformESParam(attr string) (result string) {
unitMapping := mapping.UnitValueMapping unitMapping := mapping.UnitValueMapping
//获取该属性对应的单位 //获取该属性对应的单位
for unit, _ := range unitMapping { for unit, _ := range unitMapping {
//todo 先去截取属性,得到参数,比如3kΩ(3001)这种形式的参数,要截取成3kΩ
index := strings.Index(attr, unit) index := strings.Index(attr, unit)
if index > 0 { if index > 0 {
attr = string([]rune(attr)[:index+len(unit)-1]) attr = string([]rune(attr)[:index+len(unit)-1])
...@@ -117,8 +139,8 @@ func TransformESParam(attr string) (result string) { ...@@ -117,8 +139,8 @@ func TransformESParam(attr string) (result string) {
//± //±
func getAttrValueByAttr(attr string) (attrValue string) { func getAttrValueByAttr(attr string) (attrValue string) {
//先找出单位 //先找出单位
r, _ := regexp.Compile(`[\d.]|±|\+_|\+-|/|\(.*\)|`) r, _ := regexp.Compile(mapping.GetAttrUnitRegular)
attrUnit := r.ReplaceAllString(attr, "") attrUnit := strings.Trim(r.ReplaceAllString(attr, ""), " ")
//如果单位是W,则要进行除法计算 //如果单位是W,则要进行除法计算
if strings.ToUpper(attrUnit) == "W" { if strings.ToUpper(attrUnit) == "W" {
//提取值,进行计算 //提取值,进行计算
...@@ -135,7 +157,7 @@ func getAttrValueByAttr(attr string) (attrValue string) { ...@@ -135,7 +157,7 @@ func getAttrValueByAttr(attr string) (attrValue string) {
} }
//再找出纯数字 //再找出纯数字
var attrNumber float64 var attrNumber float64
numberR, _ := regexp.Compile(`(\d+(\.\d+)?)`) numberR, _ := regexp.Compile(mapping.PureNumberRegular)
pureNumber := numberR.FindString(attr) pureNumber := numberR.FindString(attr)
attrNumber, _ = strconv.ParseFloat(pureNumber, 64) attrNumber, _ = strconv.ParseFloat(pureNumber, 64)
//找出对应单位需要转换的值 //找出对应单位需要转换的值
...@@ -157,7 +179,6 @@ func getAttrValueByAttr(attr string) (attrValue string) { ...@@ -157,7 +179,6 @@ func getAttrValueByAttr(attr string) (attrValue string) {
if value, exist := mapping.UnitAttrMapping[baseUnit]; exist { if value, exist := mapping.UnitAttrMapping[baseUnit]; exist {
attrName = value attrName = value
} }
attrNumberStr := common.ToString(attrNumber * value64) attrNumberStr := common.ToString(attrNumber * value64)
attrValue = attrName + "€" + attrNumberStr attrValue = attrName + "€" + attrNumberStr
break break
...@@ -179,7 +200,16 @@ func getAttrValueByAttr(attr string) (attrValue string) { ...@@ -179,7 +200,16 @@ func getAttrValueByAttr(attr string) (attrValue string) {
func getQuery(attrs []string) (query *es.BoolQuery) { func getQuery(attrs []string) (query *es.BoolQuery) {
subQuery := es.NewBoolQuery() subQuery := es.NewBoolQuery()
for _, attr := range attrs { for _, attr := range attrs {
subQuery = subQuery.Should(es.NewTermQuery("attrs.attr_value", attr)) if strings.Contains(attr, "|") {
name := strings.Split(attr, "€")[0]
value := strings.Split(attr, "€")[1]
multiAttr := strings.Split(name, "|")
for _, temp := range multiAttr {
subQuery = subQuery.Should(es.NewTermQuery("attrs.attr_value", temp+"€"+value))
}
} else {
subQuery = subQuery.Should(es.NewTermQuery("attrs.attr_value", attr))
}
} }
nestedQuery := es.NewNestedQuery("attrs", subQuery) nestedQuery := es.NewNestedQuery("attrs", subQuery)
query = es.NewBoolQuery() query = es.NewBoolQuery()
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"math" "math"
"regexp" "regexp"
"strings" "strings"
...@@ -78,11 +77,16 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg ...@@ -78,11 +77,16 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg
} }
defer client.Stop() defer client.Stop()
//匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充 //匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充
MatchGoodsNameByAttrs(bomItems) //MatchGoodsNameByAttrs(bomItems)
//bomItems, err = MatchGoodsNameByAttrs(bomItems) bomItems, err = MatchGoodsNameByAttrs(bomItems)
//if err != nil { //for _, item := range bomItems {
// return err // if item.GoodsName != "" {
// fmt.Println(item.GoodsName)
// }
//} //}
if err != nil {
return err
}
//第一次先去精确匹配 //第一次先去精确匹配
goodsMapList, err := getUpdateGoodsData(bomId, bomItems, deliveryType, sort, client, true) goodsMapList, err := getUpdateGoodsData(bomId, bomItems, deliveryType, sort, client, true)
...@@ -227,7 +231,6 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor ...@@ -227,7 +231,6 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
if !rawSearch { if !rawSearch {
bom.GoodsName = common.SubKeyWordStr(bom.GoodsName) bom.GoodsName = common.SubKeyWordStr(bom.GoodsName)
if bom.GoodsName == "" { if bom.GoodsName == "" {
fmt.Println("continue")
continue continue
} }
} }
...@@ -326,12 +329,9 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es ...@@ -326,12 +329,9 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es
field := "auto_goods_name" field := "auto_goods_name"
query = query.Must(es.NewTermQuery(field, goodsName)) query = query.Must(es.NewTermQuery(field, goodsName))
} }
<<<<<<< HEAD
=======
//query = query.Filter(es.NewRangeQuery("single_price").Gt(0)) //query = query.Filter(es.NewRangeQuery("single_price").Gt(0))
//query = query.Filter(es.NewRangeQuery("stock").Gt(0)) //query = query.Filter(es.NewRangeQuery("stock").Gt(0))
>>>>>>> ysx-修复数据-20200609 //query = query.Filter(es.NewTermQuery("status", 1))
query = query.Filter(es.NewTermQuery("status", 1))
//query = query.Filter(es.NewRangeQuery("single_price").Gt(0)) //query = query.Filter(es.NewRangeQuery("single_price").Gt(0))
//src, err := query.Source() //src, err := query.Source()
//if err != nil { //if err != nil {
......
...@@ -3,9 +3,21 @@ package mapping ...@@ -3,9 +3,21 @@ package mapping
var KeywordRegular = map[string]string{ var KeywordRegular = map[string]string{
//`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$`: `$1$2Ω$4`, //`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$`: `$1$2Ω$4`,
//`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$`: `$1$2Μ$4$5`, //`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$`: `$1$2Μ$4$5`,
"COG": "C0G", "COG": "C0G",
"NPO": "NP0", "NPO": "NP0",
"(欧姆|欧|O|o|R|r)": "Ω", "华科|华新科技|华新科": "华新",
"华科|华新科技|华新科": "华新",
"U|u|μ": "M", `(欧姆|欧|O|o|R|r)`: "Ω",
`(Uf|uf|μf|uF|UF)`: "μF",
`(Uh|uh|μh|uH|UH)`: "μH",
`K`: "KΩ",
`v`: "V",
} }
var PureNumberRegular = `(\d+(\.\d+)?)`
var GetAttrUnitRegular = `[\d.]|±|\+_|\+-|/|\(.*\)|`
//`^[\d\.]+(欧姆|欧|O|o|R|r)$`: "Ω",
//`^[\d\.]+(Uf|uf|μf|uF|UF)$`: "μF",
//`^[\d\.]+(Uh|uh|μh|uH|UH)$`: "μH",
//`^[\d\.]+K$`: "KΩ",
\ No newline at end of file
...@@ -2,13 +2,17 @@ package mapping ...@@ -2,13 +2,17 @@ package mapping
//属性单位对应属性 //属性单位对应属性
var UnitAttrMapping = map[string]string{ var UnitAttrMapping = map[string]string{
"r": "阻值(欧姆)", "r": "阻值(欧姆)|直流电阻(内阻)",
"Ω": "阻值(欧姆)", "Ω": "阻值(欧姆)|直流电阻(内阻)",
"OHM": "阻值(欧姆)|直流电阻(内阻)",
"mh": "电感", "mh": "电感",
"F": "容值",
"w": "功率", "w": "功率",
"W": "功率", "W": "功率",
"Ω/r ": "内阻", "Ω/r ": "内阻",
"%": "精度", "%": "精度",
"V": "额定电压",
"A": "额定电流",
} }
//属性对应的基础属性 //属性对应的基础属性
...@@ -19,21 +23,28 @@ var UnitBaseMapping = map[string]string{ ...@@ -19,21 +23,28 @@ var UnitBaseMapping = map[string]string{
"kΩ": "Ω", "kΩ": "Ω",
"KΩ": "Ω", "KΩ": "Ω",
"MΩ": "Ω", "MΩ": "Ω",
"pF": "F", "pF": "F",
"nF": "F", "nF": "F",
"μF": "F", "μF": "F",
"μf": "F",
"mF": "F", "mF": "F",
"F": "F", "F": "F",
"μH": "H", "μH": "H",
"mH": "H", "mH": "H",
"H": "H", "H": "H",
"V": "V", "V": "V",
"kV": "V", "kV": "V",
"mA": "A", "mA": "A",
"A": "A", "A": "A",
"W": "W", "W": "W",
"kW": "W", "kW": "W",
"KW": "W", "KW": "W",
"%": "%", "%": "%",
} }
......
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