Skip to content

Commit

Permalink
feat: 实现帖子综合搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx committed Apr 9, 2024
1 parent fa87942 commit 0445d2b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
7 changes: 3 additions & 4 deletions biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/convertor"
postmapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/post"
"github.com/CloudStriver/go-pkg/utils/pagination/esp"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content"
Expand Down Expand Up @@ -92,9 +91,9 @@ func (s *PostService) GetPosts(ctx context.Context, req *gencontent.GetPostsReq)

p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
filter := convertor.PostFilterOptionsToFilterOptions(req.PostFilterOptions)
if req.SearchKeyword != nil {
posts, total, err = s.PostEsMapper.Search(ctx, convertor.ConvertPostAllFieldsSearchQuery(*req.SearchKeyword),
filter, p, esp.ScoreCursorType)
if req.SearchOption != nil {
posts, total, err = s.PostEsMapper.Search(ctx, convertor.ConvertPostAllFieldsSearchQuery(*req.SearchOption.SearchKeyword),
filter, p, req.SearchOption.SearchSortType)
} else {
posts, total, err = s.PostMongoMapper.FindManyAndCount(ctx, filter,
p, mongop.IdCursorType)
Expand Down
96 changes: 76 additions & 20 deletions biz/infrastructure/mapper/post/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/esp"
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/multivaluemode"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/scoremode"
"github.com/samber/lo"
"log"
"net/http"
Expand All @@ -27,7 +30,7 @@ import (

type (
IEsMapper interface {
Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Post, int64, error)
Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter gencontent.SearchSortType) ([]*Post, int64, error)
CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error)
}

Expand Down Expand Up @@ -76,27 +79,78 @@ func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopt
return res.Count, nil
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Post, int64, error) {
func SortTypeToCursorType(sortType gencontent.SearchSortType) esp.EsCursor {
switch sortType {
case gencontent.SearchSortType_ScoreSearchSortType:
return esp.ScoreCursorType
case gencontent.SearchSortType_CreateTimeSearchSortType:
return esp.IdCursorType
case gencontent.SearchSortType_SynthesisSearchSortType:
return esp.ScoreCursorType
default:
return nil
}
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter gencontent.SearchSortType) ([]*Post, int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Search", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
p := esp.NewEsPaginator(pagination.NewRawStore(SortTypeToCursorType(sorter)), popts)
filter := newPostFilter(fopts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
return nil, 0, err
}
res, err := m.es.Search().From(int(*popts.Offset)).Size(int(*popts.Limit)).Index(m.indexName).Request(&search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: query,
Filter: filter,
var req *search.Request
if sorter == gencontent.SearchSortType_SynthesisSearchSortType {
dateDecayFunc := types.NewDateDecayFunction()
decayPlacement := types.DecayPlacementDateMathDuration{
Origin: lo.ToPtr("now"), // 衰减起点
Scale: "2d", // 衰减尺度
Offset: "1d", // 可选,定义不应用衰减的初始距离
Decay: lo.ToPtr(types.Float64(0.5)), // 衰减率
}
dateDecayFunc.DateDecayFunction[consts.CreateAt] = decayPlacement
dateDecayFunc.MultiValueMode = &multivaluemode.Avg
req = &search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: query,
Filter: filter,
},
},
},
SearchAfter: sa,
Sort: s,
}).Do(ctx)
Rescore: []types.Rescore{
{
Query: types.RescoreQuery{
Query: types.Query{
FunctionScore: &types.FunctionScoreQuery{
Functions: []types.FunctionScore{
{
Gauss: dateDecayFunc,
},
},
},
},
ScoreMode: &scoremode.Multiply,
},
},
},
}
} else {
req = &search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: query,
Filter: filter,
},
},
Sort: s,
SearchAfter: sa,
}
}
res, err := m.es.Search().From(int(*popts.Offset)).Size(int(*popts.Limit)).Index(m.indexName).Request(req).Do(ctx)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -131,14 +185,16 @@ func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
post.Score_ = float64(hit.Score_)
posts = append(posts, post)
}
// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(posts)
}
if len(posts) > 0 {
err = p.StoreCursor(ctx, posts[0], posts[len(posts)-1])
if err != nil {
return nil, 0, err
if sorter != gencontent.SearchSortType_SynthesisSearchSortType {
// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(posts)
}
if len(posts) > 0 {
err = p.StoreCursor(ctx, posts[0], posts[len(posts)-1])
if err != nil {
return nil, 0, err
}
}
}
return posts, total, nil
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/CloudStriver/cloudmind-mq v1.0.12-0.20240406143558-b7090c0fbb80
github.com/CloudStriver/go-pkg v0.0.0-20240329084540-20ca37a52fea
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408104721-1afd2da8f729
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240409124043-9373d87d4bde
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand All @@ -24,6 +24,7 @@ require (
)

//replace github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go

//replace github.com/CloudStriver/cloudmind-mq => ../cloudmind-mq

require (
Expand Down Expand Up @@ -62,7 +63,6 @@ require (
github.com/jhump/protoreflect v1.15.4 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kitex-contrib/monitor-prometheus v0.2.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,8 @@ github.com/CloudStriver/cloudmind-mq v1.0.12-0.20240406143558-b7090c0fbb80 h1:M0
github.com/CloudStriver/cloudmind-mq v1.0.12-0.20240406143558-b7090c0fbb80/go.mod h1:LLq2w5YEUj8/wFzOIPz9kC1aBjSQr/dFivi16MBiz5Y=
github.com/CloudStriver/go-pkg v0.0.0-20240329084540-20ca37a52fea h1:f+wmV1f8yaOsv9ei83oKG6kr4rbdtv3p0nPMKDZ8hUs=
github.com/CloudStriver/go-pkg v0.0.0-20240329084540-20ca37a52fea/go.mod h1:SsAxWs5EIcaDE/0e5buoFOWsM4lTvFZhySkV68+RT3g=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240406140558-0b39494165c0 h1:y2ABwBsTxL6vmJFe5dbWw+uIsbK3hRXW3rkU14AoAlM=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240406140558-0b39494165c0/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408073658-4d6c3f470a3d h1:PHvVU9Tndk4cQAMqREmf5SNpLfEgjbmVSPJJXsk3zdg=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408073658-4d6c3f470a3d/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408104721-1afd2da8f729 h1:Ba8osCuYP3i3+UYZpv29bqGk5K5okpVr84PkMRsdvr8=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408104721-1afd2da8f729/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240409124043-9373d87d4bde h1:DjXTGQT40LAcsuvGC10vRFhqLYvKVIGl729/m1kcdIw=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240409124043-9373d87d4bde/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -784,8 +780,6 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/monitor-prometheus v0.2.0 h1:cgu8UMn1lpwLD/6rQZnf3jX98rqazTkY/ATmN9DsCwY=
github.com/kitex-contrib/monitor-prometheus v0.2.0/go.mod h1:ZHWQOKRHnN1Bw+PgVYeOXmB9l4+k8dlOJ9wx2xz76NU=
github.com/kitex-contrib/obs-opentelemetry v0.2.5 h1:vV0rbEQnhv7gFPJJPAas4mvp0vopOUC3RU05pnXDOAw=
github.com/kitex-contrib/obs-opentelemetry v0.2.5/go.mod h1:+Hfa6yIiSOIL364QSiUyWTQARkV1OI/NU527SKw+/kk=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
Expand Down Expand Up @@ -907,14 +901,12 @@ github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqr
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
Expand Down

0 comments on commit 0445d2b

Please sign in to comment.