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
b570b378
authored
Jul 28, 2020
by
孙龙
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
up
parent
2ff90e41
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
206 additions
and
15 deletions
boot/app.go
conf/search.ini
controller/ziyin_controller.go
go.mod
pkg/config/mongo.go
pkg/mongo/mongo.go
service/ziyin_service.go
test/test7.go
boot/app.go
View file @
b570b378
...
...
@@ -4,6 +4,7 @@ import (
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/pkg/logger"
"search_server/pkg/mongo"
"search_server/pkg/mysql"
)
...
...
@@ -12,15 +13,19 @@ func Boot(configPath string) (err error) {
panic
(
err
)
}
if
err
=
mysql
.
Setup
();
err
!=
nil
{
panic
(
err
)
return
}
if
err
=
gredis
.
Setup
();
err
!=
nil
{
panic
(
err
)
return
}
if
err
=
mongo
.
SetUp
();
err
!=
nil
{
panic
(
err
)
return
}
//if err = mongo.SetUp(); err != nil {
// return
//}
if
err
=
logger
.
SetUp
();
err
!=
nil
{
panic
(
err
)
return
}
return
...
...
conf/search.ini
View file @
b570b378
...
...
@@ -194,4 +194,5 @@ SEARCH_TOKEN_EXPIRE_TIME = 30
999999
=
zhuanmai
[SEARCH_API_LOG]
SEARCH_API_ERROR_PRE
=
search_api_overtime_
\ No newline at end of file
SEARCH_API_ERROR_PRE
=
search_api_overtime_
controller/ziyin_controller.go
View file @
b570b378
...
...
@@ -45,7 +45,9 @@ func Index(ctx *gin.Context){
zyhRequest
[
requestName
]
=
strings
.
TrimSpace
(
requstValue
[
0
])
}
}
zyServiceImpl
:=
service
.
NewZyServiceImpl
()
zyhResponse
=
zyServiceImpl
.
Index
(
ctx
,
zyhRequest
)
common
.
Output
(
int
(
zyhResponse
.
ErrorCode
),
zyhResponse
.
ErrorMsg
,
zyhResponse
.
Data
)
return
...
...
go.mod
View file @
b570b378
...
...
@@ -31,6 +31,7 @@ require (
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
github.com/uniplaces/carbon v0.1.6
go.mongodb.org/mongo-driver v1.3.5 // indirect
go.uber.org/zap v1.14.1 // indirect
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect
...
...
pkg/config/mongo.go
0 → 100644
View file @
b570b378
package
config
type
MongoDbDatabase
struct
{
Host
string
UserName
string
Password
string
Database
string
MaxPoolSize
string
}
func
BuildMongoDbConfgs
()
map
[
string
]
MongoDbDatabase
{
return
map
[
string
]
MongoDbDatabase
{
"default"
:
{
Host
:
Get
(
"mongo.host"
)
.
String
(),
UserName
:
Get
(
"mongo.username"
)
.
String
(),
Password
:
Get
(
"mongo.password"
)
.
String
(),
Database
:
Get
(
"mongo.database"
)
.
String
(),
MaxPoolSize
:
Get
(
"mongo.maxPoolSize"
)
.
String
(),
},
}
}
pkg/mongo/mongo.go
View file @
b570b378
package
mongo
import
(
"fmt"
"gopkg.in/mgo.v2"
"search_server/pkg/config"
"strconv"
)
var
session
*
mgo
.
Session
func
SetUp
()
(
err
error
)
{
url
:=
config
.
Get
(
"mongo.url"
)
.
String
()
type
ichuntMongo
struct
{
MongoList
map
[
string
]
*
mgo
.
Session
}
var
ichuntMongo_
=
&
ichuntMongo
{}
func
getconn
(
mongoConfig
config
.
MongoDbDatabase
)
(
*
mgo
.
Session
,
error
){
url
:=
mongoConfig
.
Host
maxPoolSize
:=
mongoConfig
.
MaxPoolSize
maxPoolSizeInt
,
err
:=
strconv
.
Atoi
(
maxPoolSize
)
if
err
!=
nil
{
maxPoolSizeInt
=
100
}
url
+=
"?maxPoolSize="
+
maxPoolSize
session
,
err
=
mgo
.
Dial
(
url
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
return
fmt
.
Println
(
"url"
,
url
)
fmt
.
Println
(
"maxPoolSizeInt"
,
maxPoolSizeInt
)
session
.
SetPoolLimit
(
maxPoolSizeInt
)
session
.
SetMode
(
mgo
.
Monotonic
,
true
)
myDB
:=
session
.
DB
(
mongoConfig
.
Database
)
err
=
myDB
.
Login
(
mongoConfig
.
UserName
,
mongoConfig
.
Password
)
if
err
!=
nil
{
return
nil
,
err
}
return
session
,
nil
}
func
GetDB
()
*
mgo
.
Database
{
database
:=
config
.
Get
(
"mongo.database"
)
.
String
()
db
:=
session
.
DB
(
database
)
return
db
func
SetUp
()
(
err
error
)
{
err
=
nil
ichuntMongo_
.
MongoList
=
make
(
map
[
string
]
*
mgo
.
Session
,
0
)
mongodbList
:=
config
.
BuildMongoDbConfgs
()
if
len
(
mongodbList
)
>
0
{
for
mongoName
,
mongoConfig
:=
range
mongodbList
{
ichuntMongo_
.
MongoList
[
mongoName
],
err
=
getconn
(
mongoConfig
)
if
err
!=
nil
{
break
}
}
}
return
err
}
func
Conn
(
connection
string
)
(
*
mgo
.
Session
){
return
ichuntMongo_
.
MongoList
[
connection
]
.
Copy
()
}
service/ziyin_service.go
View file @
b570b378
...
...
@@ -9,6 +9,8 @@ import (
"github.com/prometheus/common/log"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/olivere/elastic.v5"
"math"
"regexp"
...
...
@@ -18,6 +20,7 @@ import (
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/pkg/mongo"
"search_server/service/transformer"
"strconv"
"strings"
...
...
@@ -140,6 +143,8 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
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
=
searchZiYingGoods
(
req2
,
false
,
nil
,
nil
,
1
)
return
rsp
}
...
...
@@ -842,8 +847,6 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
}
//整理自营数据参数
func
dullZiYingGoodsInfo
(
goods_id_str
string
,
keyword
string
,
goods_name_arr
[]
string
,
goods_list
[]
model
.
ApiGoods
)
(
ziyinGoodsInfo
model
.
ZiyinGoodsInfo
,
ok
bool
){
isHas
:=
false
...
...
@@ -938,3 +941,90 @@ func dullZiYingGoodsInfo(goods_id_str string,keyword string,goods_name_arr []str
return
ziyinGoodsInfo
,
true
}
/*
符合条件 直接读缓存,不需要再查es+redis
@param string type 获取缓存数据:1 单独获取列表数据(class_id第一页数据) 2 单独获取aggs统计 3 获取列表(class_id第一页数据)+aggs统计
*/
func
ziyingCache
(
req
map
[
string
]
string
,
_type
byte
)
(
bool
){
var
(
err
error
cache
bool
=
true
pInt
int
//page int
class_id
int
mongodb
*
mgo
.
Session
)
//是否开启缓存
if
isOpen
,
err
:=
config
.
Get
(
"ZIYING_CACHE.is_no_ziying_cache"
)
.
Bool
()
;
err
==
nil
&&
!
isOpen
{
return
false
}
p
:=
req
[
"p"
]
if
p
!=
""
{
pInt
,
_
=
strconv
.
Atoi
(
p
);
}
is_cache
:=
req
[
"is_cache"
]
if
pInt
>
1
||
is_cache
==
"1"
{
cache
=
false
}
else
{
nocache
:=
[]
string
{
"callback"
,
"p"
,
"pf"
,
"class_id/condition"
,
"qwertyuiop"
,
"asdfghjkl"
,
"_"
}
for
k
,
_
:=
range
req
{
if
php2go
.
InArray
(
k
,[]
string
{
"flag"
,
"no_rule"
,
"hcy_test"
,
"Yo4teW_uid"
}){
continue
}
if
!
php2go
.
InArray
(
k
,
nocache
)
{
cache
=
false
}
}
}
if
!
cache
{
//return false
}
//需要查缓存
class_idCondition
:=
req
[
"class_id/condition"
]
if
class_idCondition
!=
""
{
class_id
,
_
=
strconv
.
Atoi
(
class_idCondition
)
}
agg_table
:=
config
.
Get
(
"AGG_TABLE.agg_table"
)
.
String
()
firstLists_table
:=
config
.
Get
(
"FIRSTLISTS_TABLE.firstLists_table"
)
.
String
()
class_id
=
class_id
err
=
err
mongodb
=
mongo
.
Conn
(
"default"
);
defer
mongodb
.
Close
()
c
:=
mongodb
.
DB
(
"ichunt"
)
.
C
(
firstLists_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"`
UpdateTime
string
`json:"update_time" bson:"update_time"`
}
result
:=
res
{}
switch
_type
{
case
1
:
//单独获取列表数据(class_id第一页数据)
err
:=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
fmt
.
Println
(
"err"
,
err
)
if
err
==
nil
{
}
break
case
2
:
//#单独获取aggs
err
:=
c
.
Find
(
bson
.
M
{
"class_id"
:
class_id
})
.
One
(
&
result
)
fmt
.
Println
(
"err"
,
err
)
if
err
==
nil
{
}
break
case
3
:
//获取列表(class_id第一页数据)+aggs统计
}
return
true
}
\ No newline at end of file
test/test7.go
View file @
b570b378
package
main
import
(
"bytes"
"io"
)
const
debug
=
false
func
main
()
{
var
buf
*
bytes
.
Buffer
if
debug
{
buf
=
new
(
bytes
.
Buffer
)
// enable collection of output
}
f
(
buf
)
// NOTE: subtly incorrect!
if
debug
{
// ...use buf...
}
}
// If out is non-nil, output will be written to it.
func
f
(
out
io
.
Writer
)
{
// ...do something...
if
out
!=
nil
{
out
.
Write
([]
byte
(
"done!
\n
"
))
}
}
\ 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