Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8b1ecac0
authored
Jul 29, 2020
by
孙龙
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
up
parent
3b8428c7
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
28 deletions
conf/local/config.ini.local
controller/ziyin_controller.go
routes/router.go
service/ziyin_service.go
test/test7.go
conf/local/config.ini.local
View file @
8b1ecac0
...
...
@@ -46,9 +46,9 @@ is_sz = true
#自营mongodb缓存表 正式线ziying_class_aggs
[AGG_TABLE]
agg_table =
sz_
ziying_class_aggs
agg_table = ziying_class_aggs
#自营mongodb缓存表 正式线 ziying_first_lists
[FIRSTLISTS_TABLE]
firstLists_table = sz_ziying_first_lists
\ No newline at end of file
firstLists_table = ziying_first_lists
\ No newline at end of file
controller/ziyin_controller.go
View file @
8b1ecac0
...
...
@@ -36,6 +36,33 @@ func Zyh(ctx *gin.Context){
ctx
.
JSON
(
200
,
zyhResponse
)
}
func
Aggs
(
ctx
*
gin
.
Context
){
zyhRequest
:=
make
(
map
[
string
]
string
,
0
);
ctx
.
MultipartForm
()
for
requestName
,
requstValue
:=
range
ctx
.
Request
.
Form
{
if
requstValue
[
0
]
!=
""
{
zyhRequest
[
requestName
]
=
strings
.
TrimSpace
(
requstValue
[
0
])
}
}
//fmt.Println(zyhRequest)
zyServiceImpl
:=
service
.
NewZyServiceImpl
()
zyhResponse
:=
&
model
.
ZyhResponse
{}
zyhResponse
,
err
:=
zyServiceImpl
.
Aggs
(
ctx
,
zyhRequest
)
if
err
!=
nil
{
zyhResponse
.
ErrorCode
=
1
zyhResponse
.
ErrorMsg
=
fmt
.
Sprintf
(
"%s"
,
err
)
common
.
Output
(
int
(
zyhResponse
.
ErrorCode
),
zyhResponse
.
ErrorMsg
,
zyhResponse
.
Data
)
//ctx.JSON(200,zyhResponse)
}
//fmt.Printf("%+v",zyhResponse)
common
.
Output
(
int
(
zyhResponse
.
ErrorCode
),
zyhResponse
.
ErrorMsg
,
zyhResponse
.
Data
)
//ctx.JSON(200,zyhResponse)
}
func
Index
(
ctx
*
gin
.
Context
){
zyhRequest
:=
make
(
map
[
string
]
string
,
0
);
zyhResponse
:=
&
model
.
ZyhResponse
{}
...
...
routes/router.go
View file @
8b1ecac0
...
...
@@ -19,6 +19,8 @@ func InitRouter() *gin.Engine {
r
.
GET
(
"search/ZiYing/zyh"
,
controller
.
Zyh
)
r
.
GET
(
"search/ZiYing/index"
,
controller
.
Index
)
r
.
POST
(
"search/ZiYing/index"
,
controller
.
Index
)
r
.
GET
(
"search/ZiYing/aggs"
,
controller
.
Aggs
)
r
.
POST
(
"search/ZiYing/aggs"
,
controller
.
Aggs
)
r
.
POST
(
"search/mouser/a"
,
controller
.
GetMouserData
)
r
.
GET
(
"search/mouser/a"
,
controller
.
GetMouserData
)
...
...
service/ziyin_service.go
View file @
8b1ecac0
...
...
@@ -40,7 +40,7 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo
need_aggs
bool
_type
byte
)
isAdmin
=
fals
e
isAdmin
=
tru
e
if
req
[
"need_aggs"
]
==
"1"
{
need_aggs
=
true
}
else
{
...
...
@@ -53,12 +53,29 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo
}
else
{
_type
=
3
}
rsp
,
ok
:=
ziyingCache
(
req
,
_type
)
if
ok
{
return
rsp
,
nil
}
//设置cookie
setCookie
(
ctx
,
req
)
rsp
=
zy
(
req
,
_type
,
0
)
return
rsp
,
nil
}
func
(
this
*
ZyServiceImpl
)
Aggs
(
ctx
*
gin
.
Context
,
req
map
[
string
]
string
)
(
rsp
*
model
.
ZyhResponse
,
err
error
){
//rsp,ok := ziyingCache(req,2)
//if ok {
// return rsp,nil
//}
rsp
=
zy
(
req
,
2
,
1
)
return
rsp
,
nil
}
func
NewZyServiceImpl
()
*
ZyServiceImpl
{
return
&
ZyServiceImpl
{}
}
...
...
@@ -136,15 +153,24 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
zy_orgin_search
=
common
.
CopyMapString
(
req
)
//原始搜索數據
delete
(
req2
,
"class_id/condition"
)
rsp
=
searchZiYingGoods
(
req2
,
true
,
zhyClassList
,
classLight
,
_type
)
//fmt.Printf("返回數據:%+v",rsp)
if
_type
==
2
{
data
,
ok
:=
rsp
.
Data
.
(
map
[
string
]
interface
{})
if
ok
{
delete
(
data
,
"class"
)
delete
(
data
,
"class_light"
)
}
}
return
rsp
}
func
(
this
*
ZyServiceImpl
)
Index
(
ctx
*
gin
.
Context
,
req
map
[
string
]
string
)
(
rsp
*
model
.
ZyhResponse
){
req2
:=
req
zy_orgin_search
=
common
.
CopyMapString
(
req
)
//原始搜索數據
xx
:=
ziyingCache
(
req
,
1
)
fmt
.
Println
(
xx
)
rsp
,
ok
:=
ziyingCache
(
req
,
3
)
//fmt.Println(ok)
if
ok
{
return
rsp
}
rsp
=
searchZiYingGoods
(
req2
,
false
,
nil
,
nil
,
1
)
return
rsp
}
...
...
@@ -308,13 +334,13 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
source
.
Query
(
query
)
queryJson
,
_
:=
elastic
.
NewSearchRequest
()
.
Source
(
source
)
.
Body
()
fmt
.
Println
(
queryJson
)
logger
.
Info
(
"es搜索查詢條件 %s"
,
queryJson
)
result
,
err
:=
es
.
CurlES
(
config
.
Get
(
"ZIYING_CONFIG.Index"
)
.
String
(),
queryJson
)
if
err
!=
nil
{
log
.
Error
(
err
)
}
//fmt.Println(result)
logger
.
Info
(
result
)
logger
.
Info
(
"查詢結果:%s"
,
result
)
//单独返回goods_id
if
just_id
,
ok
:=
req
[
"just_id"
];
ok
&&
just_id
==
"2"
{
hits2
:=
gjson
.
Get
(
result
,
"hits.hits.#._source"
)
.
Array
()
...
...
@@ -354,6 +380,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
aggregationsKeyBuckets
:=
v
.
Map
()
id
:=
aggregationsKeyBuckets
[
"key"
]
.
String
()
aggregationsKeyBucketsStr
:=
""
aggregationsKeyBucketsStr
=
aggregationsKeyBuckets
[
"key"
]
.
String
()
switch
field
{
case
"brand_id"
:
brand_j
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_Brand"
,
aggregationsKeyBuckets
[
"key"
]))
...
...
@@ -366,6 +393,8 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
arr_oj
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_SelfClassInfo"
,
aggregationsKeyBuckets
[
"key"
]))
if
err
==
nil
{
aggregationsKeyBucketsStr
=
gjson
.
Get
(
arr_oj
,
"class_name"
)
.
String
()
}
else
{
aggregationsKeyBucketsStr
=
""
}
case
"packing"
:
Self_UnitAliasInfo
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_UnitAlias"
,
aggregationsKeyBuckets
[
"key"
]))
...
...
@@ -395,21 +424,25 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
}
//attrs
attrsAttrNameBuckets
:=
gjson
.
Get
(
aggregations
[
"attrs"
]
.
String
(),
"attr_name.buckets"
)
.
Array
()
if
zy
&&
len
(
attrsAttrNameBuckets
)
>
0
{
//fmt.Println("attrsAttrNameBuckets",attrsAttrNameBuckets)
aggsFieldList
:=
make
([]
map
[
string
]
string
,
0
)
for
_
,
vbuckets
:=
range
attrsAttrNameBuckets
{
vbucketsMap
:=
vbuckets
.
Map
()
vbucketsMapKey
:=
vbucketsMap
[
"key"
]
.
String
()
attr_value_buckets
:=
gjson
.
Get
(
vbuckets
.
String
(),
"attr_value.buckets"
)
for
_
,
v
:=
range
attr_value_buckets
.
Map
(){
res
[
"aggs"
][
vbucketsMapKey
]
=
make
(
map
[
string
]
interface
{},
0
)
aggsFieldList
:=
make
([]
map
[
string
]
string
,
0
)
for
_
,
v
:=
range
attr_value_buckets
.
Array
(){
fieldMd5
:=
common
.
Md5
(
vbucketsMap
[
"key"
]
.
String
())
aggsFieldList
=
append
(
aggsFieldList
,
map
[
string
]
string
{
"name"
:
v
.
Map
()[
"key"
]
.
String
(),
"id"
:
v
.
Map
()[
"key"
]
.
String
(),
"num"
:
v
.
Map
()[
"doc_count"
]
.
String
(),
})
res
[
"aggs"
][
vbucketsMap
[
"key"
]
.
String
()
][
"list"
]
=
aggsFieldList
res
[
"aggs"
][
vbucketsMap
[
"key"
]
.
String
()
][
"name"
]
=
fieldMd5
[
8
:
24
]
res
[
"aggs"
][
vbucketsMap
Key
][
"list"
]
=
aggsFieldList
res
[
"aggs"
][
vbucketsMap
Key
][
"name"
]
=
fieldMd5
[
8
:
24
]
}
}
}
...
...
@@ -945,18 +978,21 @@ func dullZiYingGoodsInfo(goods_id_str string,keyword string,goods_name_arr []str
符合条件 直接读缓存,不需要再查es+redis
@param string type 获取缓存数据:1 单独获取列表数据(class_id第一页数据) 2 单独获取aggs统计 3 获取列表(class_id第一页数据)+aggs统计
*/
func
ziyingCache
(
req
map
[
string
]
string
,
_type
byte
)
(
bool
){
func
ziyingCache
(
req
map
[
string
]
string
,
_type
byte
)
(
*
model
.
ZyhResponse
,
bool
){
var
(
err
error
cache
bool
=
true
pInt
int
//page int
class_id
int
mongodb
*
mgo
.
Session
rsp
*
model
.
ZyhResponse
)
rsp
=
&
model
.
ZyhResponse
{
ErrorCode
:
0
,
ErrorMsg
:
"成功,数据更新时间: "
}
//是否开启缓存
if
isOpen
,
err
:=
config
.
Get
(
"ZIYING_CACHE.is_no_ziying_cache"
)
.
Bool
()
;
err
==
nil
&&
!
isOpen
{
return
false
return
rsp
,
false
}
p
:=
req
[
"p"
]
if
p
!=
""
{
...
...
@@ -977,7 +1013,7 @@ func ziyingCache(req map[string]string,_type byte) ( bool){
}
}
if
!
cache
{
//return
false
return
rsp
,
false
}
//需要查缓存
...
...
@@ -985,7 +1021,7 @@ func ziyingCache(req map[string]string,_type byte) ( bool){
if
class_idCondition
!=
""
{
class_id
,
_
=
strconv
.
Atoi
(
class_idCondition
)
}
//
agg_table := config.Get("AGG_TABLE.agg_table").String()
agg_table
:=
config
.
Get
(
"AGG_TABLE.agg_table"
)
.
String
()
firstLists_table
:=
config
.
Get
(
"FIRSTLISTS_TABLE.firstLists_table"
)
.
String
()
class_id
=
class_id
err
=
err
...
...
@@ -995,36 +1031,72 @@ func ziyingCache(req map[string]string,_type byte) ( bool){
defer
mongodb
.
Close
()
c
:=
mongodb
.
DB
(
"ichunt"
)
.
C
(
firstLists_table
)
//
c1 :=mongodb.DB("ichunt").C(agg_table)
c1
:=
mongodb
.
DB
(
"ichunt"
)
.
C
(
agg_table
)
//var res interface{}
type
res
struct
{
ID
bson
.
ObjectId
`json:"_id" bson:"_id"`
ClassId
int
`json:"class_id" bson:"class_id"`
//Data string
`json:"data",bson:"data"`
Data
interface
{}
`json:"data",bson:"data"`
UpdateTime
string
`json:"update_time" bson:"update_time"`
}
result
:=
res
{}
result2
:=
res
{}
switch
_type
{
case
1
:
//单独获取列表数据(class_id第一页数据)
err
:=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
fmt
.
Println
(
"err"
,
err
)
err
=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
if
err
==
nil
{
rsp
.
Data
=
result
.
Data
rsp
.
ErrorMsg
+=
result
.
UpdateTime
}
else
{
return
rsp
,
false
}
break
case
2
:
//#单独获取aggs
err
:=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
fmt
.
Println
(
"err"
,
err
)
err
=
c1
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
if
err
==
nil
{
rsp
.
Data
=
result
.
Data
rsp
.
ErrorMsg
+=
result
.
UpdateTime
}
else
{
return
rsp
,
false
}
break
case
3
:
//获取列表(class_id第一页数据)+aggs统计
err1
:=
c1
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result2
)
err2
:=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
if
err1
==
nil
&&
err2
==
nil
{
result2Data
:=
result2
.
Data
result2DataBson
,
ok
:=
result2Data
.
(
bson
.
M
)
if
!
ok
{
return
rsp
,
false
}
aggs
,
ok
:=
result2DataBson
[
"data"
]
.
(
bson
.
M
)
if
!
ok
{
return
rsp
,
false
}
aggsBson
:=
aggs
[
"aggs"
]
ziying_first_lists_data
,
ok
:=
result
.
Data
.
(
bson
.
M
)
if
!
ok
{
return
rsp
,
false
}
ziying_first_lists_data2
:=
ziying_first_lists_data
[
"data"
]
ziying_first_lists_data2_bson
,
ok
:=
ziying_first_lists_data2
.
(
bson
.
M
)
if
!
ok
{
return
rsp
,
false
}
}
ziying_first_lists_data2_bson
[
"aggs"
]
=
aggsBson
rsp
.
Data
=
ziying_first_lists_data2_bson
rsp
.
ErrorMsg
+=
result2
.
UpdateTime
return
true
}
else
{
return
rsp
,
false
}
}
return
rsp
,
true
}
\ No newline at end of file
test/test7.go
View file @
8b1ecac0
...
...
@@ -2,13 +2,16 @@ package main
import
(
"bytes"
"fmt"
"io"
)
const
debug
=
false
func
main
()
{
var
buf
*
bytes
.
Buffer
//var buf *bytes.Buffer
var
buf
io
.
Writer
fmt
.
Printf
(
"%T
\n
"
,
buf
)
if
debug
{
buf
=
new
(
bytes
.
Buffer
)
// enable collection of output
}
...
...
@@ -21,7 +24,18 @@ func main() {
// If out is non-nil, output will be written to it.
func
f
(
out
io
.
Writer
)
{
// ...do something...
fmt
.
Println
(
out
)
fmt
.
Printf
(
"%T
\n
"
,
out
)
if
out
!=
nil
{
fmt
.
Println
(
"!!!!!!!!!!"
)
out
.
Write
([]
byte
(
"done!
\n
"
))
}
var
a
interface
{}
a
=
map
[
string
]
string
{
"a"
:
"b"
,
}
a
=
nil
b
,
_
:=
a
.
(
map
[
string
]
string
)
fmt
.
Println
(
b
)
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment