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
fe798240
authored
Jul 21, 2020
by
孙龙
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
up
parent
7fe143a7
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
261 additions
and
33 deletions
controller/ziyin_controller.go
model/ziyin.go
pkg/common/function.go
service/ziyin_service.go
test/test5.go
controller/ziyin_controller.go
View file @
fe798240
...
...
@@ -23,11 +23,14 @@ func Zyh(ctx *gin.Context){
zyhResponse
:=
&
model
.
ZyhResponse
{}
err
:=
zyServiceImpl
.
Zyh
(
ctx
,
zyhRequest
,
zyhResponse
)
zyhResponse
,
err
:=
zyServiceImpl
.
Zyh
(
ctx
,
zyhRequest
)
if
err
!=
nil
{
zyhResponse
.
ErrorCode
=
1
zyhResponse
.
ErrorMsg
=
fmt
.
Sprintf
(
"%s"
,
err
)
ctx
.
JSON
(
200
,
zyhResponse
)
}
fmt
.
Println
(
"6666666666666"
)
fmt
.
Printf
(
"%+v"
,
zyhResponse
)
ctx
.
JSON
(
200
,
zyhResponse
)
}
model/ziyin.go
View file @
fe798240
...
...
@@ -44,4 +44,6 @@ type ZyhResponse struct {
ErrorCode
int64
`json:"error_code"`
ErrorMsg
string
`json:"error_msg"`
Data
interface
{}
`json:"data"`
}
\ No newline at end of file
}
pkg/common/function.go
View file @
fe798240
...
...
@@ -7,6 +7,7 @@ import (
"github.com/syyongx/php2go"
"math/big"
"math/rand"
"reflect"
"regexp"
"search_server/pkg/vars"
"strconv"
...
...
@@ -155,4 +156,38 @@ func CopyMapString(distmap map[string]string) map[string]string{
tmpmap
[
k
]
=
v
}
return
tmpmap
}
\ No newline at end of file
}
// interface{}转为 []interface{}
//將任意類型的切片轉為 []interface{}
func
CreateAnyTypeSlice
(
slice
interface
{})
([]
interface
{},
bool
)
{
val
,
ok
:=
isSlice
(
slice
)
if
!
ok
{
return
nil
,
false
}
sliceLen
:=
val
.
Len
()
out
:=
make
([]
interface
{},
sliceLen
)
for
i
:=
0
;
i
<
sliceLen
;
i
++
{
out
[
i
]
=
val
.
Index
(
i
)
.
Interface
()
}
return
out
,
true
}
// 判断是否为slcie数据
func
isSlice
(
arg
interface
{})
(
val
reflect
.
Value
,
ok
bool
)
{
val
=
reflect
.
ValueOf
(
arg
)
if
val
.
Kind
()
==
reflect
.
Slice
{
ok
=
true
}
return
}
service/ziyin_service.go
View file @
fe798240
...
...
@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
"github.com/prometheus/common/log"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
...
...
@@ -13,6 +14,7 @@ import (
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"strconv"
"strings"
...
...
@@ -24,10 +26,7 @@ type ZyServiceImpl struct{}
/*
自营搜索
*/
func
(
this
*
ZyServiceImpl
)
Zyh
(
ctx
*
gin
.
Context
,
req
map
[
string
]
string
,
rsp
*
model
.
ZyhResponse
)
(
error
)
{
////fmt.Printf("%+v",req)
//fmt.Println(req.NeedAggs)
//fmt.Println(5566)
func
(
this
*
ZyServiceImpl
)
Zyh
(
ctx
*
gin
.
Context
,
req
map
[
string
]
string
)
(
rsp
*
model
.
ZyhResponse
,
err
error
)
{
var
(
need_aggs
bool
_type
byte
...
...
@@ -42,12 +41,12 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string, rsp *mod
if
need_aggs
{
_type
=
1
}
else
{
_type
=
1
_type
=
3
}
//设置cookie
setCookie
(
ctx
,
req
)
rsp
=
zy
(
req
,
_type
,
0
)
return
nil
return
rsp
,
nil
}
func
NewZyServiceImpl
()
*
ZyServiceImpl
{
...
...
@@ -125,8 +124,9 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
req2
:=
make
(
map
[
string
]
string
,
0
)
req2
=
common
.
CopyMapString
(
req
)
delete
(
req2
,
"class_id/condition"
)
searchZiYingGoods
(
req2
,
true
,
zhyClassList
,
classLight
,
_type
)
return
nil
rsp
=
searchZiYingGoods
(
req2
,
true
,
zhyClassList
,
classLight
,
_type
)
//fmt.Printf("返回數據:%+v",rsp)
return
rsp
}
...
...
@@ -138,12 +138,24 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
* @param type $class_light 高亮分类
* @param type $type 1:全部输出(属性统计+列表) 2:只输出属性统计 3:只输出列表
*/
func
searchZiYingGoods
(
req
map
[
string
]
string
,
zy
bool
,
classList
*
model
.
ZhyClassList
,
classLight
[]
*
model
.
ZhyClassListModel
,
aType
byte
){
func
searchZiYingGoods
(
req
map
[
string
]
string
,
zy
bool
,
classList
*
model
.
ZhyClassList
,
classLight
[]
*
model
.
ZhyClassListModel
,
aType
byte
)
(
rsp
*
model
.
ZyhResponse
)
{
var
(
goods_name_arr
[]
map
[
string
]
string
keyword
string
is_sample
bool
redisConn
redis
.
Conn
response
map
[
string
]
interface
{}
)
defer
func
()
{
redisConn
.
Close
()
}()
rsp
=
&
model
.
ZyhResponse
{}
rsp
.
ErrorMsg
=
""
rsp
.
ErrorCode
=
0
response
=
make
(
map
[
string
]
interface
{},
0
)
keyword
=
""
goods_name_arr
=
make
([]
map
[
string
]
string
,
0
)
if
req
[
"goods_name/condition"
]
!=
""
{
...
...
@@ -159,7 +171,10 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
}
goods_temp_status
:=
0
source
:=
elastic
.
NewSearchSource
()
source
.
StoredFields
(
"goods_id"
,
"goods_name"
,
"sample_max_number"
,
"sample_status"
,
"sample_class_id"
)
//source.fie("goods_id","goods_name","sample_max_number","sample_status","sample_class_id")
source
.
FetchSourceContext
(
elastic
.
NewFetchSourceContext
(
true
)
.
Include
(
"goods_id"
,
"goods_name"
,
"sample_max_number"
,
"sample_status"
,
"sample_class_id"
))
query
:=
elastic
.
NewBoolQuery
()
if
(
len
(
req
)
>
0
){
...
...
@@ -251,9 +266,140 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
from
=
10000
-
length
}
source
.
From
(
int
(
from
))
.
Size
(
int
(
length
))
excludeBrandIds
:=
model
.
GetExcludeBrandIds
(
1
)
if
len
(
excludeBrandIds
)
>
0
{
brandIdTermsQuery
,
ok
:=
common
.
CreateAnyTypeSlice
(
excludeBrandIds
)
if
ok
{
query
.
MustNot
(
elastic
.
NewTermsQuery
(
"brand_id"
,
brandIdTermsQuery
...
))
}
}
source
.
Query
(
query
)
res
,
_
:=
elastic
.
NewSearchRequest
()
.
Source
(
source
)
.
Body
()
fmt
.
Println
(
res
)
queryJson
,
_
:=
elastic
.
NewSearchRequest
()
.
Source
(
source
)
.
Body
()
//fmt.Println(queryJson)
result
,
err
:=
es
.
CurlES
(
config
.
Get
(
"ZIYING_CONFIG.Index"
)
.
String
(),
queryJson
)
if
err
!=
nil
{
log
.
Error
(
err
)
}
//fmt.Println(result)
logger
.
Info
(
result
)
//单独返回goods_id
if
just_id
,
ok
:=
req
[
"just_id"
];
ok
&&
just_id
==
"2"
{
hits2
:=
gjson
.
Get
(
result
,
"hits.hits.#._source"
)
.
Array
()
hitsTotal
:=
gjson
.
Get
(
result
,
"hits.total"
)
.
Int
()
var
flag
int64
var
goodsIdList
[]
string
flag
=
1
if
hitsTotal
>
0
&&
len
(
hits2
)
>
0
{
flag
=
0
//fmt.Println(hits2)
for
_
,
goods
:=
range
hits2
{
goodsId
:=
gjson
.
Get
(
goods
.
String
(),
"goods_id"
)
.
String
()
goodsIdList
=
append
(
goodsIdList
,
goodsId
)
}
}
rsp
.
ErrorCode
=
flag
rsp
.
ErrorMsg
=
""
rsp
.
Data
=
goodsIdList
return
rsp
}
//处理聚合运算数据
res
:=
make
(
map
[
string
]
map
[
string
]
map
[
string
]
interface
{},
0
)
res
[
"aggs"
]
=
make
(
map
[
string
]
map
[
string
]
interface
{},
0
)
aggregations
:=
gjson
.
Get
(
result
,
"aggregations"
)
.
Map
()
redisConn
=
gredis
.
Conn
(
"search_r"
)
if
len
(
aggregations
)
>
0
{
//brand_id class_id2 packing
for
field
,
vbuckets
:=
range
aggregations
{
if
field
==
"attrs"
{
continue
}
buckets
:=
gjson
.
Get
(
vbuckets
.
String
(),
"buckets"
)
.
Array
()
res
[
"aggs"
][
field
]
=
make
(
map
[
string
]
interface
{},
0
)
aggsFieldList
:=
make
([]
map
[
string
]
string
,
0
)
for
k
,
v
:=
range
buckets
{
aggregationsKeyBuckets
:=
v
.
Map
()
id
:=
aggregationsKeyBuckets
[
"key"
]
.
String
()
aggregationsKeyBucketsStr
:=
""
switch
field
{
case
"brand_id"
:
brand_j
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_Brand"
,
aggregationsKeyBuckets
[
"key"
]))
if
err
!=
nil
{
aggregationsKeyBucketsStr
=
""
}
else
{
aggregationsKeyBucketsStr
=
gjson
.
Get
(
brand_j
,
"brand_name"
)
.
String
()
}
case
"class_id2"
:
arr_oj
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_SelfClassInfo"
,
aggregationsKeyBuckets
[
"key"
]))
if
err
==
nil
{
aggregationsKeyBucketsStr
=
gjson
.
Get
(
arr_oj
,
"class_name"
)
.
String
()
}
case
"packing"
:
Self_UnitAliasInfo
,
err
:=
redis
.
String
(
redisConn
.
Do
(
"Hget"
,
"Self_UnitAlias"
,
aggregationsKeyBuckets
[
"key"
]))
fmt
.
Println
(
"packing"
,
aggregationsKeyBuckets
[
"key"
],
Self_UnitAliasInfo
)
if
err
==
nil
{
aggregationsKeyBucketsStr
=
Self_UnitAliasInfo
}
}
if
zy
{
fieldMd5
:=
common
.
Md5
(
field
)
aggsFieldList
=
append
(
aggsFieldList
,
map
[
string
]
string
{
"name"
:
aggregationsKeyBucketsStr
,
"id"
:
id
,
"num"
:
aggregationsKeyBuckets
[
"doc_count"
]
.
String
(),
})
res
[
"aggs"
][
field
][
"list"
]
=
aggsFieldList
res
[
"aggs"
][
field
][
"name"
]
=
fieldMd5
[
8
:
24
]
}
else
{
res
[
"aggs"
][
field
][
strconv
.
Itoa
(
k
)]
=
map
[
string
]
string
{
"name"
:
aggregationsKeyBucketsStr
,
"id"
:
id
,
"num"
:
aggregationsKeyBuckets
[
"doc_count"
]
.
String
(),
}
}
}
}
//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
()
attr_value_buckets
:=
gjson
.
Get
(
vbuckets
.
String
(),
"attr_value.buckets"
)
for
_
,
v
:=
range
attr_value_buckets
.
Map
(){
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
]
}
}
}
response
[
"aggs"
]
=
res
[
"aggs"
]
}
flag
:=
1
flag
=
flag
//如果是类目点进来,则高亮类目
if
zy
{
response
[
"class"
]
=
classList
response
[
"class_light"
]
=
classLight
}
//todo 添加属性单独搜索
if
aType
==
2
{
rsp
.
Data
=
response
return
rsp
}
rsp
.
Data
=
response
return
rsp
}
func
makeQuery
(
query
*
elastic
.
BoolQuery
,
source
*
elastic
.
SearchSource
,
conditions
[]
string
,
goods_temp_status
*
int
,
searchValue
*
string
,
...
...
@@ -369,7 +515,11 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
for
k
,
v
:=
range
term_v
{
term_v
[
k
]
=
reg
.
ReplaceAllString
(
v
,
""
)
}
query
.
Must
(
elastic
.
NewTermsQuery
(
"auto_brand_name2"
,
term_v
))
auto_brand_name2_terms
,
ok
:=
common
.
CreateAnyTypeSlice
(
term_v
)
if
ok
{
query
.
Must
(
elastic
.
NewTermsQuery
(
"auto_brand_name2"
,
auto_brand_name2_terms
...
))
}
}
case
"stock"
:
if
stockValue
,
err
=
strconv
.
ParseInt
(
*
searchValue
,
10
,
64
);
err
==
nil
{
...
...
@@ -395,7 +545,7 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
break
}
}
query
.
Must
(
elastic
.
NewTermsQuery
(
searchKey
,
term_v_interface
))
query
.
Must
(
elastic
.
NewTermsQuery
(
searchKey
,
term_v_interface
...
))
}
case
"order"
:
orderSort
:=
false
...
...
@@ -479,8 +629,11 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
termsQuerySlice
=
append
(
termsQuerySlice
,
term_v1_int
)
}
}
supplierIdTerms
,
ok
:=
common
.
CreateAnyTypeSlice
(
termsQuerySlice
)
if
ok
{
query
.
Must
(
elastic
.
NewTermsQuery
(
"supplier_id"
,
supplierIdTerms
...
))
}
query
.
Must
(
elastic
.
NewTermsQuery
(
"supplier_id"
,
termsQuerySlice
))
}
}
...
...
test/test5.go
View file @
fe798240
package
main
import
(
"
log
"
"
time
"
"
fmt
"
"
reflect
"
)
func
main
(){
bigSlowOperation
()
// interface{}转为 []interface{}
func
CreateAnyTypeSlice
(
slice
interface
{})
([]
interface
{},
bool
)
{
val
,
ok
:=
isSlice
(
slice
)
if
!
ok
{
return
nil
,
false
}
sliceLen
:=
val
.
Len
()
out
:=
make
([]
interface
{},
sliceLen
)
for
i
:=
0
;
i
<
sliceLen
;
i
++
{
out
[
i
]
=
val
.
Index
(
i
)
.
Interface
()
}
return
out
,
true
}
func
bigSlowOperation
()
{
defer
trace
(
"bigSlowOperation"
)()
// don't forget the
// ...lots of work…
time
.
Sleep
(
10
*
time
.
Second
)
// simulate slow
// 判断是否为slcie数据
func
isSlice
(
arg
interface
{})
(
val
reflect
.
Value
,
ok
bool
)
{
val
=
reflect
.
ValueOf
(
arg
)
if
val
.
Kind
()
==
reflect
.
Slice
{
ok
=
true
}
return
}
func
trace
(
msg
string
)
func
()
{
start
:=
time
.
Now
()
log
.
Printf
(
"enter %s"
,
msg
)
return
func
()
{
log
.
Printf
(
"exit %s (%s)"
,
msg
,
time
.
Since
(
start
))
// 看需求写的代码
func
faa
(
arg
interface
{})
{
slice
,
ok
:=
CreateAnyTypeSlice
(
arg
)
if
!
ok
{
return
}
for
index
,
value
:=
range
slice
{
fmt
.
Println
(
index
,
value
)
}
}
func
main
()
{
intSlice
:=
[]
int
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}
strSlice
:=
[]
string
{
"a"
,
"b"
,
"c"
,
"d"
}
boolSlice
:=
[]
bool
{
true
,
true
,
false
,
true
}
faa
(
intSlice
)
faa
(
strSlice
)
faa
(
boolSlice
)
}
\ 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