Commit 95b0141b by huangchengyi

1.0

parent 416343d1
package main
import (
"flag"
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web"
"search_server/boot"
"search_server/pkg/config"
"search_server/routes"
)
func main() {
var path string
flag.StringVar(&path, "config", "conf", "配置文件")
flag.Parse()
if err := boot.Boot(path); err != nil {
panic(err)
}
gin.SetMode(config.Get("web.mode").String())
r := routes.InitRouter()
port := config.Get("web.port").String()
//web改成micro 就是grpc,并直接注册到etcd里面
service := web.NewService(
web.Name("go.micro.api.http.search"),
web.Handler(r),
web.Address(":"+port),
)
if err := service.Init(); err != nil {
panic(err)
}
if err := service.Run(); err != nil {
panic(err)
}
}
......@@ -4,6 +4,7 @@ import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"github.com/syyongx/php2go"
"strings"
)
......@@ -16,6 +17,9 @@ func Sha1(query string, pri_key string) string {
}
func main() {
dd := php2go.InArray("1003",[]string{"1001","1002"})
print(dd)
return
ss := strings.Index("attr[456546]","attr")
print(ss)
......
......@@ -8,11 +8,13 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0
github.com/go-playground/validator/v10 v10.4.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.4.3
github.com/gomodule/redigo v2.0.1-0.20180401191855-9352ab68be13+incompatible
github.com/gookit/color v1.3.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
......@@ -21,6 +23,7 @@ require (
github.com/ichunt2019/go-rabbitmq v1.0.1
github.com/ichunt2019/logger v1.0.5
github.com/imroc/req v0.3.0
github.com/json-iterator/go v1.1.10 // indirect
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
github.com/micro/go-micro v1.16.0
github.com/micro/go-micro/v2 v2.9.0
......@@ -33,16 +36,23 @@ require (
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.1
github.com/tidwall/sjson v1.1.1 // indirect
github.com/ugorji/go v1.1.12 // indirect
github.com/uniplaces/carbon v0.1.6
github.com/xxjwxc/ginrpc v0.0.0-20200904081558-8004c9db8189 // indirect
github.com/xxjwxc/public v0.0.0-20200928160257-3db1045537d1 // indirect
go.mongodb.org/mongo-driver v1.3.5 // indirect
go.uber.org/zap v1.14.1 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee // indirect
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece
google.golang.org/grpc v1.29.1 // indirect
google.golang.org/protobuf v1.24.0
google.golang.org/protobuf v1.25.0
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/olivere/elastic.v5 v5.0.85
gopkg.in/yaml.v2 v2.3.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
xorm.io/core v0.7.2-0.20190928055935-90aeac8d08eb
)
......
......@@ -6,6 +6,7 @@ import (
"github.com/iancoleman/orderedmap"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"math"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
......@@ -19,42 +20,32 @@ import (
type OptimumService struct {
}
var REQUEST_MAP map[string][]string //接收参数的数组,只支持二维
var REQUEST map[string]string //接收所有参数
var REQUEST_ATTR map[string][]string //接收属性搜索
/*
整理接收数组参数
*/
func (qs *OptimumService) getQueryAttr(ctx *gin.Context) (map[string]map[string]string){
func (qs *OptimumService) getQueryAttr(ctx *gin.Context) {
//接收参数,接收参数搜索
allAttrs := make(map[string]map[string]string,0)
REQUEST_MAP := make(map[string][]string)
REQUEST = make(map[string]string)
REQUEST_ATTR = make(map[string][]string)
ctx.MultipartForm()
for requestName, requstValue := range ctx.Request.Form {
if len(requstValue) > 1 { //接收多维数组
values := make([]string,0);
var i int
for i = 0; i < len(requstValue); i++ {
values = append(values,requstValue[i])
}
REQUEST_MAP[requestName] = values
}
REQUEST[requestName] = strings.TrimSpace(requstValue[0])
}
for k,v := range REQUEST_MAP {
subAttrs := make(map[string]string,0)
var i int;
for i = 0;i<len(v);i++ {
subAttrs[v[i]] = "1"
ev := REQUEST["attr"]
if ev != "" { //存在属性搜索
slice1 := php2go.Explode("^", ev);
for _, proa := range slice1 {
prob := php2go.Explode("_", proa);
attr_name_id := prob[0]; //属性名称
attr_value_ids := php2go.Explode("||", prob[1])
REQUEST_ATTR[attr_name_id] = attr_value_ids
}
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
common.PrintDebugHtml(ctx,REQUEST)
common.PrintDebugHtml(ctx,REQUEST_ATTR)
}
/*
......@@ -78,13 +69,16 @@ func (qs *OptimumService) getQueryAttr(ctx *gin.Context) (map[string]map[strin
}
统计属性集合
eg:
https://ichunt.com/optimum-9987_1.html?ev=brand_12042^2660_128036||128037
class_id2=9987 page=1 ev:属性id+属性值id
class_id2=3&attr[brand][]=30485&attr[brand][]=30509&attr[brand][]=37727&attr[1002][]=2001
*/
func (qs *OptimumService) GetOptimumAttr(ctx *gin.Context) (results model.LyResponse) {
allAttrs := qs.getQueryAttr(ctx)
qs.getQueryAttr(ctx)
//获取查询条件
queryString, err := query.GetOptimumAttrQuery(ctx,allAttrs,1)
queryString, err := query.GetOptimumAttrQuery(ctx,&REQUEST_ATTR,1)
if err != nil {
results.ErrorCode = 1001;
results.ErrorMsg = err.Error()
......@@ -128,10 +122,9 @@ func (qs *OptimumService) GetOptimumAttr(ctx *gin.Context) (results model.LyResp
brandName,_ :=redis.String(redisConn.Do("Hget","brand",brandId))
ischeck := "0" //是否选中
if _, ok := allAttrs["brand"]["1002"]; ok {
if php2go.InArray(brandId,REQUEST_ATTR["brand"]) {
ischeck = "1"
}
brandArr = append(brandArr,brandId+"€"+brandName+"€"+ischeck)
}
aggs.Set("brand€品牌",brandArr)
......@@ -144,7 +137,7 @@ func (qs *OptimumService) GetOptimumAttr(ctx *gin.Context) (results model.LyResp
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)) //查询唯一值,反查
attr_name_id,_ := gredis.String(redisConn.Do("HGET",config.Get("redis_all.class_attr_by_val").String(),attr_name)) //属性id
attrValueBuckets := gjson.Get(vbuckets.String(),"attr_value.buckets").Array() //属性值数组: 1V
......@@ -152,10 +145,10 @@ func (qs *OptimumService) GetOptimumAttr(ctx *gin.Context) (results model.LyResp
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)) //查询唯一值,反查
attr_value_id,_ := gredis.String(redisConn.Do("HGET",config.Get("redis_all.class_attr_value_by_val").String(),attr_value_name)) //属性值id
ischeck := "0" //是否选中redigo
if _, ok := allAttrs[attr_value_id][attr_value_id]; ok {
if php2go.InArray(attr_value_id,REQUEST_ATTR[attr_name_id]) {
ischeck = "1"
}
......@@ -175,9 +168,9 @@ func (qs *OptimumService) GetOptimumAttr(ctx *gin.Context) (results model.LyResp
*/
func (qs *OptimumService) GetOptimumAttrInfo(ctx *gin.Context) (results model.LyResponse) {
//索引字典
allAttrs := qs.getQueryAttr(ctx)
qs.getQueryAttr(ctx)
//获取查询条件
queryString, err := query.GetOptimumAttrQuery(ctx,allAttrs,2)
queryString, err := query.GetOptimumAttrQuery(ctx,&REQUEST_ATTR,2)
if err != nil {
results.ErrorCode = 1001;
results.ErrorMsg = err.Error()
......@@ -189,6 +182,8 @@ func (qs *OptimumService) GetOptimumAttrInfo(ctx *gin.Context) (results model.Ly
results.ErrorMsg = "查询es没有数据"
return
}
common.PrintDebugHtml(ctx,esResult)
goodsIds := GetGoodsIdsByEs(esResult)
//判断返回总条数
total := gjson.Get(esResult, "hits.total").Int()
......@@ -202,8 +197,9 @@ func (qs *OptimumService) GetOptimumAttrInfo(ctx *gin.Context) (results model.Ly
A := orderedmap.New() //初始化有序map,拼接data 数据
results.ErrorCode = 0;
results.ErrorMsg = "查询成功";
A.Set("total",total) //查询总条数
A.Set("took", gjson.Get(esResult, "took").Int()) //查询总条数
page := PageClear(ctx,10,total);
page["took"] = gjson.Get(esResult, "took").Int()
A.Set("page_data",page) //分页信息
//aggs := orderedmap.New() //拼接aggs
common.PrintDebugHtml(ctx,goodsIds)
......@@ -239,3 +235,80 @@ func (qs *OptimumService) GetOptimumAttrInfo(ctx *gin.Context) (results model.Ly
results.Data = A;
return
}
//處理分頁信息
func PageClear(ctx *gin.Context,pagesize int64,hitsTotal int64) map[string]interface{}{
totalPage := math.Ceil(float64(hitsTotal)/float64(pagesize))
var (
total int64
offsetInt int
err error
)
page := make(map[string]interface{},0)
if totalPage > 1{
if hitsTotal > 10000 {
total = 10000
}else{
total = hitsTotal
}
p := ctx.Request.FormValue("page")
if p == ""{
p = "1"
}
offset := ctx.Request.FormValue("offset")
offsetInt, err = strconv.Atoi(offset)
if err == nil && offsetInt > 0{
offset = strconv.FormatInt(int64(offsetInt),10)
}else{
offsetInt = 10
offset = "10"
}
//如果是自营同步输出
//自定義分頁
page["orgin_search"] = REQUEST
page["total"] = strconv.FormatInt(total,10)
page["current_page"] = p
page["offset"] = offset
if totalPage > 1000{
page["total_page"] = "1000"
}else{
page["total_page"] = strconv.FormatInt(int64(totalPage),10)
}
pageData := make(map[int]int,0)
pInt := int(1)
pInt, err = strconv.Atoi(p)
if err != nil{
pInt = int(1)
}
if pInt > 1000 {
pInt = 1000
}
if pInt > 996{
for i:=pInt-10;i<=pInt;i++{
//原始查询参数拼接
pageData[i] = i
}
}else{
pageNum := 0
for i:=1;i<=int(totalPage);i++{
if pageNum == 10 {
break
}
if pInt >6 && pInt <= 996{
//大于10页只获取中间10页
if i < pInt -5 || i > pInt+5{
continue
}
}
pageData[i] = i
pageNum++
}
}
page["page_data"] = common.MapSort(pageData)
}else{
page["total"] = 1
}
return page
}
\ No newline at end of file
......@@ -11,13 +11,12 @@ import (
/*
统计属性集合
class_id2=3&attr_name=1001,1002&attr_val_ids=20005,5009
https://ichunt.com/optimum-9987_1.html?ev=brand_12042^2660_128036||128037
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 attrs post 参数
@param types int 返回统计还是结果: 1单独返回统计 2单独返回查询结果
*/
func GetOptimumAttrQuery(ctx *gin.Context,attrs map[string]map[string]string,types int) (param string, err error){
func GetOptimumAttrQuery(ctx *gin.Context,REQUEST_ATTR *map[string][]string,types int) (param string, err error){
query := elastic.NewBoolQuery()
query1 := elastic.NewBoolQuery()
query2 := elastic.NewBoolQuery()
......@@ -32,16 +31,18 @@ func GetOptimumAttrQuery(ctx *gin.Context,attrs map[string]map[string]string,typ
//存在属性查询
isClassAttr := 0 //存在其他属性搜索
if len(attrs) > 0 {
if len(*REQUEST_ATTR) >0 { //存在属性搜索
redisr := gredis.Conn("search_r")
for attr_name_id, v := range attrs {
for attr_name_id, attr_value_ids := range *REQUEST_ATTR {
print(attr_name_id)
if attr_name_id == "brand" {
for brand_id,_ := range v{
query1.Should(elastic.NewTermQuery("brand_id",brand_id))
for _,attr_value_id := range attr_value_ids{
query1.Should(elastic.NewTermQuery("brand_id",attr_value_id))
}
}else{
isClassAttr = 1
for attr_value_id,_ := range v{
for _,attr_value_id := range attr_value_ids{
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))))
......@@ -61,7 +62,7 @@ func GetOptimumAttrQuery(ctx *gin.Context,attrs map[string]map[string]string,typ
if types == 2 { //不需要统计,返回分页数据
page := ctx.Request.FormValue("page")
page_size := ctx.Request.FormValue("page_size")
page_from := 1
page_from := 0
if page == "" {
page = "1";
}
......
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