Commit 1fa6d352 by huangchengyi

1.0

parent fb9897f3
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"os" "os"
"search_server/boot" "search_server/boot"
"search_server/service/query"
) )
func main() { func main() {
...@@ -42,6 +43,10 @@ func main() { ...@@ -42,6 +43,10 @@ func main() {
// os.Exit(1) // os.Exit(1)
//} //}
//fmt.Errorf() //fmt.Errorf()
println(query.GetExactGoodsQuery("nninin"))
jsonstr := `{"goods_id":1156147221172418504,"goods_sn":"","spu_id":"2156147221162468806","update_time":1561472211,"goods_status":3,"goods_name":"RC0603FR-07100KL","goods_type":2,"supplier_id":17,"encoded":"10059","batch_sn":"1148","moq":27961,"mpq":1,"stock":1765,"hk_delivery_time":"","cn_delivery_time":"4\u4e2a\u5de5\u4f5c\u65e5","goods_details":"","ladder_price":[{"purchases":2765,"price_cn":0.01072951425,"price_us":0},{"purchases":45654,"price_cn":0.343434,"price_us":6666}],"goods_images":"","canal":"L0000110","cp_time":0}` jsonstr := `{"goods_id":1156147221172418504,"goods_sn":"","spu_id":"2156147221162468806","update_time":1561472211,"goods_status":3,"goods_name":"RC0603FR-07100KL","goods_type":2,"supplier_id":17,"encoded":"10059","batch_sn":"1148","moq":27961,"mpq":1,"stock":1765,"hk_delivery_time":"","cn_delivery_time":"4\u4e2a\u5de5\u4f5c\u65e5","goods_details":"","ladder_price":[{"purchases":2765,"price_cn":0.01072951425,"price_us":0},{"purchases":45654,"price_cn":0.343434,"price_us":6666}],"goods_images":"","canal":"L0000110","cp_time":0}`
m, ok := gjson.Parse(jsonstr).Value().(map[string]interface{}) m, ok := gjson.Parse(jsonstr).Value().(map[string]interface{})
if !ok { if !ok {
......
package controller
import (
"github.com/gin-gonic/gin"
"search_server/pkg/common"
"search_server/service"
)
//参数分类统计
func OptimumAttr(c *gin.Context) {
common.PrintDebugHeader() //debug
optimumservice := service.OptimumService{}
apiData := optimumservice.GetOptimumAttr()
common.Output(apiData.ErrorCode, apiData.ErrorMsg, apiData.Data)
common.PrintDebugHtml("-----执行结束----") //debug
}
//参数+分类 商品详情
func OptimumGoods(c *gin.Context) {
common.PrintDebugHeader() //debug
optimumservice := service.OptimumService{}
apiData := optimumservice.GetOptimumAttrInfo()
common.Output(apiData.ErrorCode, apiData.ErrorMsg, apiData.Data)
common.PrintDebugHtml("-----执行结束----") //debug
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ go 1.14 ...@@ -4,6 +4,7 @@ go 1.14
require ( require (
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd // indirect github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd // indirect
github.com/elliotchance/orderedmap v1.3.0 // indirect
github.com/gin-contrib/cors v1.3.1 github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0 github.com/go-ini/ini v1.57.0
...@@ -16,6 +17,7 @@ require ( ...@@ -16,6 +17,7 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.14.3 // indirect github.com/grpc-ecosystem/grpc-gateway v1.14.3 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/ichunt2019/go-rabbitmq v1.0.1 github.com/ichunt2019/go-rabbitmq v1.0.1
github.com/ichunt2019/logger v1.0.5 github.com/ichunt2019/logger v1.0.5
github.com/imroc/req v0.3.0 github.com/imroc/req v0.3.0
...@@ -29,12 +31,13 @@ require ( ...@@ -29,12 +31,13 @@ require (
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.5.1 // indirect github.com/stretchr/testify v1.5.1 // indirect
github.com/syyongx/php2go v0.9.4 github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0 github.com/tidwall/gjson v1.6.1
github.com/tidwall/sjson v1.1.1 // indirect
github.com/uniplaces/carbon v0.1.6 github.com/uniplaces/carbon v0.1.6
go.mongodb.org/mongo-driver v1.3.5 // indirect go.mongodb.org/mongo-driver v1.3.5 // indirect
go.uber.org/zap v1.14.1 // indirect go.uber.org/zap v1.14.1 // indirect
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361 golang.org/x/tools v0.0.0-20191216173652-a0e659d51361
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece
google.golang.org/grpc v1.29.1 // indirect google.golang.org/grpc v1.29.1 // indirect
google.golang.org/protobuf v1.24.0 google.golang.org/protobuf v1.24.0
gopkg.in/ini.v1 v1.51.0 // indirect gopkg.in/ini.v1 v1.51.0 // indirect
......
...@@ -6,16 +6,29 @@ import ( ...@@ -6,16 +6,29 @@ import (
) )
var Context *gin.Context var Context *gin.Context
var REQUEST map[string]string //等同于php 的$_REQUEST var REQUEST map[string]string //等同于php 的$_REQUEST,只接收非数组值
var REQUEST_MAP map[string][]string //接收参数的数组,只支持二维
//将gin的上下文放到全局变量 //将gin的上下文放到全局变量
func ContextVars() gin.HandlerFunc { func ContextVars() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
//接收所有的变量+值 //接收所有的变量+值
REQUEST = make(map[string]string,0); REQUEST = make(map[string]string)
REQUEST_MAP = make(map[string][]string)
c.MultipartForm() c.MultipartForm()
for requestName, requstValue := range c.Request.Form { for requestName, requstValue := range c.Request.Form {
values := make([]string,0);
if len(requstValue) == 1 {
REQUEST[requestName] = strings.TrimSpace(requstValue[0]) REQUEST[requestName] = strings.TrimSpace(requstValue[0])
REQUEST_MAP[requestName] = append(values,requstValue[0])
}
if len(requstValue) > 1 { //接收多维数组
var i int
for i = 0; i < len(requstValue); i++ {
values = append(values,requstValue[i])
}
REQUEST_MAP[requestName] = values
}
} }
//将gin的上下文放到全局变量 //将gin的上下文放到全局变量
......
package model package model
//联营统一返回格式
type LyResponse struct {
ErrorCode int `json:"error_code"`
ErrorMsg string `json:"error_msg"`
Data interface{} `json:"data"`
}
//联营请求外链后格式化数据 //联营请求外链后格式化数据
type LyClearGoodsList struct { type LyClearGoodsList struct {
SkuId string `json:"sku_id"` //平台型号id (非必填) SkuId string `json:"sku_id"` //平台型号id (非必填)
......
...@@ -391,6 +391,10 @@ func MapSort(mapList map[int]int) []int{ ...@@ -391,6 +391,10 @@ func MapSort(mapList map[int]int) []int{
return newList return newList
} }
////////////类型转换///////////////////// ////////////类型转换/////////////////////
func MyInt(str string) int {
res,_ := strconv.ParseInt(str,10,64)
return int(res)
}
func MyInt8(str string) int8 { func MyInt8(str string) int8 {
res,_ := strconv.ParseInt(str,10,64) res,_ := strconv.ParseInt(str,10,64)
return int8(res) return int8(res)
......
...@@ -32,6 +32,21 @@ func CurlES(index string, param string) (result string, err error) { ...@@ -32,6 +32,21 @@ func CurlES(index string, param string) (result string, err error) {
result = resp.String() result = resp.String()
return return
} }
/*
主分片查询
*/
func CurlEsPrimary(index string, param string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",")
//随机获取一个节点进行请求
esUrl := endpoints[rand.Intn(len(endpoints))]
params := req.BodyJSON(param)
resp, err := req.Get(esUrl+"/"+index+"/_search?preference=_primary_first", params)
if err != nil {
return
}
result = resp.String()
return
}
/* /*
批量插入 或者 更新 es 批量插入 或者 更新 es
......
...@@ -14,6 +14,13 @@ func InitRouter() *gin.Engine { ...@@ -14,6 +14,13 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery()) r.Use(gin.Recovery())
r.Use(middleware.Cors()) r.Use(middleware.Cors())
r.Use(middleware.ContextVars()) r.Use(middleware.ContextVars())
//联营参数选择搜索
r.POST("optimum/optimumAttr", controller.OptimumAttr)
r.GET("optimum/optimumAttr", controller.OptimumAttr)
r.POST("optimum/optimumGoods", controller.OptimumGoods)
r.GET("optimum/optimumGoods", controller.OptimumGoods)
//路由 //路由
r.POST("search/ZiYing/zyh", controller.Zyh) r.POST("search/ZiYing/zyh", controller.Zyh)
r.GET("search/ZiYing/zyh", controller.Zyh) r.GET("search/ZiYing/zyh", controller.Zyh)
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"search_server/middleware" "search_server/middleware"
"search_server/model" "search_server/model"
"search_server/pkg/common"
"search_server/pkg/config" "search_server/pkg/config"
"search_server/pkg/gredis" "search_server/pkg/gredis"
"strconv" "strconv"
...@@ -52,6 +53,11 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG ...@@ -52,6 +53,11 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG
goodsServerUrl = config.Get("goods.sz_api_url").String() goodsServerUrl = config.Get("goods.sz_api_url").String()
} }
resp, err := req.Post(goodsServerUrl+"/synchronization", params) resp, err := req.Post(goodsServerUrl+"/synchronization", params)
common.PrintDebugHtml(goodsServerUrl+"/synchronization") //debug
common.PrintDebugHtml(params) //debug
common.PrintDebugHtml(resp.String()) //debug
if err != nil { if err != nil {
return return
} }
......
package ly
import (
"github.com/imroc/req"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"strconv"
"strings"
"time"
)
/*
注意,verical 和 arrow 是同一个公司 arrow,都统一是调arrow的接口
*/
const arrow_api_url string = "http://api.arrow.com/itemservice/v3/en/search/token?login=ichunt&apikey=d229b2b714029c8a1d871ea95f635068627c480ec80ddc5e62c29b17b7192d2b&rows=25&search_token="
/*
单独请求arrow获取数据
*/
func ArrowApi(goodsName *string) string {
if *goodsName == "" {
return ""
}
var result string;
if middleware.REQUEST["flags"] == "-1" { //原始数据调试
result = `{"itemserviceresult":{"serviceMetaData":[{"version":"3.0.2"}],"transactionArea":[{"response":{"returnCode":"0","returnMsg":"","success":true},"responseSequence":{"transactionTime":"8.395 ms","queryTime":"0 ms","dbTime":"0 ms","totalItems":1,"resources":[],"qq":150}}],"data":[{"resources":[{"type":"search","uri":"https:\/\/www.arrow.com\/en\/products\/search?q=nrc06j240trf"}],"PartList":[{"itemId":1731466,"partNum":"NRC06J240TRF","manufacturer":{"mfrCd":"NIC","mfrName":"NIC Components"},"desc":"Res Thick Film 0603 24 Ohm 5% 0.1W(1\/10W) \u00b1200ppm\/\u00b0C Pad SMD Automotive T\/R","packageType":"","resources":[{"type":"cloud_part_detail","uri":"https:\/\/www.arrow.com\/en\/products\/nrc06j240trf\/nic-components"},{"type":"api_part_detail","uri":"http:\/\/api.arrow.com\/itemservice\/v2\/en\/detail?mfrCd=NIC&partNum=NRC06J240TRF"},{"type":"part_detail","uri":"http:\/\/api.arrow.com\/itemservice\/v2\/en\/detail?pkey=d4reVMqt21-TTjZ2c-CxBfkjR7Jr9jVALOn2A_ewFOo"}],"EnvData":{"compliance":[{"displayLabel":"eurohs","displayValue":"Compliant with Exemption"},{"displayLabel":"cnrohs","displayValue":"Compliant with Exemption"}]},"InvOrg":{"sources":[{"currency":"USD","sourceCd":"VERICAL","displayName":"Verical","sourceParts":[{"packSize":5000,"minimumOrderQuantity":5000,"sourcePartNumber":"","sourcePartId":"20192988","Prices":{"resaleList":[{"displayPrice":"0.0022","price":0.0022,"minQty":22778,"maxQty":99999999}]},"Availability":[{"fohQty":65000,"availabilityCd":"INSTK","availabilityMessage":"In Stock","pipeline":[]}],"customerSpecificPricing":[],"customerSpecificInventory":[],"dateCode":"","resources":[{"type":"detail","uri":"https:\/\/www.verical.com\/pd\/nic-fixed-single---surface-mount-NRC06J240TRF-2087567"},{"type":"add_to_cart","uri":"https:\/\/www.verical.com\/pd\/nic-fixed-single---surface-mount-NRC06J240TRF-2087567"}],"inStock":true,"mfrLeadTime":0,"isNcnr":false,"isNpi":false,"isASA":false,"requestQuantity":0,"productCode":"","iccLevels":[],"cloudMfrCode":"","eccnCode":"EAR99","htsCode":"8533.21.00.30","countryOfOrigin":"TW","locationId":49,"containerType":""},{"packSize":1,"minimumOrderQuantity":16667,"sourcePartNumber":"","sourcePartId":"30238877","Prices":{"resaleList":[{"displayPrice":"0.00050","price":0.0005,"minQty":16667,"maxQty":99999999}]},"Availability":[{"fohQty":18977,"availabilityCd":"INSTK","availabilityMessage":"In Stock","pipeline":[]}],"customerSpecificPricing":[],"customerSpecificInventory":[],"dateCode":"1710","resources":[{"type":"detail","uri":"https:\/\/www.verical.com\/pd\/nic-fixed-single---surface-mount-NRC06J240TRF-2087567"},{"type":"add_to_cart","uri":"https:\/\/www.verical.com\/pd\/nic-fixed-single---surface-mount-NRC06J240TRF-2087567"}],"inStock":true,"mfrLeadTime":0,"isNcnr":false,"isNpi":false,"isASA":false,"requestQuantity":0,"productCode":"","iccLevels":[],"cloudMfrCode":"","eccnCode":"EAR99","htsCode":"8533.21.00.30","countryOfOrigin":"TW","locationId":175,"containerType":""}]},{"currency":"USD","sourceCd":"ACNA","displayName":"Arrow Americas","sourceParts":[{"packSize":5000,"minimumOrderQuantity":5000,"sourcePartNumber":"","sourcePartId":"V36:1790_16243328","Prices":{"resaleList":[{"displayPrice":"0.002","price":0.002,"minQty":5000,"maxQty":999999999}]},"Availability":[{"fohQty":0,"availabilityCd":"NOSTK","availabilityMessage":"No Stock","pipeline":[]}],"customerSpecificPricing":[],"customerSpecificInventory":[],"dateCode":"","resources":[{"type":"detail","uri":"https:\/\/www.arrow.com\/en\/products\/nrc06j240trf\/nic-components"},{"type":"add_to_cart","uri":"https:\/\/www.arrow.com\/en\/products\/nrc06j240trf\/nic-components"}],"inStock":false,"mfrLeadTime":20,"isNcnr":false,"isNpi":false,"isASA":false,"requestQuantity":0,"productCode":"","iccLevels":[],"cloudMfrCode":"","eccnCode":"EAR99","htsCode":"8533.21.00.30","locationId":175,"containerType":""},{"packSize":1,"minimumOrderQuantity":1,"sourcePartNumber":"","sourcePartId":"V72:2272_16243328","Prices":{"resaleList":[{"displayPrice":"0.0046","price":0.0046,"minQty":1,"maxQty":9},{"displayPrice":"0.0025","price":0.0025,"minQty":10,"maxQty":99},{"displayPrice":"0.0012","price":0.0012,"minQty":100,"maxQty":499},{"displayPrice":"0.0011","price":0.0011,"minQty":500,"maxQty":999},{"displayPrice":"0.00070","price":0.0007,"minQty":1000,"maxQty":2999},{"displayPrice":"0.00050","price":0.0005,"minQty":3000,"maxQty":999999999}]},"Availability":[{"fohQty":18977,"availabilityCd":"CSTRP","availabilityMessage":"Cut Strips","pipeline":[]}],"customerSpecificPricing":[],"customerSpecificInventory":[],"dateCode":"1710","resources":[{"type":"detail","uri":"https:\/\/www.arrow.com\/en\/products\/nrc06j240trf\/nic-components"},{"type":"add_to_cart","uri":"https:\/\/www.arrow.com\/en\/products\/nrc06j240trf\/nic-components"}],"inStock":true,"mfrLeadTime":20,"isNcnr":false,"isNpi":false,"isASA":false,"requestQuantity":0,"productCode":"","iccLevels":[],"cloudMfrCode":"","eccnCode":"EAR99","htsCode":"8533.21.00.30","countryOfOrigin":"TW","locationId":175,"containerType":"Cut Strips"}]}]},"hasDatasheet":true}]}]}}`;
}else{
req.SetTimeout(10 * time.Second)
resp, err := req.Get(arrow_api_url+*goodsName)
if err != nil {
print(arrow_api_url)
print(err)
}
result = resp.String(); //请求外链拿到结果
}
common.PrintDebugHtml("原始数据:"+result)
return result
}
/*
获取对应数据
@param string stype 获取对应渠道的数据 eg: 1:arrow ,2:verical
@param string apiResult 请求arrow接口返回的所有数据,关键词查询
@return 返回格式化后的数据
*/
func GetDataArrow(apiResult *string,stype string) map[string]*model.LyClearGoodsList {
if apiResult == nil {
return nil
}
productList := make(map[string]*model.LyClearGoodsList,0)
if gjson.Get(*apiResult,"itemserviceresult.data").String() == "" {
common.PrintDebugHtml("返回数据存在问题"+*apiResult)
return nil
}
if stype == "1" {
stype = "Verical"
}
if stype == "2" {
stype = "Arrow Americas"
}
apiGoodsList := gjson.Get(*apiResult, "itemserviceresult.data.PartList").Array()
for _, goods := range apiGoodsList {
//brandName := goods.Get("manufacturer.mfrName").String() //品牌
goodsName := goods.Get("partNum").String() //型号名称
sources := goods.Get("InvOrg.sources").Array();
for _,info := range sources{
if info.Get("displayName").String() != stype {
continue;
}
sourceParts := info.Get("sourceParts").Array()
for _,item := range sourceParts{
url := item.Get("resources.1.uri").String()
url_arr := strings.Split(url,"-");
itemId := url_arr[len(url_arr)-1]
goodsSn := goodsName+"€€"+itemId
ladderPrice := make([]*model.TierItem, 0)
priceTemp := make([]interface{}, 0)
//拼接价格梯度
apiPriceTi := goods.Get("PriceBreaks").Array()
var apiLowerPrice float64 = 0; //计算最低价格
for _,priceItem := range apiPriceTi{
priceItemStr := priceItem.String();
//价格转换
onePrice := gjson.Get(priceItemStr, "Price").String()
onePrice = strings.ReplaceAll(strings.Trim(onePrice,"$"),",","")
skuPrice,_ := strconv.ParseFloat(onePrice,64) //转成float64
//数量转换
quantity := gjson.Get(priceItemStr, "Quantity").Int()
if apiLowerPrice == 0 {
apiLowerPrice = skuPrice
}else if apiLowerPrice > skuPrice {
apiLowerPrice = skuPrice
}
ladder := model.TierItem{
Purchases: quantity,
PriceUs: skuPrice,
PriceCn: 0,
}
ladderPrice = append(ladderPrice, &ladder)
//梯度缓存数据
priceTemp = append(priceTemp,[]interface{}{
quantity,
skuPrice,
})
}
//计算库存
tempStock := goods.Get("Availability").String()
stock := strings.Trim(strings.ReplaceAll(tempStock," 有庫存","")," ")
var Mystock int64 = 0 ;
if stock != "" {
Mystock = common.MyInt64(stock);
}
//拼接联营数据
LyClearGoodsList := model.LyClearGoodsList{
GoodsName: goods.Get("ManufacturerPartNumber").String(),
BrandName: goods.Get("Manufacturer").String(),
Desc: goods.Get("Description").String(),
GoodsSn: goodsSn,
Docurl: goods.Get("DataSheetUrl").String(),
Url: goods.Get("ProductDetailUrl").String(),
GoodsImg: goods.Get("ImagePath").String(),
Cat: goods.Get("Category").String(),
Increment: goods.Get("Mult").Int(),
RestrictionMessage: goods.Get("RestrictionMessage").String(),
Stock:Mystock, //库存
Moq:goods.Get("Min").Int(), //最低起订量
SinglePrice: apiLowerPrice,
Tiered:ladderPrice,
PriceTemp: priceTemp,
}
productList[goodsSn] = &LyClearGoodsList
}
}
}
//fmt.Println(productList)
return productList
}
package service
import (
"github.com/gomodule/redigo/redis"
"github.com/iancoleman/orderedmap"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/service/query"
"strings"
)
type OptimumService struct {
}
/*
整理接收数组参数
*/
func (qs *OptimumService) getQueryAttr() (map[string]map[string]string){
//接收参数,接收参数搜索
allAttrs := make(map[string]map[string]string,0)
for k,v := range middleware.REQUEST_MAP {
subAttrs := make(map[string]string,0)
var i int;
for i = 0;i<len(v);i++ {
subAttrs[v[i]] = "1"
}
if strings.Index(k,"attr") == -1 { //查询是否是需要的属性
continue;
}
k = php2go.StrReplace("attr[","",k,1)
k = php2go.StrReplace("]","",k,1)
//fmt.Print(k)
allAttrs[k] = subAttrs
}
return allAttrs
}
/*
获取统计分类属性,分组统计
{
"error_code": 0,
"error_msg": "success",
"data": {
"total": 10, #查询总数
"aggs": { #属性数组
"brand€品牌": [ #属性id+属性名称
"3001€mouser€0", #属性值id+属性值+是否选中
"4003€arrowV€0"
],
"1002€标称电压": [ #属性id+属性名称
"2001€5.5V€0", #属性值id+属性值+是否选中
"2003€5.6V€0"
]
}
}
}
统计属性集合
class_id2=3&attr[brand][]=30485&attr[brand][]=30509&attr[brand][]=37727&attr[1002][]=2001
*/
func (qs *OptimumService) GetOptimumAttr() (results model.LyResponse) {
allAttrs := qs.getQueryAttr()
//获取查询条件
queryString, err := query.GetOptimumAttrQuery(allAttrs,1)
if err != nil {
results.ErrorCode = 1001;
results.ErrorMsg = err.Error()
return
}
//查询es
esResult, err := es.CurlEsPrimary("goods_optimum", queryString)
if err != nil {
results.ErrorCode = 1002;
results.ErrorMsg = err.Error()
return
}
common.PrintDebugHtml(esResult)
//判断返回总条数
total := gjson.Get(esResult, "hits.total").Int()
if total == 0 {
results.ErrorCode = 0;
results.ErrorMsg = "查询没有数据"
return
}
//拼接返回数据
A := orderedmap.New() //初始化有序map,拼接data 数据
results.ErrorCode = 0;
results.ErrorMsg = "查询成功";
A.Set("total",total) //查询总条数
aggs := orderedmap.New() //拼接aggs
aggregations := gjson.Get(esResult,"aggregations").Map()
redisConn := gredis.Conn("search_r")
//brand_id,存在品牌统计数据
brandIdGroup := gjson.Get(aggregations["brand_id"].String(),"buckets").Array()
if len(aggregations) > 0{
brandArr := make([]string,0)
for _,v := range brandIdGroup{
valueMap := v.Map()
brandId := valueMap["key"].String()
brandName,_ :=redis.String(redisConn.Do("Hget","brand",brandId))
ischeck := "0" //是否选中
if _, ok := allAttrs["brand"]["1002"]; ok {
ischeck = "1"
}
brandArr = append(brandArr,brandId+"€"+brandName+"€"+ischeck)
}
aggs.Set("brand€品牌",brandArr)
}
//拼接属性统计
attrsAttrNameBuckets := gjson.Get(aggregations["attrs"].String(),"attr_name.buckets").Array()
if len(attrsAttrNameBuckets) > 0{
for _,vbuckets:=range attrsAttrNameBuckets{
vbucketsMap := vbuckets.Map()
attr_name :=vbucketsMap["key"].String() //属性名称: 电容
attr_name_id,_ := gredis.String(redisConn.Do("HGET",config.Get("redis_all.class_attr_by_val").String(),attr_name)) //查询唯一值,反查
attrValueBuckets := gjson.Get(vbuckets.String(),"attr_value.buckets").Array() //属性值数组: 1V
attrValueArr := make([]string,0)
for _,v := range attrValueBuckets{
valueMap := v.Map()
attr_value_name := valueMap["key"].String() //属性值:1V
attr_value_id,_ := gredis.String(redisConn.Do("HGET",config.Get("redis_all.class_attr_value_by_val").String(),attr_value_name)) //查询唯一值,反查
ischeck := "0" //是否选中
if _, ok := allAttrs[attr_value_id][attr_value_id]; ok {
ischeck = "1"
}
attrValueArr = append(attrValueArr,attr_value_id+"€"+attr_value_name+"€"+ischeck)
}
aggs.Set(attr_name_id+"€"+attr_name,attrValueArr)
}
}
A.Set("aggs",aggs)
results.Data = aggs
return
}
/*
返回供应商详情
*/
func (qs *OptimumService) GetOptimumAttrInfo() (results model.LyResponse) {
//索引字典
allAttrs := qs.getQueryAttr()
//获取查询条件
queryString, err := query.GetOptimumAttrQuery(allAttrs,2)
if err != nil {
results.ErrorCode = 1001;
results.ErrorMsg = err.Error()
return
}
esResult, err := es.CurlES("goods_optimum", queryString)
if err != nil {
results.ErrorCode = 0;
results.ErrorMsg = "查询es没有数据"
return
}
goodsIds := GetGoodsIdsByEs(esResult)
//判断返回总条数
total := gjson.Get(esResult, "hits.total").Int()
if total == 0 {
results.ErrorCode = 0;
results.ErrorMsg = "查询没有数据"
return
}
//拼接返回数据
A := orderedmap.New() //初始化有序map,拼接data 数据
results.ErrorCode = 0;
results.ErrorMsg = "查询成功";
A.Set("total",total) //查询总条数
A.Set("took", gjson.Get(esResult, "took").Int()) //查询总条数
//aggs := orderedmap.New() //拼接aggs
common.PrintDebugHtml(goodsIds)
//组装商品详情
goodsList, _ := GetGoodsInfo(goodsIds)
//for _,v := range goodsList{
// v.
//}
A.Set("goods_lists",goodsList)
results.Data = A;
return
}
package query
import (
"errors"
"gopkg.in/olivere/elastic.v5"
"search_server/middleware"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/gredis"
)
/*
统计属性集合
class_id2=3&attr_name=1001,1002&attr_val_ids=20005,5009
cat_id=398&attr[brand][]=30485&attr[brand][]=30509&attr[brand][]=37727&attr[brand][]=37774&attr[13896][]=157963&s=0&i=0&d=0
@param attrs 前端组装好post参数
@param types int 返回统计还是结果: 1单独返回统计 2单独返回查询结果
*/
func GetOptimumAttrQuery(attrs map[string]map[string]string,types int) (param string, err error){
query := elastic.NewBoolQuery()
query1 := elastic.NewBoolQuery()
query2 := elastic.NewBoolQuery()
//二级分类查询
class_id2 := middleware.REQUEST["class_id2"]
if class_id2 == "" {
return "",errors.New("二级分类不得为空")
}else{
query1.Must(elastic.NewTermQuery("class_id2",class_id2))
}
//存在属性查询
isClassAttr := 0 //存在其他属性搜索
if len(attrs) > 0 {
redisr := gredis.Conn("search_r")
for attr_name_id, v := range attrs {
if attr_name_id == "brand" {
for brand_id,_ := range v{
query1.Should(elastic.NewTermQuery("brand_id",brand_id))
}
}else{
isClassAttr = 1
for attr_value_id,_ := range v{
attr_name,_ := gredis.String(redisr.Do("HGET",config.Get("redis_all.class_attr_by_id").String(),attr_name_id)) //查询唯一值,反查sku_id
attr_value_name,_ := gredis.String(redisr.Do("HGET",config.Get("redis_all.class_attr_value_by_id").String(),attr_value_id)) //查询唯一值,反查sku_id
query2.Should(elastic.NewNestedQuery("attrs",elastic.NewBoolQuery().Must(elastic.NewTermQuery("attrs.attr_name",attr_name),elastic.NewTermQuery("attrs.attr_value",attr_value_name))))
}
}
}
}
source := elastic.NewSearchSource()
if types == 1 { //单独返回属性统计
source.Aggregation("brand_id",elastic.NewTermsAggregation().Field("brand_id").Size(1000))
source.Aggregation("attrs",elastic.NewNestedAggregation().Path("attrs").
SubAggregation("attr_name",elastic.NewTermsAggregation().Field("attrs.attr_name").Size(5000).
SubAggregation("attr_value",elastic.NewTermsAggregation().Field("attrs.attr_value").Size(5000))))
source.Size(0)
}
if types == 2 { //不需要统计,返回分页数据
page := middleware.REQUEST["page"]
page_size := middleware.REQUEST["page_size"]
page_from := 1
if page == "" {
page = "1";
}
if page_size == "" {
page_size = "10";
}
if page != "1" {
page_from = (common.MyInt(page)-1)*common.MyInt(page_size)
if page_from >10000 {
page_from = 10000;
}
}
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_id")) //显示字段
source.Size(common.MyInt(page_size))
source.From(page_from)
}
query.Must(query1)
if isClassAttr == 1 { //存在nested 参数搜索
query.Must(query2)
}
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
param, _ = searchRequest.Body()
common.PrintDebugHtml(param)
return
}
/*
上面函数最终拼接的es查询:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"brand_id": 108
}
},
{
"term": {
"brand_id": 107
}
}
]}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"nested": {
"path": "attrs",
"query": {
"term": {
"attrs.attr_value": "10%"
}
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"term": {
"attrs.attr_name": "容差"
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "attrs",
"query": {
"term": {
"attrs.attr_value": "4A"
}
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"term": {
"attrs.attr_name": "额定电流(安培)"
}
}
}
}
]
}
}
]
}
}
]
}
},
"aggs": {
"brand_id": {
"terms": {
"field": "brand_id",
"size": 1000
}
},
"attrs": {
"nested": {
"path": "attrs"
},
"aggs": {
"attr_name": {
"terms": {
"field": "attrs.attr_name",
"size": 5000
},
"aggs": {
"attr_value": {
"terms": {
"field": "attrs.attr_value",
"size": 5000
}
}
}
}
}
}
},
"sort": {
"sort": {
"order": "desc"
}
},
"from": 0,
"size": 10
}
*/
File mode changed
...@@ -4,8 +4,7 @@ import ( ...@@ -4,8 +4,7 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
_ "encoding/base64" "strings"
_ "github.com/syyongx/php2go"
) )
func Sha1(query string, pri_key string) string { func Sha1(query string, pri_key string) string {
...@@ -17,7 +16,11 @@ func Sha1(query string, pri_key string) string { ...@@ -17,7 +16,11 @@ func Sha1(query string, pri_key string) string {
} }
func main() { func main() {
ss := strings.Index("attr[456546]","attr")
print(ss)
return
//println(php2go.Sha1("123456"+"123456")) //println(php2go.Sha1("123456"+"123456"))
ss := Sha1("POST&https%3A%2F%2Fapi.tme.eu%2FProducts%2FSearch.json&Country%3DUS%26Currency%3DUSD%26Language%3DEN%26SearchPlain%3DRMCF0402FT12R0%26Token%3D3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4","a1d8647fa38710140628") //ss := Sha1("POST&https%3A%2F%2Fapi.tme.eu%2FProducts%2FSearch.json&Country%3DUS%26Currency%3DUSD%26Language%3DEN%26SearchPlain%3DRMCF0402FT12R0%26Token%3D3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4","a1d8647fa38710140628")
println(ss) //println(ss)
} }
package main
import (
"encoding/json"
"fmt"
"github.com/iancoleman/orderedmap"
)
func main() {
o := orderedmap.New()
o.Set("error_code", 1)
o.Set("error_msg", "fagaga")
s := orderedmap.New()
s.Set("total",10)
aggs := orderedmap.New()
aggs.Set("1001€品牌",[...]string{"3001€mouser€0","4003€arrowV€0"})
aggs.Set("1002€标称电压",[...]string{"2001€5.5V€0","4003€arrowV€0"})
s.Set("aggs",aggs)
o.Set("data", s)
m1,_ := o.Get("error_msg")
fmt.Print(m1)
bytes, err := json.Marshal(o)
fmt.Print(string(bytes),err)
return
//// use Get instead of i, ok := o["a"]
//val, ok := o.Get("a")
//
//// use Keys instead of for k, v := range o
//key := o.Keys()
//for _, k := range keys {
// v, _ := o.Get(k)
//}
// use o.Delete instead of delete(o, key)
//err := o.Delete("a")
// serialize to a json string using encoding/json
//bytes, err := json.Marshal(o)
//fmt.Print(string(bytes),err)
//prettyBytes, err := json.MarshalIndent(o)
//
//// deserialize a json string using encoding/json
//// all maps (including nested maps) will be parsed as orderedmaps
//s := `{"a": 1}`
//err := json.Unmarshal([]byte(s), &o)
//
//// sort the keys
//o.SortKeys(sort.Strings)
//
//// sort by Pair
//o.Sort(func(a *Pair, b *Pair) bool {
// return a.Value().(float64) < b.Value().(float64)
//})
}
\ No newline at end of file
package main
import (
"github.com/tidwall/sjson"
"gopkg.in/olivere/elastic.v5"
)
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
const param1 = `{
"query": {
"bool": {
"must": [
]
}
},
"aggs": {
"brand_id": {
"terms": {
"field": "brand_id",
"size": 1000
}
},
"attrs": {
"nested": {
"path": "attrs"
},
"aggs": {
"attr_name": {
"terms": {
"field": "attrs.attr_name",
"size": 5000
},
"aggs": {
"attr_value": {
"terms": {
"field": "attrs.attr_value",
"size": 5000
}
}
}
}
}
}
},
"sort": {
"sort": {
"order": "desc"
}
},
"from": 0,
"size": 0
}`
func main() {
query := elastic.NewBoolQuery()
query1 := elastic.NewBoolQuery()
query2 := elastic.NewBoolQuery()
query1.Must(elastic.NewTermQuery("class_id2",111))
query1.Should(elastic.NewTermQuery("brand_id","108"))
query1.Should(elastic.NewTermQuery("brand_id","107"))
query2.Should(elastic.NewNestedQuery("attrs",elastic.NewBoolQuery().Must(elastic.NewTermQuery("attrs.attr_name","nin"),elastic.NewTermQuery("attrs.attr_value","nni"))))
query2.Should(elastic.NewNestedQuery("attrs",elastic.NewBoolQuery().Must(elastic.NewTermQuery("attrs.attr_name","3333"),elastic.NewTermQuery("attrs.attr_value","333"))))
source := elastic.NewSearchSource()
source.Aggregation("brand_id",elastic.NewTermsAggregation().Field("brand_id").Size(1000))
source.Aggregation("attrs",elastic.NewNestedAggregation().Path("attrs").
SubAggregation("attr_name",elastic.NewTermsAggregation().Field("attrs.attr_name").Size(5000).
SubAggregation("attr_value",elastic.NewTermsAggregation().Field("attrs.attr_value").Size(5000))))
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_id", "stock")) //显示字段
source.Size(1)
query.Must(query1)
query.Must(query2)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
queryString, _ := searchRequest.Body()
print(queryString)
return
value, _ := sjson.Set(param1, "query.bool.must.-1", map[string]interface{}{"hello":"world"})
value, _ = sjson.Set(value, "query.bool.must.-1", map[string]interface{}{"hellosss":"world"})
println(value)
}
\ 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