Commit d2747670 by huangchengyi

Merge branch 'dev' of http://119.23.72.7/mushishixian/search_server into dev

parents 603ad1e3 ac67a6be
......@@ -65,6 +65,48 @@ MK=MK(米客方德)
[ZIYING_CONFIG]
Index=liexin_ziying
[SEARCH_KEYWORD_MAPPING]
`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$` = ${1}${2}Ω${4}
`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$` = ${1}${2}Μ${4}${5}
`COG` = C0G
`NPO` = NP0
`华科|华新科技|华新科` = 华新
[mathExactGoodsName]
0=M7
1=M1
2=M4
3=M2
4=A7
5=F7
6=X2
7=G4
8=X4
9=m7
10=m1
11=m4
12=m2
13=a7
14=f7
15=x2
16=g4
17=x4
;自营以某些单位进行搜索时,进行特殊处理
[ZIYING_MEASURE]
0=
1=uf
2=v
3=kg
4=MHz
5=pf
6=pin
7=ppm
8=kw
[official_website]
1 = https://www.futureelectronics.cn/search/?text=liexin
2 = http://www.powerandsignal.com/Products/Search?searchBox=liexin
......
......@@ -23,6 +23,7 @@ func QuoteIndex(c *gin.Context) {
if err := c.ShouldBind(&r); err != nil {
fmt.Println(err)
}
quoteService := service.QuoteService{}
//检查参数,有错误就返回错误
errCode, errMsg := requests.CheckQuoteRequest(r)
if errCode != 0 {
......@@ -32,7 +33,7 @@ func QuoteIndex(c *gin.Context) {
flag := 1
var res map[string]interface{}
if r.FollowHit != 0 && r.FollowHit != 1 {
res, _ = service.GetGoodsBySupplier(r)
res, _ = quoteService.GetGoodsBySupplier(r)
if len(res) > 0 {
flag = 0
}
......@@ -50,7 +51,7 @@ func QuoteIndex(c *gin.Context) {
//mouser同步请求搜索
res, _ = mouserOrg(r.Keyword, r.SupplierId)
} else {
res, _ = service.GetGoodsBySupplierName(r)
res, _ = quoteService.GetGoodsBySupplierName(r)
}
if len(res) > 0 {
flag = 0
......@@ -138,3 +139,10 @@ func GetDataPur(c *gin.Context) {
c.String(200, "ok")
return
}
//提供外链搜索接口
func GetData(keyword, supplierName string) (res interface{}) {
quoteService := service.QuoteService{}
res, _ = quoteService.GetData(keyword, supplierName)
return
}
......@@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"search_server/model"
"search_server/service"
"strings"
)
......@@ -14,7 +15,7 @@ func Zyh(ctx *gin.Context){
ctx.MultipartForm()
for requestName, requstValue := range ctx.Request.Form {
if requstValue[0] != "" {
zyhRequest[requestName] = requstValue[0]
zyhRequest[requestName] = strings.TrimSpace(requstValue[0])
}
}
......
package model
//采集返回的商品数据结构
type CollectData struct {
HasNext bool `json:"hasNext"`
Data []struct {
GoodsSn string `json:"goods_sn"`
GoodsName string `json:"goods_name"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
PdfURL string `json:"pdf_url"`
GoodsBrief string `json:"goods_brief"`
GoodsDesc string `json:"goods_desc"`
GoodsThumb string `json:"goods_thumb"`
GoodsImg string `json:"goods_img"`
URL string `json:"url"`
Attributes []string`json:"attributes"`
Stock int `json:"stock"`
Prices []struct {
Purchases int `json:"purchases"`
Price float64 `json:"price"`
} `json:"prices"`
MOQ int `json:"MOQ"`
Increment int `json:"increment"`
GoodsID string `json:"goods_id"`
From string `json:"from"`
} `json:"data"`
PageToken string `json:"pageToken"`
Total int `json:"total"`
DataType string `json:"dataType"`
AppCode string `json:"appCode"`
}
......@@ -148,3 +148,11 @@ func Rand(min, max int) int {
n, _ := crand.Int(crand.Reader, big.NewInt(int64(max+1)))
return int(n.Int64()) + min
}
func CopyMapString(distmap map[string]string) map[string]string{
tmpmap := make(map[string]string,0)
for k, v := range distmap {
tmpmap[k] = v
}
return tmpmap
}
\ No newline at end of file
......@@ -27,6 +27,11 @@ type QuoteIndexRequest struct {
K2 int `form:"k2"`
}
type QuoteGetDataRequest struct {
Keyword string `form:"keyword"`
SupplierName string `form:"supplier_name"`
}
func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
//解码参数
var err error
......@@ -46,3 +51,4 @@ func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
}
return
}
package service
import (
"encoding/json"
"fmt"
"github.com/imroc/req"
"github.com/syyongx/php2go"
. "github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/es"
"search_server/requests"
"search_server/service/query"
"search_server/service/transformer"
"strconv"
)
type QuoteService struct {
}
//根据供应商获取商品信息
func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
func (qs *QuoteService) GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
//索引字典
supplierIndex := query.GetSupplierIndex(r.SupplierId)
//获取查询条件
......@@ -32,7 +41,7 @@ func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interfa
return
}
func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
func (qs *QuoteService) GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
supplierIndex := r.SupplierName
queryString, err := query.GetGoodsBySupplierNameQuery(r)
fmt.Println(supplierIndex)
......@@ -49,3 +58,62 @@ func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]int
result["took"] = Get(esResult, "took").Int()
return
}
//获取采集那边的数据
func (qs *QuoteService) GetData(keyword, supplier string) (res interface{}, err error) {
collectData, err := qs.GetCollectData(keyword, supplier)
if err != nil {
return
}
quoteTransformer := transformer.QuoteTransformer{}
res = quoteTransformer.TransformCollectData(keyword, supplier, collectData)
return
}
//去采集那边的接口获取数据
func (qs *QuoteService) GetCollectData(keyword, supplierName string) (collectData model.CollectData, err error) {
portSet01 := []int{8084, 8084, 8084, 8084, 8085, 8086, 8086, 8086, 8086, 8087}
portSet02 := []int{8084, 8084, 8084, 8084, 8086, 8086, 8086, 8086}
portSet03 := []int{8086}
portSet04 := []int{8087}
supplierNamePortSetMap := map[string][]int{
"vnet": portSet03,
"arrow": portSet03,
"verical": portSet03,
"mouser": portSet02,
"rs": portSet02,
"rochester": portSet02,
"master": portSet04,
"aipco": portSet01,
"alliedelec": portSet01,
"buerklin": portSet01,
"digikey": portSet01,
"chip1stop": portSet01,
"element14": portSet01,
"tme": portSet01,
}
ip := "http://39.108.51.147"
key, exist := supplierNamePortSetMap[supplierName]
if !exist {
return
}
var index int
if len(key) == 1 {
index = 0
} else {
index = common.Rand(0, len(key)-1)
}
port := key[index]
portStr := strconv.Itoa(port)
keyword = php2go.URLEncode(keyword)
url := ip + ":" + portStr + "/elec/" + supplierName + "?kw=" + keyword
resp, err := req.Get(url)
if err != nil {
return
}
err = json.Unmarshal(resp.Bytes(), &collectData)
if err != nil {
return
}
return
}
package transformer
import (
"search_server/model"
"search_server/pkg/common"
)
type QuoteTransformer struct {
}
//针对GetUrl返回的数据进行数据转换
func (qt *QuoteTransformer) TransformCollectData(keyword, supplierName string, collectData model.CollectData) (transformedData map[string]interface{}) {
transformedData = make(map[string]interface{})
if len(collectData.Data) > 0 {
for _, data := range collectData.Data {
item := make(map[string]interface{})
if data.GoodsName == "" || data.GoodsSn == "" || data.ProviderName == "" {
continue
}
item["goods_name"] = data.GoodsName
item["goods_sn"] = data.GoodsSn
item["url"] = data.URL
goodsNameTemp := common.CaseInsensitiveReplace(data.GoodsName, keyword, "<b class='f-red'>"+keyword+"</b>")
item["goods_name_temp"] = goodsNameTemp
item["desc"] = data.GoodsDesc
item["displayName"] = supplierName
item["brand_name"] = data.ProviderName
item["docurl"] = data.PdfURL
item["goods_img"] = data.PdfURL
item["increment"] = data.Increment
item["single_price"] = 0
var tiered []model.LadderPrice
if len(data.Prices) > 0 {
for _, price := range data.Prices {
//获取正式格式的阶梯价
var ladderPrice model.LadderPrice
ladderPrice.Purchases = price.Purchases
ladderPrice.PriceUs = price.Price
tiered = append(tiered, ladderPrice)
item["single_price"] = price.Price
}
}
item["tiered"] = tiered
item["price_temp"] = data.Prices
item["stock"] = []int{data.MOQ, data.Stock}
transformedData[data.GoodsSn] = item
}
}
return
}
......@@ -6,8 +6,11 @@ import (
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
"github.com/syyongx/php2go"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/gredis"
"strconv"
......@@ -72,6 +75,7 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
var (
classid int64
err error
)
rsp = &model.ZyhResponse{}
zhyClassList := &model.ZhyClassList{}
......@@ -92,7 +96,7 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
if err != nil{
logger.Error("解析自营分类列表失败 s%,%s",err,classList)
}
//fmt.Println(classList)
classidstr := req["class_id/condition"]
classid,err = strconv.ParseInt(classidstr, 10, 64)
if err != nil{
......@@ -104,20 +108,23 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
if classid > 0 && item.ClassID1 == classid{
item.Flag = 1
classLight = append(classLight,item)
req["class_id1/condition"] = strconv.FormatInt(item.ClassID1,10)
break OuterLoop
}
for _,item2 := range item.ClassID2List{
if classid > 0 && item2.ClassID2 == classid{
item2.Flag = 1
req["class_id2/condition"] = strconv.FormatInt(item2.ClassID2,10)
classLight = append(classLight,item)
break OuterLoop
}
}
}
}
searchZiYingGoods(req,true,zhyClassList,classLight,_type)
req2 := make(map[string]string,0)
req2 = common.CopyMapString(req)
delete(req2,"class_id/condition")
searchZiYingGoods(req2,true,zhyClassList,classLight,_type)
return nil
}
......@@ -133,40 +140,197 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassList,classLight []*model.ZhyClassListModel,aType byte){
var (
goods_name_arr []map[string]string
keyword string
)
keyword = ""
goods_name_arr = make([]map[string]string,0)
if req["goods_name/condition"] != "" {
goodsNameCondition := strings.ToUpper(req["goods_name/condition"])
ziyingBrandReplace := config.Cfg.Section("ZIYING_BRAND_REPLACE").KeysHash()
if ziyingBrandReplace != nil && ziyingBrandReplace[goodsNameCondition] != "" {
goods_name_arr = append(goods_name_arr,map[string]string{"brand_name/condition":ziyingBrandReplace[goodsNameCondition]})
delete(req,"goods_name/condition")
}
}
if req["p"] == "" || req["p"] == "0"{
req["p"] = "1"
}
//index := config.Get("ZIYING_CONFIG")
goods_temp_status := 0
source := elastic.NewSearchSource()
source.StoredFields("goods_id","goods_name","sample_max_number","sample_status","sample_class_id")
query := elastic.NewBoolQuery()
if(len(req) > 0){
for k,v:=range req{
k_temp := make([]string,0)
if strings.Contains(k,"/") {
k_temp = strings.Split(k,"/")
if len(k_temp) == 2 {
isok := makeQuery(query,k_temp,&goods_temp_status,&v,&keyword)
if !isok {
continue;
}
}
}
}
}
source.Query(query)
res,_ := elastic.NewSearchRequest().Source(source).Body()
fmt.Println(res)
}
func makeQuery(query *elastic.BoolQuery,conditions []string,goods_temp_status *int,searchValue *string,keyword *string) bool{
var (
err error
stockValue int64
term_v []string
)
upper_field := []string{"goods_name","encap","packing","brand_name"}
int_field := []string{"status","class_id2","class_id1","single_price","stock","goods_id","goods_type","sort",
"brand_id","sample_class_id","sample_status","sample_max_number"}
upper_field=upper_field
int_field=int_field
upper_field = upper_field
int_field = int_field
searchKey := conditions[0]
searchCondition := conditions[1]
source := elastic.NewSearchSource()
source.StoredFields("goods_id","goods_name","sample_max_number","sample_status","sample_class_id")
query := elastic.NewBoolQuery()
fmt.Println(query)
for k,v:=range req{
k_temp := make([]string,0)
v = v
if strings.Contains(k,"/"){
k_temp = strings.Split(k,"/")
fmt.Println(k_temp)
}
redisConn :=gredis.Conn("search_r")
defer func(){
redisConn.Close()
}()
switch searchCondition{
case "condition":
if searchKey != "status" && searchCondition == ""{
return false;
}
switch searchKey {
case "status":
*goods_temp_status = 1
case "goods_name":
//fmt.Println(5569)
strNo := []string{"/" , "," ,"、" ,"?" , "," , ":",",","?",",","、",":"}
for _,strNoV:=range strNo{
*searchValue = php2go.StrReplace(string(strNoV)," ",*searchValue,-1)
}
//获取正式替换关键词
preg_arr := config.Cfg.Section("SEARCH_KEYWORD_MAPPING").KeysHash()
for pk,pv:=range preg_arr{
reg := regexp.MustCompile(pk)
*searchValue = reg.ReplaceAllString(*searchValue, pv)
}
//提取带有小数点的数字去掉.0小数
reg := regexp.MustCompile(`((\d+)\.(\d+))`)
all_ix := reg.FindAllSubmatch([]byte(*searchValue),-1)
for _,v := range all_ix{
if string(v[len(v)-1]) == "0"{
*searchValue = strings.Replace(*searchValue,".0","",-1)
break
}
}
fmt.Println(*searchValue)
keywordUper := strings.ToUpper(*searchValue)
mathExactGoodsName := config.Cfg.Section("mathExactGoodsName").KeysHash()
for _,v:=range mathExactGoodsName{
if keywordUper == v{
query.Must(elastic.NewTermQuery("auto_goods_name.raw", keywordUper))
*keyword = *searchValue
return false
}
}
skuMapStr,err := redis.String(redisConn.Do("Hget","sku_map",keywordUper))
if(err == nil || skuMapStr != ""){
filterNestedAttrsBoolQuery := elastic.NewBoolQuery()
skuMapSlice := strings.Split(skuMapStr,",")
for _,v := range skuMapSlice{
filterNestedAttrsBoolQuery.Should(elastic.NewMatchQuery("attrs.attr_value",v))
}
query.Filter(elastic.NewNestedQuery("attrs",filterNestedAttrsBoolQuery))
*keyword = skuMapStr
return false
}
//提取汉子
reg = regexp.MustCompile(`[\p{Han}]+`)
goods_name_cn := reg.FindAllString(*searchValue,-1)
if len(goods_name_cn) > 0 {
for k,v :=range goods_name_cn{
goods_name_cn[k] = strings.TrimSpace(v)
}
}
//去除汉字,并以空格分词条
reg = regexp.MustCompile(`[\P{Han}]+`)
term_v =reg.FindAllString(*searchValue, -1)
if len(term_v) > 0 {
for k,v :=range term_v{
term_v[k] = strings.TrimSpace(v)
}
}
goods_name_arr := append(term_v,goods_name_cn...)
fmt.Println("goods_name_arr",goods_name_arr)
*keyword = *searchValue
for _,v:=range term_v{
if v != ""{
query.Must(elastic.NewTermQuery("goods_name",v))
}
}
if len(goods_name_arr) > 0{
query.Must(elastic.NewMatchQuery("goods_name_cn",strings.Join(goods_name_cn," ")).MinimumShouldMatch("100%"))
}
case "brand_name":
term_v = strings.Split(*searchValue,",")
*searchValue = strings.ToUpper(strings.TrimSpace(*searchValue))
reg := regexp.MustCompile(`[[:^alnum:]]+`)
if len(term_v) > 0{
for k,v:=range term_v{
term_v[k] = reg.ReplaceAllString(v,"")
}
query.Must(elastic.NewTermsQuery("auto_brand_name2",term_v))
}
case "stock":
if stockValue,err = strconv.ParseInt(*searchValue, 10, 64);err == nil{
if stockValue > 1000000 {
stockValue = 1000000
}
query.Must(elastic.NewRangeQuery("stock").Gte(stockValue))
}
default:
*searchValue = strings.ToUpper(strings.TrimSpace(*searchValue))
term_v = strings.Split(*searchValue,",")
term_v_interface := make([]interface{},0)
for _,v:=range term_v{
term_v_interface = append(term_v_interface,v)
}
for _,field:=range int_field{
if searchKey == field{
for k,v:=range term_v{
if stockValue,err = strconv.ParseInt(v, 10, 64);err == nil{
term_v_interface[k] = stockValue
}
}
break
}
}
query.Must(elastic.NewTermsQuery(searchKey,term_v_interface))
}
case "order":
case "range":
case "sr":
case "nested":
case "eq":
}
}
return true
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/syyongx/php2go"
"search_server/boot"
"search_server/service"
"search_server/controller"
)
func StrRandom(lenNum int) string {
......@@ -29,5 +29,6 @@ func main() {
if err := boot.Boot(path); err != nil {
fmt.Println(err)
}
fmt.Println(controller.GetData("lm358","arrow"))
}
package main
import (
<<<<<<< HEAD
"bytes"
"fmt"
"strconv"
)
func intsToString(values []int) string {
......@@ -19,30 +19,23 @@ func intsToString(values []int) string {
return buf.String()
}
func main() {
fmt.Println(intsToString([]int{1, 2, 3})) // "[1, 2, 3]"
=======
"fmt"
"strconv"
)
func main() {
var a,b,c float64
a=1.69*100
b=1.7*10
c=a*b/(100*10)
//ȷ2.873
//��ȷ���2.873
fmt.Println(c)
//fmt.Println(fmt.Sprintf("%.2f",c))
//fmt.Println(FloatRound(c,2))
}
// ȡСλ
// ��ȡС��λ��
func FloatRound(f float64, n int) float64 {
format := "%." + strconv.Itoa(n) + "f"
//fmt.Println(format)
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
return res
>>>>>>> 326ae7ed67d5d2db331dddd1277d36b8b1992b08
}
\ No newline at end of file
package main
import (
"log"
"os"
)
func breadthFirst(f func(item string) []string, worklist []string) {
seen := make(map[string]bool)
for len(worklist) > 0 {
items := worklist
worklist = nil
for _, item := range items {
if !seen[item] {
seen[item] = true
worklist = append(worklist, f(item)...)
}
}
}
}
func crawl(url string) []string {
fmt.Println(url)
list, err := links.Extract(url)
if err != nil {
log.Print(err)
}
return list
}
func main() {
// Crawl the web breadth-first,
// starting from the command-line arguments.
breadthFirst(crawl, os.Args[1:])
}
\ No newline at end of file
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