Commit 7ff1937b by huangchengyi

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

parents 850a0673 9365927c
......@@ -13,17 +13,9 @@ max_active = 100
idle_timeout = 20
[api_redis_read]
[api_redis]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
[api_redis_write]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
idle_timeout = 20
\ No newline at end of file
......@@ -13,7 +13,7 @@
12 = alliedelec
13 = avnet
14 = mouser
16 = liexin_lianying1
#16 = liexin_lianying1
17 = zhuanmai
18 = liexin_ti
19 = peigenesis
......@@ -34,6 +34,35 @@
19 = peigenesis
20 = powell
21 = rs
[SEARCH_SUPPLIER]
1 = future
#2 = powerandsignal
3 = rochester
4 = tme
5 = verical
6 = element14
7 = digikey
8 = chip1stop
9 = aipco
10 = arrow
#11 = bisco
12 = alliedelec
13 = avnet
14 = mouser
#15 = company
#16 = liexin_lianying1
17 = zhuanmai
#18 = liexin_ti
19 = peigenesis
20 = powell
21 = rs
#22 = liexin_sell
#100 = ziying
#101 = liexin_jingdiao
1676 = buerklin
#相似物料供应商
[ALIKE_SUPPLIER]
1 = ti
......
package controller
//func init() {
// courseService := service.NewCourseServiceImpl()
// gin_.NewBuilder().WithService(courseService).
// WithMiddleware(Check_Middleware()).
// WithMiddleware(Cors_Middleware()).
// WithEndpoint(Courselist_Endpoint(courseService)).
// WithRequest(Courselist_Request()).
// WithResponse(Course_Response()).Build("/course", "GET")
//
// gin_.NewBuilder().WithService(courseService).
// WithMiddleware(Check_Middleware()).
// WithEndpoint(Coursedetail_Endpoint(courseService)).
// WithRequest(Coursedetail_Request()).
// WithResponse(Course_Response()).Build("/detail/:course_id", "GET")
//
//}
//
////详细相关
//func Coursedetail_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
// return func(context *gin.Context, request interface{}) (response interface{}, err error) {
// rsp := &course.DetailResponse{Course: new(course.CourseModel), Counts: make([]*course.CourseCounts, 0)}
// err = c.GetDetail(context, request.(*course.DetailRequest), rsp)
// return rsp, err
// }
//}
//
////这个函数的作用是怎么处理请求
//func Coursedetail_Request() gin_.EncodeRequestFunc {
// return func(context *gin.Context) (i interface{}, e error) {
// bReq := &course.DetailRequest{}
// err := context.BindUri(bReq)
// if err != nil {
// return nil, err
// }
// err = context.BindHeader(bReq)
// if err != nil {
// return nil, err
// }
// return bReq, nil
// }
//}
//
////获取列表相关
//func Courselist_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
// return func(context *gin.Context, request interface{}) (response interface{}, err error) {
// rsp := &course.ListResponse{}
// err = c.ListForTop(context, request.(*course.ListRequest), rsp)
// return rsp, err
// }
//}
//
////这个函数的作用是怎么处理请求
//func Courselist_Request() gin_.EncodeRequestFunc {
// return func(context *gin.Context) (i interface{}, e error) {
// bReq := &course.ListRequest{}
// err := context.BindQuery(bReq) //使用的是query 参数
// if err != nil {
// return nil, err
// }
// return bReq, nil
// }
//}
//
////这个函数作用是:怎么处理响应结果
//func Course_Response() gin_.DecodeResponseFunc {
// return func(context *gin.Context, res interface{}) error {
// context.JSON(200, res)
// return nil
// }
//}
package controller
//func init() {
// courseService:=service.NewCourseTopicServiceImpl()
// gin_.NewBuilder().WithService(courseService).
// WithMiddleware(Check_Middleware()).
// WithEndpoint(CourseTopicList_Endpoint(courseService)).
// WithRequest(CourseTopicList_Request()).
// WithResponse(Course_Response()).Build("/topic/:cid","GET")
//}
//
//func CourseTopicList_Endpoint(c *service.CourseTopicServiceImpl) gin_.Endpoint {
// return func(context *gin.Context, request interface{}) (response interface{}, err error) {
// rsp:=&course.TopicResponse{Result:make([]*course.CourseTopic,0)}
// err=c.GetTopic(context,request.(*course.TopicRequest),rsp)
// return rsp,err
// }
//}
////这个函数的作用是怎么处理请求
//func CourseTopicList_Request() gin_.EncodeRequestFunc{
// return func(context *gin.Context) (i interface{}, e error) {
// bReq:=&course.TopicRequest{}
// err:=context.BindUri(bReq) //使用的是query 参数
// if err!=nil{
// return nil,err
// }
// return bReq,nil
// }
//}
\ No newline at end of file
package controller
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"search_server/service/ly"
"time"
)
//搜索型号
func GetMouserData(c *gin.Context) {
goodsName, _ := c.GetPostForm("goods_name")
//lycon := new(ly.CommonLyService)
//调外链拿数据
apiData := ly.OutLinkMouser(&goodsName, "-1")
//var shu string
//for _, info := range apiData {
//
// //PrintHtml(t.ctx,&t.flag,"snSku:"+snSku+" 查询得出sku"+skuId+" info",info)
// //jsonDatas,_ := json.Marshal(map[string]interface{}{
// // "dddd":"333333",
// // "ninifa":"333333",
// // "3333":"333333",
// // "gagaga":"333333",
// //})
// //fmt.Println(len(info))
// josn, errs := json.Marshal(info)
// if errs != nil {
// fmt.Println("错误")
// }
// shu += string(josn)
//}
// jsonDatas,_ := json.Marshal(map[string]interface{}{
// "dddd":"333333",
// "ninifa":"333333",
// "3333":"333333",
// "gagaga":"333333",
// })
josn, errs := json.Marshal(apiData)
if errs != nil {
fmt.Println("错误")
}
time.Sleep(1 * time.Second)
temp := string(josn)
c.JSON(200, temp)
return
//供应商详情
//supplierInfo := model.SUPPLIER_REDIS_INFO{
// SupplierId:14,
// SupplierNickname: "mouser",
//}
//err := lycon.GetSkuByGoodsSn(apiData,&supplierInfo)
//var errCode int
//if !err {
// errCode = 1
//}
//res := common.BomResponse{
// ErrCode: errCode,
// ErrMsg: "",
// Data: "",
//}
//c.JSON(200, map[string]interface{}{
// "error_code":777777,
// "err_msg":"ddddd2",
//})
}
package controller
import (
"github.com/gin-gonic/gin"
"regexp"
"search_server/pkg/common"
"search_server/requests"
"search_server/service"
"strings"
)
//查询联营型号是否有库存
func CheckHasStock(c *gin.Context) {
noRule, _ := c.GetPostForm("no_rule")
if noRule != "1122" {
flag := common.CheckSignApi()
if flag > 0 {
common.Output(flag, "验证不通过:", nil)
return
}
}
goodsName, _ := c.GetPostForm("goods_name")
if goodsName == "" {
common.Output(1001, "型号不存在", nil)
return
}
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName = replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
otherService := service.OtherService{}
hasStock, err := otherService.CheckHasStock(goodsName)
if err != nil {
common.Output(1, "查询失败,系统报错"+err.Error(), nil)
return
}
common.Output(0, "查询成功:"+goodsName, hasStock)
return
}
//精确搜索型号名称
func ExactGoods(c *gin.Context) {
var r requests.ExactGoodsRequest
c.ShouldBind(&r)
if r.GoodsName == "" {
common.Output(1, "查询型号名称不得为空", nil)
return
}
//查询条件
otherService := service.OtherService{}
otherService.ExactGoods(r.SupplierId, r.GoodsName)
}
package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
"search_server/pkg/common"
"search_server/pkg/es"
"search_server/requests"
"search_server/service"
"strconv"
"strings"
)
//提供日志接口给日志系统
func SearchLogIndex(c *gin.Context) {
request := make(map[string]string, 0)
c.MultipartForm()
for name, value := range c.Request.Form {
if value[0] != "" {
request[name] = strings.TrimSpace(value[0])
}
}
searchLogService := service.SearchLogService{}
result, err := searchLogService.GetIndexData(request)
if err != nil {
common.ReturnData(1, "获取ES数据失败 : "+err.Error(), nil)
}
common.ReturnData(0, "", result)
return
}
func Scroll(c *gin.Context) {
var r requests.ScrollRequest
c.ShouldBind(&r)
if r.ScrollId == 0 {
common.ReturnData(1, "", nil)
}
params := make(map[string]interface{})
if r.Scroll > 0 {
params["scroll"] = strconv.Itoa(r.Scroll) + "m"
} else {
params["scroll"] = "5m"
}
params["scroll_id"] = r.ScrollId
paramsStr, _ := json.Marshal(params)
_, err := es.ScrollES(string(paramsStr))
if err != nil {
common.ReturnData(1, "Scroll失败 : "+err.Error(), nil)
}
}
//根据搜索日志统计出时间短访问最多的ip'
func SumIp(c *gin.Context) {
//统计品牌下的商品数量
flag := common.CheckSignApi()
if flag > 0 {
common.ReturnData(flag, "", nil)
return
}
var r requests.SumIpRequest
c.ShouldBind(&r)
searchLogService := service.SearchLogService{}
res, err := searchLogService.GetSumIp(r)
if err != nil {
common.ReturnData(1, err.Error(), nil)
return
}
common.ReturnData(0, "", res)
return
}
package dao
//func GetCourseList() *gorm.DB {
// return mysql.GetDB().Table(vars.Table_CourseMain).
// Order("course_id desc").Limit(3)
//}
//
//const course_list = "select * from course_main order by course_id desc limit ?"
//
//func GetCourseListBySql(args ...interface{}) *gorm.DB {
// return mysql.GetDB().Raw(course_list, args...)
//}
//func GetCourseDetail(course_id int) *gorm.DB {
// return mysql.GetDB().Table(vars.Table_CourseMain).Where("course_id=?", course_id)
//}
//
////取计数表
//func GetCourseCounts(course_id int) *gorm.DB {
// return mysql.GetDB().Table(vars.Table_CourseCounts).Where("course_id=?", course_id)
//}
......@@ -51,7 +51,7 @@ type ApiGoods struct {
ScmBrandName string `json:"scm_brand_name,omitempty"`
AllowCoupon int `json:"allow_coupon"`
ClassId1Name string `json:"class_id1_name"`
CLassId2Name string `json:"c_lass_id2_name"`
CLassId2Name string `json:"class_id2_name"`
SpuId string `json:"spu_id,omitempty"`
BatchSn string `json:"batch_sn"`
Canal string `json:"canal"`
......
package model
type SearchLogHits struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Score int64 `json:"_score"`
Source SearchLogSource `json:"_source"`
}
type SearchLogSource struct {
IP int64 `json:"ip"`
UserID int64 `json:"user_id"`
UserSign string `json:"user_sign"`
Ptag string `json:"ptag"`
BrowserInfo string `json:"browser_info"`
CreateTime int64 `json:"create_time"`
Keyword string `json:"keyword"`
Flag int64 `json:"flag"`
Platform int64 `json:"platform"`
Adtag string `json:"adtag"`
}
......@@ -196,7 +196,8 @@ func CopyMapString(distmap map[string]string) map[string]string {
}
//反爬虫加密验证
func CheckSignApi(params map[string]string) (resNo int) {
func CheckSignApi() (resNo int) {
params := make(map[string]string)
ctx := middleware.Context
if ctx == nil {
return
......
......@@ -61,8 +61,25 @@ func Output(errCode int, errMsg string, data interface{}) {
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
//跨域请求头设置
ctx.JSONP(200, response)
ctx.JSON(200, response)
}
}
//简单的返回数据方法
func ReturnData(errCode int, errMsg string, data interface{}) {
if data == nil {
data = []string{}
if errCode == 0 {
errCode = 1
}
}
response := Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
ctx := middleware.Context
ctx.JSON(200, response)
}
//错误的搜索日志记录
......
......@@ -25,5 +25,11 @@ func BuildRedisConfgs() (RedisDatabaseMap map[string]RedisDatabase) {
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"api_redis": {
Host: Get("api_redis.host").String(),
Password: Get("api_redis.password").String(),
MaxIdle: Get("api_redis.max_idle").String(),
MaxActive: Get("api_redis.max_active").String(),
},
}
}
......@@ -70,6 +70,8 @@ func BulkES(param string) (result string, err error) {
result = resp.String()
return
}
//多条件多索引查询,需要查询的索引和条件自己拼接成queryJson里面
func CurlESMSearch(queryJson string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",")
//随机获取一个节点进行请求
......@@ -83,3 +85,18 @@ func CurlESMSearch(queryJson string) (result string, err error) {
result = resp.String()
return
}
//滚动搜索和索引之间的文档重索引
func ScrollES(param string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",")
//随机获取一个节点进行请求
req.Debug = false
esUrl := endpoints[rand.Intn(len(endpoints))]
params := req.BodyJSON(param)
resp, err := req.Post(esUrl+"/_search/scroll", params)
if err != nil {
return
}
result = resp.String()
return
}
package requests
type ExactGoodsRequest struct {
SupplierId string `json:"supplier_id"`
GoodsName string `json:"goods_name"`
}
package requests
type SumIpRequest struct {
StartTime int64 `form:"start_time"`
EndTime int64 `form:"end_time"`
Size int `form:"size"`
}
type ScrollRequest struct {
Scroll int `form:"scroll"`
ScrollId int `form:"scroll_id"`
}
......@@ -4,7 +4,6 @@ import (
"github.com/gin-gonic/gin"
"search_server/controller"
"search_server/middleware"
"search_server/pkg/common"
)
//初始化路由
......@@ -15,7 +14,6 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery())
r.Use(middleware.Cors())
r.Use(middleware.ContextVars())
//路由
r.POST("/search/bom/autospu", controller.AutoSpu)
r.POST("/search/bom/recommend", controller.Recommend)
......@@ -34,9 +32,19 @@ func InitRouter() *gin.Engine {
r.GET("/search/get_data", controller.GetDataIndex)
r.POST("/search/get_data", controller.GetDataIndex)
r.GET("/test", func(c *gin.Context) {
common.Output(1000, "testest", nil)
})
//Other控制器相关
r.POST("/search/other/hasStock", controller.CheckHasStock)
r.GET("/search/other/exactGoods", controller.ExactGoods)
r.POST("/search/other/exactGoods", controller.ExactGoods)
//SearchLog控制器相关
r.GET("/search/search_log/sumIp", controller.SumIp)
r.POST("/search/search_log/sumIp", controller.SumIp)
r.POST("/search/search_log/scroll", controller.Scroll)
r.GET("/search/search_log/index", controller.SearchLogIndex)
r.POST("/search/search_log/index", controller.SearchLogIndex)
r.GET("/search/search_log", controller.SearchLogIndex)
r.POST("/search/search_log", controller.SearchLogIndex)
return r
}
......@@ -40,8 +40,17 @@ func GetGoodsInfoByApi(goodsIdsStr string) (goodsList []model.ApiGoods, err erro
//isMap:是否以字典形式返回值,默认是数组
func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiGoods, goodsListMap map[string]model.ApiGoods, err error) {
goodsIdList := strings.Split(goodsIdsStr, ",")
if len(goodsIdList) == 0 {
return
}
//req.Debug = true
goodsServerUrl := config.Get("goods.api_url").String()
var goodsServerUrl string
if len(goodsIdList[0]) > 7 {
goodsServerUrl = config.Get("goods.api_url").String()
}else{
goodsServerUrl = config.Get("goods.sz_api_url").String()
}
resp, err := req.Post(goodsServerUrl+"/synchronization", params)
if err != nil {
return
......@@ -50,7 +59,6 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG
//先判断返回的data是不是字典,不是字典代表可能是返回字符串了
if gjson.Get(resp.String(), "data").IsObject() {
//排序操作
goodsIdList := strings.Split(goodsIdsStr, ",")
for _, goodsId := range goodsIdList {
for _, data := range gjson.Get(resp.String(), "data").Map() {
if goodsId == data.Get("goods_id").String() {
......@@ -133,7 +141,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG
goods.LadderPrice = ladderPrice
//other_attrs
goods.OtherAttrs.GrossWeight = data.Get("other_attrs.gross_weight").String()
goods.OtherAttrs.GrossWeight = data.Get("other_attrs.gross_wegiht").String()
//Attrs
attrMap := make(map[string]model.Attr, 0)
......@@ -193,7 +201,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG
func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
//判断新客价
if userId != 0 {
redisConn := gredis.Conn("search_r")
redisConn := gredis.Conn("api_redis")
defer redisConn.Close()
userInfoStr, err := redis.String(redisConn.Do("HGET", "api_user", userId))
if err != nil {
......
package service
import (
"fmt"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"regexp"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/service/query"
"strings"
)
type OtherService struct {
}
func (os *OtherService) CheckHasStock(goodsName string) (hasStock int, err error) {
//获取所有供应商名称
supplierAllSlice := config.GetSectionValues("SUPPLIER_ALL")
index := strings.Join(supplierAllSlice, ",")
queryString := query.GetCheckStockQuery(goodsName)
esResult, err := es.CurlES(index, queryString)
if err != nil {
return
}
fmt.Println(queryString)
//获取是否有结果,有结果就是有库存
total := gjson.Get(esResult, "hits.total").Int()
if total > 0 {
hasStock = 1
}
return
}
func (os *OtherService) ExactGoods(supplierId, goodsName string) {
//获取索引
index := getExactGoodsIndex(supplierId)
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文和特殊字符串
removeCnGoodsName := r1.ReplaceAllString(goodsName, "")
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
goodsName = r2.ReplaceAllString(removeCnGoodsName, "")
//查询条件
queryString := query.GetExactGoodsQuery(goodsName)
esResult, err := es.CurlES(index, queryString)
if err != nil {
return
}
//最终结果接受者
var result interface{}
//所有供应商
if supplierId == "-1" {
var goodsList []interface{}
for _, jsonItem := range gjson.Get(esResult, "hits.hits").Array() {
goods := make(map[string]string)
goods["_index"] = jsonItem.Get("_index").String()
goods["goods_id"] = jsonItem.Get("_source.goods_id").String()
goods["old_goods_id"] = jsonItem.Get("_source.old_goods_id").String()
goodsList = append(goodsList, goods)
}
result = goodsList
} else {
//获取所有的商品id
ids := gjson.Get(esResult, "hits.hits.#._id").Array()
var goodsIds []string
for _, id := range ids {
goodsIds = append(goodsIds, id.String())
}
result = goodsIds
}
common.Output(0, "", result)
return
}
//获取这个方法对应的索引
func getExactGoodsIndex(supplierId string) (index string) {
supplierAllSlice := config.GetSectionValues("SEARCH_SUPPLIER")
supplierAllMap := config.Cfg.Section("SEARCH_SUPPLIER").KeysHash()
searchSupplierIds := config.Cfg.Section("SEARCH_SUPPLIER").KeyStrings()
if supplierId == "-1" {
index = strings.Join(supplierAllSlice, ",") + "," + config.Get("ZIYING_CONFIG.Index").String()
} else {
if php2go.InArray(supplierId, searchSupplierIds) {
index = supplierAllMap[supplierId]
} else {
//不存在就去搜索自营的
index = config.Get("ZIYING_CONFIG.Index").String()
}
}
return
}
package query
import (
"gopkg.in/olivere/elastic.v5"
)
//获取查询语句
func GetCheckStockQuery(goodsName string) (queryString string) {
query := elastic.NewBoolQuery()
query.Filter(elastic.NewRangeQuery("stock").Gt(0))
query.Filter(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
source := elastic.NewSearchSource()
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_name", "stock"))
source.Size(1)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
queryString, _ = searchRequest.Body()
return
}
//精确匹配商品的查询语句
func GetExactGoodsQuery(goodsName string) (queryString string) {
query := elastic.NewBoolQuery()
query.Must(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
source := elastic.NewSearchSource()
source.From(0)
source.Size(200)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source).Preference("_primary_first")
queryString, _ = searchRequest.Body()
return
}
......@@ -191,7 +191,7 @@ func GetGoodsBySupplierIdQuery(r requests.GetGoodsBySupplierRequest) (string, er
//判断关键词是否为空
if r.Keyword != "" {
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文?
//去除中文
removeCnKeyword := r1.ReplaceAllString(r.Keyword, "")
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
r.Keyword = r2.ReplaceAllString(removeCnKeyword, "")
......
package query
import (
"github.com/syyongx/php2go"
"gopkg.in/olivere/elastic.v5"
"search_server/requests"
"strconv"
"strings"
)
func GetSumIpQuery(r requests.SumIpRequest) (queryString string) {
query := elastic.NewBoolQuery()
source := elastic.NewSearchSource()
query.Must(elastic.NewRangeQuery("create_time").Gte(r.StartTime).Lte(r.EndTime))
source.Aggregation("tatol", elastic.NewTermsAggregation().Field("ip").Size(100))
source.Size(0)
source.Query(query)
queryRequest := elastic.NewSearchRequest().Source(source)
queryString, _ = queryRequest.Body()
return
}
func GetSearchLogIndexQuery(request map[string]string) (queryString string) {
page, _ := strconv.Atoi(request["p"])
if page == 0 {
page = 1
}
//需要转换成大写的字段
needUpperField := []string{"keyword"}
//开始构建ES的查询条件
query := elastic.NewBoolQuery()
source := elastic.NewSearchSource()
for name, value := range request {
//为什么要切割这个参数名呢,因为传上来的参数名,是类似
//goods_name/condition , stock/range 这样子的形式
fieldNameAndCondition := strings.Split(name, "/")
if len(fieldNameAndCondition) == 2 {
filedName := fieldNameAndCondition[0]
condition := fieldNameAndCondition[1]
if condition == "condition" {
//es的term条件数组
//用interface{}类型主要是为了下面构建查询条件方便
termSlice := []interface{}{value}
if filedName != "keyword" && filedName != "browser_info" && filedName != "adtag" {
if filedName == "ip" {
value = strconv.Itoa(int(php2go.IP2long(value)))
}
termSlice[0] = value
} else {
//判断参数名是否属于需要转换成大写的字段
if php2go.InArray(filedName, needUpperField) {
if request["is_exact"] != "" {
termSlice[0] = value
termSlice = append(termSlice, strings.ToLower(value))
termSlice = append(termSlice, strings.ToUpper(value))
} else {
termSlice[0] = strings.ToUpper(value)
}
}
}
//判断is_exact字段
if request["is_exact"] != "" {
query.Must(elastic.NewTermsQuery("keyword.raw", termSlice...))
} else {
query.Must(elastic.NewTermsQuery(filedName, termSlice...))
}
}
hasOrder := false
//排序相关的参数
if condition == "order" {
hasOrder = true
//默认正序
orderFlag := true
if value == "false" || value == "desc" || value == "-1" {
orderFlag = false
}
source.Sort(filedName, orderFlag)
}
//范围相关的查询
if condition == "range" {
rangeCondition := strings.Split(value, ",")
if len(rangeCondition) >= 2 {
gteValue := rangeCondition[0]
lteValue := rangeCondition[1]
query.Must(elastic.NewRangeQuery(filedName).Gte(gteValue).Lte(lteValue))
}
}
//单边范围查询
if condition == "sr" {
rangeCondition := strings.Split(value, ",")
if len(rangeCondition) >= 2 {
}
}
//如果没有排序,就开启默认排序
if !hasOrder {
source.Sort("create_time", false)
}
if page > 1000 {
page = 1000
}
//获取size
size := 10
if request["offset"] != "" {
size, _ = strconv.Atoi(request["offset"])
}
start := (page - 1) * size
source.From(start)
source.Size(size)
}
}
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
queryString, _ = searchRequest.Body()
return
}
package service
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"github.com/uniplaces/carbon"
"search_server/model"
"search_server/pkg/es"
"search_server/requests"
"search_server/service/query"
"strconv"
)
type SearchLogService struct {
}
//获取index方法的数据
func (sls *SearchLogService) GetIndexData(request map[string]string) (result map[string]interface{}, err error) {
//索引/类型
index := "keyword_search_log_1/log"
var esResult string
_, exist := request["scroll_id"]
if exist || request["scroll_id"] == "" {
queryString := query.GetSearchLogIndexQuery(request)
esResult, err = es.CurlES(index, queryString)
if err != nil {
return
}
} else {
params := make(map[string]string)
scroll, _ := strconv.Atoi(request["scroll"])
if scroll > 0 {
params["scroll"] = params["scroll"] + "m"
} else {
params["scroll"] = "5m"
}
params["scroll_id"] = request["scroll_id"]
paramsStr, _ := json.Marshal(params)
esResult, err = es.ScrollES(string(paramsStr))
if err != nil {
return
}
}
fmt.Println(esResult)
result = make(map[string]interface{})
//循环遍历出数据
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["scroll_id"] = gjson.Get(esResult, "_scroll_id").String()
sources := gjson.Get(esResult, "hits.hits.#_source").Array()
var list []map[string]interface{}
for _, source := range sources {
listItem := make(map[string]interface{})
var hits model.SearchLogHits
var platform string
_ = json.Unmarshal([]byte(source.String()), &hits)
if hits.Source.Platform == 1 {
platform = "PC"
} else if hits.Source.Platform == 2 {
platform = "H5"
} else {
platform = ""
}
listItem["create_time"] = hits.Source.CreateTime
listItem["user_id"] = hits.Source.UserID
listItem["flag"] = hits.Source.Flag
listItem["ip"] = hits.Source.IP
listItem["keyword"] = hits.Source.Keyword
listItem["browser_info"] = hits.Source.BrowserInfo
listItem["platform"] = platform
listItem["adtag"] = hits.Source.Adtag
listItem["ptag"] = hits.Source.Ptag
listItem["user_sign"] = hits.Source.UserSign
list = append(list, listItem)
}
result["list"] = list
return
}
func (sls *SearchLogService) GetSumIp(r requests.SumIpRequest) (res []map[string]interface{}, err error) {
//默认24小时
if r.StartTime == 0 {
r.StartTime = carbon.Now().SubDays(1).Timestamp()
}
if r.EndTime == 0 {
r.EndTime = carbon.Now().Timestamp()
}
if r.Size == 0 {
r.Size = 100
}
if r.Size > 20000 {
r.Size = 20000
}
queryString := query.GetSumIpQuery(r)
fmt.Println(queryString)
index := "keyword_search_log_1"
esResult, err := es.CurlES(index, queryString)
if err != nil {
return
}
buckets := gjson.Get(esResult, "aggregations.tatol.buckets").Array()
for _, bucket := range buckets {
item := map[string]interface{}{
"ip": bucket.Map()["key"].String(),
"num": bucket.Map()["doc_count"].Int(),
}
res = append(res, item)
}
return
}
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