Commit 603ad1e3 by huangchengyi

es

parent 03792ca1
...@@ -3,10 +3,8 @@ package main ...@@ -3,10 +3,8 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"fmt"
"search_server/boot" "search_server/boot"
"search_server/pkg/gredis" "search_server/pkg/es"
"search_server/service"
) )
func main() { func main() {
...@@ -26,16 +24,21 @@ func main() { ...@@ -26,16 +24,21 @@ func main() {
//fmt.Println(flag) //fmt.Println(flag)
//os.Exit(1) //os.Exit(1)
redisConn := gredis.Conn("search_r") //redisConn := gredis.Conn("search_r")
defer redisConn.Close() //defer redisConn.Close()
attrName, err := gredis.String(redisConn.Do("GET", "hcy1")) //attrName, err := gredis.String(redisConn.Do("GET", "hcy1"))
//
fmt.Println(err) //fmt.Println(err)
fmt.Println(attrName) //fmt.Println(attrName)
//os.Exit(1) //os.Exit(1)
dd := service.OutLink("LM358","-1") //dd := service.OutLink("LM358","-1")
print("niin") result,err := es.BulkES(`{ "index":{"_index":"hcy1","_type":"goods","_id":"8888888"} }
print(dd) { "name":"john doe","age":25 }
{ "index":{"_index":"hcy1","_type":"goods","_id":"9999999"} }
{ "name":"mary smith","age":32 }`)
println(result,err)
os.Exit(1) os.Exit(1)
} }
......
...@@ -31,3 +31,29 @@ func CurlES(index string, param string) (result string, err error) { ...@@ -31,3 +31,29 @@ func CurlES(index string, param string) (result string, err error) {
result = resp.String() result = resp.String()
return return
} }
/*
批量插入 或者 更新 es
POST /_bulk
{ "index":{"_index":"hcy1","_type":"goods","_id":"333333"} }
{ "name":"john doe","age":25 }
{ "index":{"_index":"hcy1","_type":"goods","_id":"6666"} }
{ "name":"mary smith","age":32 }
eg:
es.BulkES(`{"index":{"_index":"hcy1","_type":"goods","_id":"8888888"} }
{ "name":"john doe","age":25 }
{ "index":{"_index":"hcy1","_type":"goods","_id":"9999999"} }
{ "name":"mary smith","age":32 }`)
*/
func BulkES(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.Post(esUrl+"/_bulk", params)
if err != nil {
return
}
result = resp.String()
return
}
\ No newline at end of file
package service package service
/*
等同于 php.search_server.middleBaseController
联营请求外链后 更新或者新增到平台 (es,redis,rabmq)
*/
import ( import (
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go" "github.com/syyongx/php2go"
...@@ -19,14 +22,8 @@ type CommonLyService struct { ...@@ -19,14 +22,8 @@ type CommonLyService struct {
supplierId int64 //调用当前方法的供应商ID supplierId int64 //调用当前方法的供应商ID
supplierName string //调用当前方法的供应商名称 supplierName string //调用当前方法的供应商名称
skuEsUpdataList []map[string]interface{} //组装批量更新es数据 skuEsUpdataList []map[string]interface{} //组装批量更新es数据
SKU_UNIQUE_JUDGE string // spuId string //spuId
SPU_UNIQUE_JUDGE string // skuId string //skuId
BRAND_NAME_ALL string //
SUPPLIER_BRAND_ALL string //
BRAND string //
SKU string //
SPU string
SKU_RAW_MAP string
} }
//初始化类 //初始化类
...@@ -40,15 +37,7 @@ func (t *CommonLyService) LyServerRun(){ ...@@ -40,15 +37,7 @@ func (t *CommonLyService) LyServerRun(){
"tme":3600, "tme":3600,
"buerklin":3600, "buerklin":3600,
} }
//用到的redis键值
CommonLyService.SKU_UNIQUE_JUDGE = config.Get("redis_all.SKU_UNIQUE_JUDGE").String() //
CommonLyService.SPU_UNIQUE_JUDGE = config.Get("redis_all.SPU_UNIQUE_JUDGE").String() //
CommonLyService.BRAND_NAME_ALL = config.Get("redis_all.BRAND_NAME_ALL").String() //
CommonLyService.SUPPLIER_BRAND_ALL = config.Get("redis_all.SUPPLIER_BRAND_ALL").String() //
CommonLyService.BRAND = config.Get("redis_all.BRAND").String() //
CommonLyService.SKU = config.Get("redis_all.SKU").String() //
CommonLyService.SPU = config.Get("redis_all.SPU").String()
CommonLyService.SKU_RAW_MAP = config.Get("redis_all.SKU_RAW_MAP").String()
} }
/* /*
...@@ -71,7 +60,7 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo ...@@ -71,7 +60,7 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo
for goods_sn, info := range goodsList { for goods_sn, info := range goodsList {
snSku := php2go.Md5(strings.ToLower(goods_sn)) snSku := php2go.Md5(strings.ToLower(goods_sn))
skuId,_ := gredis.String(redisRead.Do("HGET",t.SKU_UNIQUE_JUDGE,snSku)) //查询唯一值,反查sku_id skuId,_ := gredis.String(redisRead.Do("HGET",config.Get("redis_all.SKU_UNIQUE_JUDGE").String(),snSku)) //查询唯一值,反查sku_id
var sku_flag bool = false; //是否新增或者更新db+redis,为true则新增 var sku_flag bool = false; //是否新增或者更新db+redis,为true则新增
if skuId == "" { //为空,先创建sku if skuId == "" { //为空,先创建sku
...@@ -102,7 +91,7 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str ...@@ -102,7 +91,7 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str
defer redisWrite.Close() defer redisWrite.Close()
spuKey := common.GetKey(skuInfo.GoodsName+"_"+skuInfo.BrandName) spuKey := common.GetKey(skuInfo.GoodsName+"_"+skuInfo.BrandName)
spuId,_ := gredis.String(redisWrite.Do("HGET",t.SKU_UNIQUE_JUDGE,spuKey)) spuId,_ := gredis.String(redisWrite.Do("HGET",config.Get("redis_all.SKU_UNIQUE_JUDGE").String(),spuKey))
if spuId == "" { //不存在spu,创建新的spu if spuId == "" { //不存在spu,创建新的spu
spuId = t.writeSpuInfo(skuInfo) spuId = t.writeSpuInfo(skuInfo)
} }
...@@ -134,11 +123,11 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str ...@@ -134,11 +123,11 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str
redisWrite.Do("HSET","sku",spuId,jsonStr) redisWrite.Do("HSET","sku",spuId,jsonStr)
skuKey := common.GetKey(skuInfo.GoodsSn+"_"+strconv.FormatInt(t.supplierId,10)) skuKey := common.GetKey(skuInfo.GoodsSn+"_"+strconv.FormatInt(t.supplierId,10))
redisWrite.Do("HSET",t.SKU_UNIQUE_JUDGE,skuKey,skuId) redisWrite.Do("HSET",config.Get("redis_all.SKU_UNIQUE_JUDGE").String(),skuKey,skuId)
//todo 如果是digikey新增,需要插入原始映射表 //todo 如果是digikey新增,需要插入原始映射表
if t.supplierId == 7 { if t.supplierId == 7 {
redisWrite.Do("HSET",t.SKU_RAW_MAP,skuKey,skuId) redisWrite.Do("HSET",config.Get("redis_all.SKU_RAW_MAP").String(),skuKey,skuId)
} }
} }
...@@ -151,9 +140,9 @@ func (t *CommonLyService) writeSpuInfo(skuInfo *model.LyClearGoodsList) string { ...@@ -151,9 +140,9 @@ func (t *CommonLyService) writeSpuInfo(skuInfo *model.LyClearGoodsList) string {
defer redisWrite.Close() defer redisWrite.Close()
brand_key := common.GetKey(strings.ToLower(skuInfo.BrandName)) brand_key := common.GetKey(strings.ToLower(skuInfo.BrandName))
brandId,_ := redis.Int64(redisWrite.Do("HGET", t.BRAND_NAME_ALL,brand_key)) brandId,_ := redis.Int64(redisWrite.Do("HGET", config.Get("redis_all.BRAND_NAME_ALL").String(),brand_key))
if brandId == 0 { if brandId == 0 {
brandId,_ = redis.Int64(redisWrite.Do("HGET",t.SUPPLIER_BRAND_ALL,brand_key)) brandId,_ = redis.Int64(redisWrite.Do("HGET",config.Get("redis_all.SUPPLIER_BRAND_ALL").String(),brand_key))
if brandId == 0 { if brandId == 0 {
brandId = t.writeBrandInfo(skuInfo) brandId = t.writeBrandInfo(skuInfo)
} }
...@@ -178,7 +167,7 @@ func (t *CommonLyService) writeSpuInfo(skuInfo *model.LyClearGoodsList) string { ...@@ -178,7 +167,7 @@ func (t *CommonLyService) writeSpuInfo(skuInfo *model.LyClearGoodsList) string {
redisWrite.Do("HSET","spu",spuId,jsonStr) redisWrite.Do("HSET","spu",spuId,jsonStr)
spuKey := common.GetKey(skuInfo.GoodsName+"_"+skuInfo.BrandName) spuKey := common.GetKey(skuInfo.GoodsName+"_"+skuInfo.BrandName)
redisWrite.Do("HSET",t.SPU_UNIQUE_JUDGE,spuKey,spuId) redisWrite.Do("HSET",config.Get("redis_all.SPU_UNIQUE_JUDGE").String(),spuKey,spuId)
return spuId return spuId
} }
...@@ -210,15 +199,15 @@ func (t *CommonLyService) writeBrandInfo(skuInfo *model.LyClearGoodsList) int64 ...@@ -210,15 +199,15 @@ func (t *CommonLyService) writeBrandInfo(skuInfo *model.LyClearGoodsList) int64
brandId,_ = results.LastInsertId() brandId,_ = results.LastInsertId()
} }
//更新redis 品牌数据 //更新redis 品牌数据
redisWrite.Do("HSET",t.BRAND_NAME_ALL,brandkey,brandId) redisWrite.Do("HSET",config.Get("redis_all.BRAND_NAME_ALL").String(),brandkey,brandId)
redisWrite.Do("HSET",t.BRAND,brandkey,brandId) redisWrite.Do("HSET",config.Get("redis_all.BRAND").String(),brandkey,brandId)
return brandId; return brandId;
} }
/* /*
组装推送到es数据 组装推送到es数据
*/ */
func esSkuInfo(skuInfo *model.LyClearGoodsList) { func esSkuInfo(skuInfo *model.LyClearGoodsList,brandId *string) {
} }
......
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