Commit 64a90538 by 孙龙

up

parent 4327d17d
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"search_server/model" "search_server/model"
"search_server/pkg/common"
"search_server/service" "search_server/service"
"strings" "strings"
) )
...@@ -34,3 +35,18 @@ func Zyh(ctx *gin.Context){ ...@@ -34,3 +35,18 @@ func Zyh(ctx *gin.Context){
//fmt.Printf("%+v",zyhResponse) //fmt.Printf("%+v",zyhResponse)
ctx.JSON(200,zyhResponse) ctx.JSON(200,zyhResponse)
} }
func Index(ctx *gin.Context){
zyhRequest := make(map[string]string,0);
zyhResponse := &model.ZyhResponse{}
ctx.MultipartForm()
for requestName, requstValue := range ctx.Request.Form {
if requstValue[0] != "" {
zyhRequest[requestName] = strings.TrimSpace(requstValue[0])
}
}
zyServiceImpl := service.NewZyServiceImpl()
zyhResponse = zyServiceImpl.Index(ctx,zyhRequest)
common.Output(int(zyhResponse.ErrorCode), zyhResponse.ErrorMsg, zyhResponse.Data)
return
}
\ No newline at end of file
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"search_server/middleware" "search_server/middleware"
"search_server/pkg/config" "search_server/pkg/config"
"search_server/pkg/vars" "search_server/pkg/vars"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
...@@ -323,3 +324,22 @@ func RemoveSliceString(s []string, i string) []string { ...@@ -323,3 +324,22 @@ func RemoveSliceString(s []string, i string) []string {
} }
return b return b
} }
//map排序
func MapSort(mapList map[int]int) []int{
var(
keys []int
newList []int
)
newList = make([]int,0)
for _,v:=range mapList{
keys = append(keys,v)
}
sort.Ints(keys)
for _,k:=range keys{
newList = append(newList,mapList[k])
}
return newList
}
\ No newline at end of file
...@@ -21,6 +21,8 @@ func InitRouter() *gin.Engine { ...@@ -21,6 +21,8 @@ func InitRouter() *gin.Engine {
r.POST("/search/bom/recommend", controller.Recommend) r.POST("/search/bom/recommend", controller.Recommend)
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)
r.GET("search/ZiYing/index", controller.Index)
r.POST("search/ZiYing/index", controller.Index)
r.POST("search/mouser/GetMouserData", controller.GetMouserData) r.POST("search/mouser/GetMouserData", controller.GetMouserData)
r.GET("search/mouser/GetMouserData", controller.GetMouserData) r.GET("search/mouser/GetMouserData", controller.GetMouserData)
......
...@@ -37,7 +37,7 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo ...@@ -37,7 +37,7 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo
need_aggs bool need_aggs bool
_type byte _type byte
) )
isAdmin = true isAdmin = false
if req["need_aggs"] == "1"{ if req["need_aggs"] == "1"{
need_aggs = true need_aggs = true
}else{ }else{
...@@ -137,6 +137,12 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){ ...@@ -137,6 +137,12 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
return rsp return rsp
} }
func(this *ZyServiceImpl) Index(ctx *gin.Context, req map[string]string) (rsp *model.ZyhResponse){
req2 := req
zy_orgin_search = common.CopyMapString(req)//原始搜索數據
rsp = searchZiYingGoods(req2,false,nil,nil,1)
return rsp
}
//自营型号查询 //自营型号查询
/* /*
...@@ -194,7 +200,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -194,7 +200,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
if strings.Contains(k,"/") { if strings.Contains(k,"/") {
k_temp = strings.Split(k,"/") k_temp = strings.Split(k,"/")
if len(k_temp) == 2 { if len(k_temp) == 2 {
isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword,&is_sample) isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword,&is_sample,&goods_name_arr)
if !isok { if !isok {
continue; continue;
} }
...@@ -257,8 +263,8 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -257,8 +263,8 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
//综合排序 //综合排序
if com_rank,ok :=req["com_rank"];ok && strings.TrimSpace(com_rank) == "1" { if com_rank,ok :=req["com_rank"];ok && strings.TrimSpace(com_rank) == "1" {
source.Sort("sort",true) //asc source.Sort("sort",false) //desc
source.Sort("single_price",false) //desc source.Sort("single_price",true) //asc
defaultSort = false defaultSort = false
} }
if defaultSort { if defaultSort {
...@@ -269,13 +275,13 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -269,13 +275,13 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
length := int64(10) length := int64(10)
if offset,ok := req["offset"];ok { if offset,ok := req["offset"];ok {
if offsetInt,err:=strconv.ParseInt(offset, 10, 64); err != nil && offsetInt > 0{ if offsetInt,err:=strconv.ParseInt(offset, 10, 64); err == nil && offsetInt > 0{
length = offsetInt length = offsetInt
} }
} }
page := int64(1) page := int64(1)
if p,ok := req["p"];ok { if p,ok := req["p"];ok {
if pInt,err:=strconv.ParseInt(p, 10, 64); err != nil && pInt > 0{ if pInt,err:=strconv.ParseInt(p, 10, 64); err == nil && pInt > 0{
page = pInt page = pInt
} }
} }
...@@ -470,10 +476,25 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -470,10 +476,25 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
} }
referer := middleware.Context.GetHeader("Referer") referer := middleware.Context.GetHeader("Referer")
if flag <= 0 && referer != "" && php2go.Stripos(referer,"/s/?k=",0) == -1{ if flag <= 0 && php2go.Stripos(referer,"/s/?k=",0) == -1{
response = getPage(response,length,hitsTotal)
}
rsp.Data = response
return rsp
}
//處理分頁信息
func getPage(response map[string]interface{},length int64,hitsTotal int64) map[string]interface{}{
pagesize := length pagesize := length
totalPage := math.Ceil(float64(hitsTotal)/float64(pagesize)) totalPage := math.Ceil(float64(hitsTotal)/float64(pagesize))
var total int64 var (
total int64
offsetInt int
err error
)
if totalPage > 1{ if totalPage > 1{
if hitsTotal > 10000 { if hitsTotal > 10000 {
total = 10000 total = 10000
...@@ -486,10 +507,11 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -486,10 +507,11 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
p = "1" p = "1"
} }
offset := _zy_orgin_search["offset"] offset := _zy_orgin_search["offset"]
offsetInt, err := strconv.Atoi(offset) offsetInt, err = strconv.Atoi(offset)
if err == nil && offsetInt > 0{ if err == nil && offsetInt > 0{
offset = strconv.FormatInt(int64(offsetInt),10) offset = strconv.FormatInt(int64(offsetInt),10)
}else{ }else{
offsetInt = 10
offset = "10" offset = "10"
} }
if isAdmin { if isAdmin {
...@@ -536,40 +558,31 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -536,40 +558,31 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
continue continue
} }
} }
fmt.Println("i:",i) //fmt.Println("i:",i)
pageData[i] = i pageData[i] = i
pageNum++ pageNum++
} }
} }
page["page_data"] = pageData page["page_data"] = common.MapSort(pageData)
response["page"] = page response["page"] = page
}else{ }else{
//非同步输出自己输出分页样式 //非同步输出自己输出分页样式
page, err := strconv.Atoi(p) page, err := strconv.Atoi(p)
if err != nil{ if err != nil || page < 1 {
page = int(1) page = int(1)
} }
pageSize := 10 paging := common.Paginator(page, offsetInt, int64(hitsTotal))
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 10
}
paging := common.Paginator(page, pageSize, 365)
pageHtml := makePageInfo(paging,page) pageHtml := makePageInfo(paging,page)
response["page"] = pageHtml response["page"] = pageHtml
} }
}else{ }else{
response["page"] = map[string]interface{}{"total":totalPage} response["page"] = map[string]interface{}{"total":totalPage}
} }
} return response
rsp.Data = response
return rsp
} }
func makePageInfo(paginator map[string]interface{},currentPage int) string{ func makePageInfo(paginator map[string]interface{},currentPage int) string{
//fmt.Printf("%+v",paginator)
pageHtml := "" pageHtml := ""
pageHtml += `<div class="pagination">` pageHtml += `<div class="pagination">`
pageHtml += fmt.Sprintf(`<a class="prev" href="/product-%d.html">«</a> `,paginator["firstpage"]) pageHtml += fmt.Sprintf(`<a class="prev" href="/product-%d.html">«</a> `,paginator["firstpage"])
...@@ -588,7 +601,7 @@ func makePageInfo(paginator map[string]interface{},currentPage int) string{ ...@@ -588,7 +601,7 @@ func makePageInfo(paginator map[string]interface{},currentPage int) string{
} }
func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string, func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string,
keyword *string,is_sample *bool) bool{ keyword *string,is_sample *bool,goods_name_arr *[]string) bool{
var ( var (
err error err error
...@@ -665,6 +678,7 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -665,6 +678,7 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
return false return false
} }
//提取汉子 //提取汉子
//goods_name_cn 里面装的汉子
reg = regexp.MustCompile(`[\p{Han}]+`) reg = regexp.MustCompile(`[\p{Han}]+`)
goods_name_cn := reg.FindAllString(*searchValue,-1) goods_name_cn := reg.FindAllString(*searchValue,-1)
if len(goods_name_cn) > 0 { if len(goods_name_cn) > 0 {
...@@ -672,8 +686,8 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -672,8 +686,8 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
goods_name_cn[k] = strings.TrimSpace(v) goods_name_cn[k] = strings.TrimSpace(v)
} }
} }
//去除汉字,并以空格分词条 //去除汉字,并以空格分词条
//term_v 里面装的非汉子
reg = regexp.MustCompile(`[\P{Han}]+`) reg = regexp.MustCompile(`[\P{Han}]+`)
term_v =reg.FindAllString(*searchValue, -1) term_v =reg.FindAllString(*searchValue, -1)
if len(term_v) > 0 { if len(term_v) > 0 {
...@@ -681,15 +695,15 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -681,15 +695,15 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
term_v[k] = strings.TrimSpace(v) term_v[k] = strings.TrimSpace(v)
} }
} }
goods_name_arr := append(term_v,goods_name_cn...) *goods_name_arr = append(*goods_name_arr,term_v...)
//fmt.Println("goods_name_arr",goods_name_arr) *goods_name_arr = append(*goods_name_arr,goods_name_cn...)
*keyword = *searchValue *keyword = *searchValue
for _,v:=range term_v{ for _,v:=range term_v{
if v != ""{ if v != ""{
query.Must(elastic.NewTermQuery("goods_name",v)) query.Must(elastic.NewTermQuery("goods_name",v))
} }
} }
if len(goods_name_arr) > 0{ if len(goods_name_cn) > 0{
query.Must(elastic.NewMatchQuery("goods_name_cn",strings.Join(goods_name_cn," ")).MinimumShouldMatch("100%")) query.Must(elastic.NewMatchQuery("goods_name_cn",strings.Join(goods_name_cn," ")).MinimumShouldMatch("100%"))
} }
......
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