Commit 204e81fb by mushishixian

配置

parent 52bd65bb
......@@ -38,3 +38,9 @@ idle_timeout = 20
[mongo]
host = 127.0.0.1:27017
database = liexin_config
[brand]
#是否开启排除品牌
is_not_ti = 1
ti_ly_brand_ids = 23,45069,7754,17484,43215,13421,5220,7636,44534,43556,44543,9429,758,9470,10430,10653,10707,11227,11276,11819,12608,12965,12997,13327,13675,13680,13752
ti_zy_brand_id = 23
\ No newline at end of file
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"search_server/pkg/common"
"search_server/requests"
"search_server/service"
)
//搜索快手搜搜
//check_button=2&pf=1&keyword=123213&supplier_id=7&offset=2&follow_hit=1&p=1&_=1593582814402
func QuoteIndex(c *gin.Context) {
var request requests.QuoteIndexRequest
if err := c.ShouldBind(&request); err != nil {
fmt.Println(err)
}
if request.Keyword == "" || request.SupplierId == "" {
c.JSON(200, common.GetResponse(1, "缺少参数", []string{}))
return
}
supplierName := "temp"
if supplierName == "" {
c.JSON(200, common.GetResponse(1, "供应商id有误", []string{}))
return
}
service.GetGoodsInfoBySupplier(request)
}
......@@ -33,6 +33,7 @@ require (
github.com/stretchr/testify v1.5.1 // indirect
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
github.com/uniplaces/carbon v0.1.6
go.uber.org/zap v1.14.1 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
......
......@@ -13,3 +13,11 @@ type BomResponse struct {
Total int `json:"total"`
Data interface{} `json:"data"`
}
func GetResponse(errCode int, errMsg string, data interface{}) Response {
return Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
}
package config
import (
"fmt"
"github.com/go-ini/ini"
"strings"
)
var (
cfg *ini.File
Cfg *ini.File
)
func SetUp(path string) (err error) {
//引入多个文件
cfg, err = ini.LooseLoad(path+"/config.ini", path+"/search.ini")
Cfg, err = ini.LooseLoad(path+"/config.ini", path+"/search.ini")
return
}
func Get(key string) *ini.Key {
if strings.Contains(key, ".") {
keys := strings.Split(key, ".")
return cfg.Section(keys[0]).Key(keys[1])
return Cfg.Section(keys[0]).Key(keys[1])
}
return cfg.Section("").Key(key)
return Cfg.Section("").Key(key)
}
func GetSectionValue(key string) {
fmt.Println(cfg.Section("supplier_all").KeyStrings())
func GetSectionValues(key string) (result []string) {
values := Cfg.Section(key).Keys()
for _, value := range values {
result = append(result, value.String())
}
return result
}
......@@ -24,6 +24,8 @@
#### 1.配置读取
接在```conf/config.ini```文件添加自己的配置项
然后使用```config.Get("xxx.xxx").String()```或者```config.Get("xxx.xxx").Int()```即可
如果想要获取某个节点下的所有值,可以用```config.GetSectionValues("xxx")```
如果想要去获取更多其它的值,可以用暴露出来的配置变量自己去获取,比如: ```config.Cfg.Section("xxx").Key("xxx")```
配置包的文档 : https://ini.unknwon.io/docs
#### 2.mysql连接实例
......
package requests
type QuoteIndexRequest struct {
Keyword string `form:"key_word"`
CheckButton int `form:"check_button"`
SupplierId string `form:"supplier_id"`
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"`
}
......@@ -15,5 +15,8 @@ func InitRouter() *gin.Engine {
r.POST("/search/bom/autospu", controller.AutoSpu)
r.POST("/search/bom/recommend", controller.Recommend)
//快手平台相关
r.GET("/search/quote", controller.QuoteIndex)
return r
}
......@@ -4,8 +4,13 @@ import (
"fmt"
"github.com/imroc/req"
"github.com/tidwall/gjson"
"github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/protopb/bom"
"search_server/requests"
"strings"
)
......@@ -68,7 +73,177 @@ func GetGoodsInfo(goodsIdsStr string) (goodsList []*bom.GoodsModel, err error) {
}
//根据供应商获取商品信息
func GetGoodsInfoBySupplier(supplierId, q, stock int) {
func GetGoodsInfoBySupplier(req requests.QuoteIndexRequest) error{
supplierIndex := config.Get("es.search_supplier").String()
//获取查询条件
queryString := getSupplierQuery(req)
res, err := es.CurlES(supplierIndex, queryString)
if err != nil {
return err
}
fmt.Println(supplierIndex)
}
func getSupplierIndex(supplierId string) string {
return "temp"
}
func getSupplierQuery(req requests.QuoteIndexRequest) string {
req.SupplierId = strings.TrimSpace(req.SupplierId)
source := elastic.NewSearchSource()
query := elastic.NewBoolQuery()
//增加联营精确搜索
if req.GoodsNameEq != "" {
var goodsName string
r, _ := regexp.Compile(`/[^A-Za-z0-9]+/u`)
goodsName = r.ReplaceAllString(goodsName, "")
query.Must(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
}
//只显示更新时间在一个月内的数据
oneMonthTimeStamp := carbon.Now().SubMonth().Timestamp()
query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp))
keyword := strings.ToUpper(req.Keyword)
//判断关键词是否为空
if req.Keyword != "" {
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文?
removeCnKeyword := r1.ReplaceAllString(req.Keyword, "")
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
req.Keyword = r2.ReplaceAllString(removeCnKeyword, "")
//
if req.IsExact == 1 {
query.Must(elastic.NewTermQuery("auto_goods_name.raw", keyword))
} else {
if req.Keyword != "" {
subQuery := elastic.NewBoolQuery()
subQuery.Should(elastic.NewTermQuery("goods_name", keyword))
subQuery.Should(elastic.NewTermQuery("goods_name_alike", keyword))
query.Must(subQuery)
}
}
req.Keyword = removeCnKeyword
}
//判断是否存在类似物料搜索
//todo:类似索引获取
if true {
query.Should(elastic.NewTermQuery("goods_name_alike", keyword))
}
//根据状态,排除专卖
query.MustNot(elastic.NewTermQuery("status", 10))
//判断库存
if req.Stock > 0 {
query.Must(elastic.NewRangeQuery("stick").Gte(req.Stock))
}
//判断是否要根据品牌id搜索
if req.BrandId != "" {
brandIdSlice := strings.Split(req.BrandId, ",")
query.Must(elastic.NewTermsQuery("brand_id", brandIdSlice))
}
//是否有货
if req.AvailRank == 1 {
query.Must(elastic.NewTermsQuery("sort", 31))
source.Sort("_score", false)
if req.SupplierId == "17" {
source.Sort("update_time", false)
}
source.Sort("stock", false)
source.Sort("single_price", true)
}
//价格排序
if req.SingleRank != 0 {
//todo : 清空排序
source.Sort("single_price_sort", false)
if req.SingleRank == 1 {
source.Sort("single_price", true)
} else if req.SingleRank == 2 {
source.Sort("single_price", false)
}
source.Sort("_score", false)
}
//库存排序
if req.StockRank != 0 {
source.Sort("stock_sort", false)
if req.StockRank == 2 {
source.Sort("stock", true)
}
if req.StockRank == 1 {
source.Sort("stock", false)
}
source.Sort("_score", false)
}
//综合排序
if req.ComRank == 1 {
//todo : 清空排序
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
//如果什么排序都没有,就开启默认排序
if req.AvailRank == 0 && req.SingleRank == 0 && req.StockRank == 0 && req.ComRank == 0 {
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
var pageSize, pageCurrent, fromSize int
if req.Offset == 0 {
pageSize = 15
} else {
pageSize = req.Offset
}
if req.P == 0 {
pageCurrent = 15
} else {
pageCurrent = req.P
}
//ES从哪里开始获取数据
fromSize = pageSize * (pageCurrent - 1)
if fromSize+pageSize >= 10000 {
fromSize = 9000
pageSize = 100
}
//搜索过来的分页方式 页码,第一页为2页,接下来以每页5条展示
if (req.FirstSearchPage == 2 || req.P == 2) && req.Topic != 1 && req.Keyword != "" {
pageSize = 2
if req.P != 0 {
pageCurrent = req.P
} else {
pageCurrent = 1
}
fromSize = pageSize * (pageCurrent - 1)
if pageCurrent > 1 {
pageSize = 5
fromSize = pageSize*(pageCurrent-1) - 2 - 1
}
}
source.From(fromSize)
source.Size(pageSize)
//todo : $params['preference'] = '_primary_first';
//判断是否要排除某些品牌
//todo : 待确定需不需要接入
searchRequest := elastic.NewSearchRequest().Source(source)
return searchRequest.Body()
}
......@@ -4,7 +4,7 @@ import (
"flag"
"fmt"
"search_server/boot"
"search_server/model"
"search_server/pkg/config"
)
func main() {
......@@ -14,7 +14,10 @@ func main() {
if err := boot.Boot(path); err != nil {
fmt.Println(err)
}
fmt.Println(model.GetExcludeBrandIds(1))
//fmt.Println(model.GetExcludeBrandIds(1))
fmt.Println(config.GetSectionValues("supplier_all"))
fmt.Println(config.Cfg.Section("xxx").Key("xxx"))
//client := client.
//c := course.NewCourseService("go.micro.api.jtthink.course", client)
//course_rsp, _ := c.ListForTop(context.TODO(), &course.ListRequest{Size: 10})
......
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