Commit b937bb0f by huangchengyi

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

parents c0ebea20 b47b2d33
[supplier_all]
[SUPPLIER_ALL]
1 = future
2 = powerandsignal
3 = rochester
......@@ -21,9 +21,22 @@
21 = rs
1676 = buerklin
[SEARCH_SUPPLIER_MERGE]
17 = zhuanmai
1 = future
3 = rochester
6 = element14
7 = digikey
8 = chip1stop
9 = aipco
12 = alliedelec
13 = avnet
19 = peigenesis
20 = powell
21 = rs
#相似物料供应商
[alike_supplier]
1 = "ti"
[ALIKE_SUPPLIER]
1 = ti
[common]
MOUSER_API = 'http://footstone.liexin.net/webapi/handle_mouser'
......@@ -40,30 +53,30 @@ SUPPLIER_REDIS_PRE = SUPPLIER_REDIS_INFO_
[ZIYING_BRAND_REPLACE]
FH=FH(风华)
BL=BL(上海贝岭)
TE=TE(泰科)
FM=FM(富满)
CJ=CJ(长电)
ST=ST(意法)
SK=SK(时科)
ON=ON(安森美)
TM=TM(天微)
TP=TP(拓微)
TI=TI(德州仪器)
MD=MD(明达)
EG=EG(屹晶微)
HF=HF(宏发)
DY=DY(迪一)
MK=MK(米客方德)
国民=Nationz(国民技术)
中芯=SMIC(中芯国际)
富士=Fuji Electric(富士电机)
国星=Nationstar(国星光电)
贝岭=BL(上海贝岭)
FH = FH(风华)
BL = BL(上海贝岭)
TE = TE(泰科)
FM = FM(富满)
CJ = CJ(长电)
ST = ST(意法)
SK = SK(时科)
ON = ON(安森美)
TM = TM(天微)
TP = TP(拓微)
TI = TI(德州仪器)
MD = MD(明达)
EG = EG(屹晶微)
HF = HF(宏发)
DY = DY(迪一)
MK = MK(米客方德)
国民 = Nationz(国民技术)
中芯 = SMIC(中芯国际)
富士 = Fuji Electric(富士电机)
国星 = Nationstar(国星光电)
贝岭 = BL(上海贝岭)
[ZIYING_CONFIG]
Index=liexin_ziying
Index = liexin_ziying
[SEARCH_KEYWORD_MAPPING]
`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$` = ${1}${2}Ω${4}
......
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/requests"
"search_server/service"
"search_server/service/transformer"
"strings"
)
func GetDataIndex(c *gin.Context) {
var r requests.GetDataIndexRequest
_ = c.ShouldBind(&r)
if r.Keyword != "" {
word, _ := php2go.URLDecode(r.Keyword)
r.Keyword = strings.ToUpper(word)
}
if r.KNums != 0 {
r.Stock = r.KNums
}
//获取所有合并供应商列表
supplierList := config.GetSectionValues("SEARCH_SUPPLIER_MERGE")
supplierName := config.Get("SUPPLIER_ALL." + r.SupplierId).String()
if supplierName == "" && r.IsSort != 2 {
c.JSON(200, common.ErrorResponse(1, "供应商id有误"))
return
}
if r.SupplierId != "" && r.IsSort == 2 {
//所有供应商列表
//如果供应商不在合并供应商列表,则实时更新数据
if supplierName != "" && !php2go.InArray(r.SupplierName, supplierList) {
redisCon := gredis.Conn("search_w")
defer redisCon.Close()
//获取stockFlag
res, err := redis.Bool(redisCon.Do("SADD", "search_keyword_member", r.Keyword+"_"+r.SupplierName))
if err != nil {
fmt.Println(err)
}
if res {
_, err := redis.Bool(redisCon.Do("INCR", config.Get("redis_all.SEARCH_API_TOTAL_PRE").String()+r.SupplierName))
if err != nil {
fmt.Println(err)
}
}
r.FirstSearchPage = 2
quoteService := service.QuoteService{}
result, _ := quoteService.GetGoodsBySupplier(r.GetGoodsBySupplierRequest)
c.JSON(200, common.SuccessResponse(1, "", result))
return
}
}
//查询并输出数据
//获取除了专卖的供应商组装查询条件
queryStr := getDataQueryStr(r, supplierList)
esResult, err := es.CurlESMSearch(queryStr)
if err != nil {
return
}
goodsIds := service.GetGoodsIdsByEsMsearch(esResult)
goodsList, err := service.GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
result := service.GetReturnSupplierData(dullData)
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["took"] = gjson.Get(esResult, "took").Int()
c.JSON(200, common.SuccessResponse(0, "", result))
}
//获取查询es的条件语句
func getDataQueryStr(r requests.GetDataIndexRequest, supplierList []string) string {
getDataService := service.GetDataService{}
query, _ := getDataService.GetQuery(r.Keyword, r.SupplierName, r.BrandId, r.GoodsNameEq, r.Stock)
//获取专卖的组装查询条件
zhuanmaiQuery, _ := getDataService.GetQuery(r.Keyword, "zhuanmai", "", r.GoodsNameEq, r.Stock)
//类似物料搜索的组装查询条件
alikeQuery, _ := getDataService.GetQuery(r.Keyword, "alike", "", r.GoodsNameEq, r.Stock)
//类似物料供应商列表
alikeSuppliers := config.GetSectionValues("ALIKE_SUPPLIER")
//构建msearch搜索json
var queryStr string
for _, supplierName := range supplierList {
indexQueryStr := `{"index":"` + supplierName + `","type":"goods","preference":"_primary_first"}`
if php2go.InArray(supplierName, alikeSuppliers) {
queryStr = queryStr + indexQueryStr + "\n" + alikeQuery + "\n"
}
if supplierName == "zhuanmai" {
queryStr = queryStr + indexQueryStr + "\n" + zhuanmaiQuery + "\n"
} else {
queryStr = queryStr + indexQueryStr + "\n" + query + "\n"
}
}
return queryStr
}
......@@ -33,7 +33,7 @@ func QuoteIndex(c *gin.Context) {
flag := 1
var res map[string]interface{}
if r.FollowHit != 0 && r.FollowHit != 1 {
res, _ = quoteService.GetGoodsBySupplier(r)
res, _ = quoteService.GetGoodsBySupplier(r.GetGoodsBySupplierRequest)
if len(res) > 0 {
flag = 0
}
......@@ -41,7 +41,7 @@ func QuoteIndex(c *gin.Context) {
return
}
//获取供应商名称
supplierNameMap := config.Cfg.Section("supplier_all").KeysHash()
supplierNameMap := config.Cfg.Section("SUPPLIER_ALL").KeysHash()
supplierName, exist := supplierNameMap[r.SupplierId]
if !exist {
c.JSON(200, common.ErrorResponse(1, "供应商有误"))
......@@ -51,7 +51,7 @@ func QuoteIndex(c *gin.Context) {
//mouser同步请求搜索
res, _ = mouserOrg(r.Keyword, r.SupplierId)
} else {
res, _ = quoteService.GetGoodsBySupplierName(r)
res, _ = quoteService.GetGoodsBySupplierName(r.GetGoodsBySupplierRequest)
}
if len(res) > 0 {
flag = 0
......
......@@ -9,7 +9,7 @@ import (
func GetExcludeBrandIds(brandType int) (excludeBrandIds []int) {
isNoTi := config.Get("brand.IS_NOT_TI").String()
//如果关闭,直接返回空数据
if isNoTi == "1" {
if isNoTi == "0" {
return
}
//返回联营的brand_id切片
......
......@@ -92,6 +92,8 @@ type DullGoodsData struct {
Alike string `json:"alike"`
BrandUrl string `json:"brand_url"`
GoodsUrl string `json:"goods_url"`
CompanyName string `json:"-"`
ProviderName string `json:"-"`
}
type Attr struct {
......
......@@ -26,7 +26,7 @@ type PriceJson struct {
func getSupplier(supplierId string) {
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
supplierInfoStr, _ := redis.String(redisCon.Do("HGET", config.Get("supplier.SUPPLIER_REDIS_PRE")))
supplierInfoStr, _ := redis.String(redisCon.Do("HGET", config.Get("supplier.SUPPLIER_REDIS_PRE"),supplierId))
if supplierInfoStr != "" {
//序列化出来使用
} else {
......
......@@ -55,18 +55,31 @@ eg:
param := strings.Join(lines, "\n")+"\n"
*/
*/
func BulkES(param string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",")
esUrl := endpoints[rand.Intn(len(endpoints))] //随机获取一个节点进行请求
esUrl := endpoints[rand.Intn(len(endpoints))] //随机获取一个节点进行请求
//params := req.BodyJSON(param)
//req.Debug = true
header := make(http.Header)
header.Set("Content-Type", "application/x-ndjson")
resp, err := req.Post(esUrl+"/_bulk",header, param)
resp, err := req.Post(esUrl+"/_bulk", header, param)
if err != nil {
return
}
result = resp.String()
return
}
\ No newline at end of file
}
func CurlESMSearch(queryJson string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",")
//随机获取一个节点进行请求
req.Debug = true
esUrl := endpoints[rand.Intn(len(endpoints))]
params := req.BodyJSON(queryJson)
resp, err := req.Post(esUrl+"/_msearch", params)
if err != nil {
return
}
result = resp.String()
return
}
package requests
//使用根据供应商获取商品信息方法的通用请求
type GetGoodsBySupplierRequest struct {
Keyword string `form:"keyword"`
SupplierName string `form:"supplier_name"`
Stock int `form:"stock"`
SupplierId string `form:"supplier_id"`
Offset int `form:"offset"`
FollowHit int `form:"follow_hit"`
CheckButton int `form:"check_button"`
GoodsNameEq string `form:"goods_name/eq"`
IsExact int `form:"is_exact"`
BrandId string `form:"brand_id"`
AvailRank int `form:"avail_rank"`
SingleRank int `form:"single_rank"`
StockRank int `form:"stock_rank"`
ComRank int `form:"com_rank"`
FirstSearchPage int `form:"first_search_page"`
P int `form:"p"`
Topic int `form:"topic"`
K1 int `form:"k1"`
K2 int `form:"k2"`
}
package requests
type GetDataIndexRequest struct {
//Keyword string `form:"keyword"`
KNums int `form:"kNums"`
IsSort int `form:"is_sort"`
//SupplierId string `form:"supplier_id"`
//SupplierName string `form:"supplier_name"`
//GoodsNameEq string `form:"goods_name/eq"`
//BrandId string `form:"brand_id"`
//Offset int `form:"offset"`
//FollowHit int `form:"follow_hit"`
//Stock int `form:"stock"`
//IsExact int `form:"is_exact"`
//AvailRank int `form:"avail_rank"`
//SingleRank int `form:"single_rank"`
//StockRank int `form:"stock_rank"`
//ComRank int `form:"com_rank"`
//FirstSearchPage int `form:"first_search_page"`
//P int `form:"p"`
//Topic int `form:"topic"`
//K1 int `form:"k1"`
//K2 int `form:"k2"`
//Keyword string `form:"keyword"`
//SupplierId string `form:"supplier_id"`
//SupplierName string `form:"supplier_name"`
//Stock int `form:"stock"`
GetGoodsBySupplierRequest
}
......@@ -6,30 +6,12 @@ import (
)
type QuoteIndexRequest struct {
Keyword string `form:"keyword"`
CheckButton int `form:"check_button"`
SupplierId string `form:"supplier_id"`
SupplierName string `form:"supplier_name"`
Offset int `form:"offset"`
FollowHit int `form:"follow_hit"`
Stock int `form:"stock"`
GoodsNameEq string `form:"goods_name/eq"`
IsExact int `form:"is_exact"`
BrandId string `form:"brand_id"`
AvailRank int `form:"avail_rank"`
SingleRank int `form:"single_rank"`
StockRank int `form:"stock_rank"`
ComRank int `form:"com_rank"`
FirstSearchPage int `form:"first_search_page"`
P int `form:"p"`
Topic int `form:"topic"`
K1 int `form:"k1"`
K2 int `form:"k2"`
GetGoodsBySupplierRequest
}
type QuoteGetDataRequest struct {
Keyword string `form:"keyword"`
SupplierName string `form:"supplier_name"`
Keyword string `form:"keyword"`
SupplierName string `form:"supplier_name"`
}
func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
......@@ -46,9 +28,8 @@ func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
return 1, "缺少参数"
}
//判断供应商
if config.Get("supplier_all."+r.SupplierId).String() == "" {
if config.Get("SUPPLIER_ALL."+r.SupplierId).String() == "" {
return 1, "供应商id有误"
}
return
}
......@@ -27,5 +27,9 @@ func InitRouter() *gin.Engine {
r.GET("/search/quote/getDataPur", controller.GetDataPur)
r.POST("/search/quote/getDataPur", controller.GetDataPur)
//GetData控制器相关
r.GET("/search/get_data/index", controller.GetDataIndex)
r.POST("/search/get_data/index", controller.GetDataIndex)
return r
}
package service
import (
"github.com/gomodule/redigo/redis"
"github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/gredis"
"strings"
)
type GetDataService struct {
}
func (gs *GetDataService) GetQuery(originKeyword, supplierName, brandId, goodsNameEq string, stock int) (string, error) {
query := elastic.NewBoolQuery()
rex, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
keyword := rex.ReplaceAllString(originKeyword, "")
keyword = strings.ToUpper(keyword)
//只显示更新时间在一个月内的数据
oneMonthTimeStamp := carbon.Now().SubMonth().Timestamp()
query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp))
//类似物料搜索
if supplierName == "alike" && keyword != "" {
subQuery := elastic.NewBoolQuery()
subQuery.Should(elastic.NewTermQuery("goods_name_alike", strings.ToUpper(originKeyword)))
subQuery.Should(elastic.NewTermQuery("goods_name", keyword))
} else if supplierName != "zhuanmai" && keyword != "" {
//增加联营精确搜索
if goodsNameEq != "" {
query.Must(elastic.NewTermQuery("auto_goods_name.raw", keyword))
} else {
query.Must(elastic.NewTermQuery("goods_name", keyword))
}
} else {
if keyword != "" {
subQuery := elastic.NewBoolQuery()
subQuery.Should(elastic.NewTermQuery("goods_name", keyword))
}
}
//根据品牌id查找
if brandId != "" {
brandIdSlice := strings.Split(brandId, ",")
var brandIdsParams []interface{}
for _, brandId := range brandIdSlice {
brandIdsParams = append(brandIdsParams, strings.TrimSpace(brandId))
}
query.Must(elastic.NewTermsQuery("brand_id", brandIdsParams...))
}
//去除status=10
query.MustNot(elastic.NewTermQuery("status", 10))
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
//获取stockFlag
stockFlag, _ := redis.Int(redisCon.Do("GET", "StockSwitch"))
//判断库存
if stock > 0 {
limitNumber := 1000000
if stock > 1000000 {
stock = limitNumber
}
query.Must(elastic.NewRangeQuery("stock").Gte(stock))
} else if stockFlag == 1 && supplierName == "zhuanmai" {
query.MustNot(elastic.NewRangeQuery("stock").Gt(0))
}
//是否排除某些品牌数据
excludeBrandIds := model.GetExcludeBrandIds(1)
if len(excludeBrandIds) > 0 {
for _, brandId := range excludeBrandIds {
query.MustNot(elastic.NewTermQuery("brand_id", brandId))
}
}
source := elastic.NewSearchSource()
//source.StoredFields("goods_id", "old_goods_id", "status")
//综合排序
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("_score", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.From(0)
source.Size(2)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
return searchRequest.Body()
}
package service
import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/imroc/req"
"github.com/tidwall/gjson"
......@@ -230,5 +231,18 @@ func GetGoodsIdsByEs(res string) (goodsIds []string) {
for _, item := range gjArray {
goodsIds = append(goodsIds, item.String())
}
fmt.Println(goodsIds)
return
}
func GetGoodsIdsByEsMsearch(res string) (goodsIds []string) {
//直接用gjson去获取goods_id列表
gjArray := gjson.Get(res, "responses.hits.hits.#._source.goods_id").Array()
for _, item := range gjArray {
goodsIds = append(goodsIds, item.String())
}
fmt.Println(goodsIds)
return
}
......@@ -14,7 +14,7 @@ import (
)
func GetSupplierIndex(supplierId string) (supplierIndex string) {
supplierIndexMap := config.Cfg.Section("supplier_all").KeysHash()
supplierIndexMap := config.Cfg.Section("SUPPLIER_ALL").KeysHash()
if index, exist := supplierIndexMap[supplierId]; exist {
return index
}
......@@ -33,7 +33,7 @@ func GetSupplierIndex(supplierId string) (supplierIndex string) {
return
}
func GetGoodsBySupplierNameQuery(r requests.QuoteIndexRequest) (string, error) {
func GetGoodsBySupplierNameQuery(r requests.GetGoodsBySupplierRequest) (string, error) {
query := elastic.NewBoolQuery()
subQuery := elastic.NewBoolQuery()
subQuery2 := elastic.NewBoolQuery()
......@@ -172,7 +172,7 @@ func GetGoodsBySupplierNameQuery(r requests.QuoteIndexRequest) (string, error) {
return searchRequest.Body()
}
func GetGoodsBySupplierIdQuery(r requests.QuoteIndexRequest) (string, error) {
func GetGoodsBySupplierIdQuery(r requests.GetGoodsBySupplierRequest) (string, error) {
r.SupplierId = strings.TrimSpace(r.SupplierId)
source := elastic.NewSearchSource()
query := elastic.NewBoolQuery()
......@@ -214,7 +214,7 @@ func GetGoodsBySupplierIdQuery(r requests.QuoteIndexRequest) (string, error) {
//判断是否存在类似物料搜索
supplierIndexName := GetSupplierIndex(r.SupplierId)
//获取相似物料供应商列表
alikeSuppliers := config.Cfg.Section("alike_supplier").KeyStrings()
alikeSuppliers := config.Cfg.Section("ALIKE_SUPPLIER").KeyStrings()
if php2go.InArray(supplierIndexName, alikeSuppliers) && keyword != "" {
query.Should(elastic.NewTermQuery("goods_name_alike", keyword))
}
......
......@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/imroc/req"
"github.com/syyongx/php2go"
. "github.com/tidwall/gjson"
"github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/es"
......@@ -19,7 +19,7 @@ type QuoteService struct {
}
//根据供应商获取商品信息
func (qs *QuoteService) GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
func (qs *QuoteService) GetGoodsBySupplier(r requests.GetGoodsBySupplierRequest) (result map[string]interface{}, err error) {
//索引字典
supplierIndex := query.GetSupplierIndex(r.SupplierId)
//获取查询条件
......@@ -36,12 +36,12 @@ func (qs *QuoteService) GetGoodsBySupplier(r requests.QuoteIndexRequest) (result
goodsList, err := GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
result = GetReturnSupplierData(dullData)
result["total"] = Get(esResult, "hits.total").Int()
result["took"] = Get(esResult, "took").Int()
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["took"] = gjson.Get(esResult, "took").Int()
return
}
func (qs *QuoteService) GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
func (qs *QuoteService) GetGoodsBySupplierName(r requests.GetGoodsBySupplierRequest) (result map[string]interface{}, err error) {
supplierIndex := r.SupplierName
queryString, err := query.GetGoodsBySupplierNameQuery(r)
fmt.Println(supplierIndex)
......@@ -54,8 +54,8 @@ func (qs *QuoteService) GetGoodsBySupplierName(r requests.QuoteIndexRequest) (re
goodsList, err := GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
result = GetReturnSupplierData(dullData)
result["total"] = Get(esResult, "hits.total").Int()
result["took"] = Get(esResult, "took").Int()
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["took"] = gjson.Get(esResult, "took").Int()
return
}
......
package service
import (
"search_server/middleware"
"search_server/model"
"strings"
)
//推送搜索关键词
func ShowSku() {
func ShowSku(goods model.DullGoodsData) {
len := len(goods.GoodsId)
if goods.GoodsId == "" || goods.BrandName == "" || goods.SupplierName == "" {
return
}
referer := middleware.Context.Request.Header.Get("referer")
if strings.Contains(referer, "www.ichunt.com/s/?k") {
if goods.ProviderName != "" {
goods.BrandName = goods.ProviderName
}
if goods.CompanyName != "" {
goods.SupplierName = goods.CompanyName
}
result := make(map[string]string)
result["goods_id"] = goods.GoodsId
//todo : brand_name正则转换
result["brand_name"] = goods.BrandName
if goods.SupplierId == 17 {
result["supplier_name"] = goods.SupplierName + "-" + goods.Canal
result["supplier_type"] = goods.SupplierName
} else if goods.CompanyName != "" && goods.ProviderName != "" {
result["supplier_name"] = "撮合-" + goods.SupplierName
result["supplier_type"] = "撮合"
} else {
result["supplier_name"] = goods.SupplierName
result["supplier_type"] = goods.SupplierName
}
//todo : goods_name正则替换
result["goods_name"] = goods.GoodsName
if (len < 19 || goods.SupplierId == 10000) && result["supplier_type"] != "撮合" {
result["keyword"] = strings.TrimSpace("keyword")
}
result["adtag"], _ = middleware.Context.Cookie("adtag")
if len < 19 {
result["status"] = "1"
}
}
\ No newline at end of file
}
}
......@@ -5,7 +5,6 @@ import (
"fmt"
"github.com/syyongx/php2go"
"search_server/boot"
"search_server/controller"
)
func StrRandom(lenNum int) string {
......@@ -29,6 +28,4 @@ func main() {
if err := boot.Boot(path); err != nil {
fmt.Println(err)
}
fmt.Println(controller.GetData("lm358","arrow"))
}
......@@ -38,4 +38,4 @@ func FloatRound(f float64, n int) float64 {
//fmt.Println(format)
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
return res
}
\ 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