diff --git a/biz/application/service/post.go b/biz/application/service/post.go index 646fd81..fbd1489 100644 --- a/biz/application/service/post.go +++ b/biz/application/service/post.go @@ -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" @@ -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) diff --git a/biz/infrastructure/mapper/post/es.go b/biz/infrastructure/mapper/post/es.go index c2fdac5..9a0964c 100644 --- a/biz/infrastructure/mapper/post/es.go +++ b/biz/infrastructure/mapper/post/es.go @@ -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" @@ -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) } @@ -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 } @@ -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 diff --git a/go.mod b/go.mod index b6c9e1b..8cdac4b 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ( @@ -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 diff --git a/go.sum b/go.sum index 422871b..df2ad8f 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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=