Skip to content

Commit

Permalink
feat: 添加时间过滤字段
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx committed Apr 11, 2024
1 parent 081e689 commit 15ac36c
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 239 deletions.
2 changes: 1 addition & 1 deletion biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (s *FileService) GetFileList(ctx context.Context, req *gencontent.GetFileLi
files, total, err2 = s.FileMongoMapper.FindManyAndCount(ctx, filter, p, cursor)
} else {
files, total, err2 = s.FileEsMapper.Search(ctx, convertor.ConvertFileAllFieldsSearchQuery(*req.SearchOption.SearchKeyword),
filter, p, req.SearchOption.SearchSortType)
filter, p, req.SearchOption)
}
if err2 != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func (s *PostService) GetPosts(ctx context.Context, req *gencontent.GetPostsReq)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
filter := convertor.PostFilterOptionsToFilterOptions(req.PostFilterOptions)
if req.SearchOption != nil {
posts, total, err = s.PostEsMapper.Search(ctx, convertor.ConvertPostAllFieldsSearchQuery(*req.SearchOption.SearchKeyword),
filter, p, req.SearchOption.SearchSortType)
posts, total, err = s.PostEsMapper.Search(ctx, convertor.ConvertPostAllFieldsSearchQuery(*req.SearchOption.SearchKeyword), filter, p, req.SearchOption)
} else {
posts, total, err = s.PostMongoMapper.FindManyAndCount(ctx, filter,
p, mongop.IdCursorType)
Expand Down
3 changes: 1 addition & 2 deletions biz/application/service/publicfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *PublicFileService) GetPublicFileList(ctx context.Context, req *genconte
files, total, err = s.PublicFileMongoMapper.FindManyAndCount(ctx, filter, p, cursor)
} else {
files, total, err = s.PublicFileEsMapper.Search(ctx, convertor.ConvertPublicFileAllFieldsSearchQuery(*req.SearchOption.SearchKeyword),
filter, p, req.SearchOption.SearchSortType)
filter, p, req.SearchOption)
}
if err != nil {
return err
Expand All @@ -178,7 +178,6 @@ func (s *PublicFileService) GetPublicFileList(ctx context.Context, req *genconte
return resp, err
}


return resp, nil
}

Expand Down
2 changes: 1 addition & 1 deletion biz/application/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *UserService) GetUsers(ctx context.Context, req *gencontent.GetUsersReq)

p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
if req.SearchOption != nil {
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserAllFieldsSearchQuery(*req.SearchOption.SearchKeyword), p, req.SearchOption.SearchSortType)
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserAllFieldsSearchQuery(*req.SearchOption.SearchKeyword), p, req.SearchOption)
}
if err != nil {
return resp, err
Expand Down
36 changes: 18 additions & 18 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ func ConvertFileAllFieldsSearchQuery(in string) []types.Query {
}
}

func ConvertPublicFileAllFieldsSearchQuery(in string) []types.Query {
return []types.Query{{
MultiMatch: &types.MultiMatchQuery{
Query: in,
Fields: []string{consts.Name + "^3", consts.Description + "^3"},
}},
}
}

func ConvertFileMultiFieldsSearchQuery(in *gencontent.SearchOptions_MultiFieldsKey) []types.Query {
var q []types.Query
if in.MultiFieldsKey.Name != nil {
Expand Down Expand Up @@ -226,15 +217,6 @@ func ConvertFileMultiFieldsSearchQuery(in *gencontent.SearchOptions_MultiFieldsK
return q
}

func ConvertPostAllFieldsSearchQuery(in string) []types.Query {
return []types.Query{{
MultiMatch: &types.MultiMatchQuery{
Query: in,
Fields: []string{consts.Title + "^3", consts.Text},
}},
}
}

func ProductFilterOptionsToFilterOptions(in *gencontent.ProductFilterOptions) *productmapper.FilterOptions {
if in == nil {
return &productmapper.FilterOptions{}
Expand Down Expand Up @@ -441,6 +423,15 @@ func ConvertCouponMultiFieldsSearchQuery(in *gencontent.SearchOptions_MultiField
return q
}

func ConvertPublicFileAllFieldsSearchQuery(in string) []types.Query {
return []types.Query{{
MultiMatch: &types.MultiMatchQuery{
Query: in,
Fields: []string{consts.Name + "^3", consts.Description + "^3"},
}},
}
}

func ConvertUserAllFieldsSearchQuery(in string) []types.Query {
return []types.Query{{
MultiMatch: &types.MultiMatchQuery{
Expand All @@ -449,3 +440,12 @@ func ConvertUserAllFieldsSearchQuery(in string) []types.Query {
}},
}
}

func ConvertPostAllFieldsSearchQuery(in string) []types.Query {
return []types.Query{{
MultiMatch: &types.MultiMatchQuery{
Query: in,
Fields: []string{consts.Title + "^3", consts.Text},
}},
}
}
60 changes: 6 additions & 54 deletions biz/infrastructure/mapper/file/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/utils"
"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 @@ -17,7 +16,6 @@ import (
"github.com/bytedance/sonic"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/count"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/mitchellh/mapstructure"
"github.com/zeromicro/go-zero/core/trace"
Expand All @@ -30,7 +28,7 @@ import (

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

Expand Down Expand Up @@ -92,64 +90,18 @@ func SortTypeToCursorType(sortType gencontent.SearchSortType) esp.EsCursor {
}
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter gencontent.SearchSortType) ([]*File, int64, error) {
func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sopts *gencontent.SearchOption) ([]*File, 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(SortTypeToCursorType(sorter)), popts)
p := esp.NewEsPaginator(pagination.NewRawStore(SortTypeToCursorType(sopts.SearchSortType)), popts)
filter := makeEsFilter(fopts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
return nil, 0, err
}
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,
},
},
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,
}
}
req := utils.GetQuery(query, filter, s, sa, sopts)
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 @@ -185,7 +137,7 @@ func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
file.Score_ = float64(hit.Score_)
files = append(files, file)
}
if sorter != gencontent.SearchSortType_SynthesisSearchSortType {
if sopts.SearchSortType != gencontent.SearchSortType_SynthesisSearchSortType {
// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(files)
Expand Down
60 changes: 6 additions & 54 deletions biz/infrastructure/mapper/post/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/utils"
"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 @@ -17,7 +16,6 @@ import (
"github.com/bytedance/sonic"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/count"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/mitchellh/mapstructure"
"github.com/zeromicro/go-zero/core/trace"
Expand All @@ -30,7 +28,7 @@ import (

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

Expand Down Expand Up @@ -92,64 +90,18 @@ func SortTypeToCursorType(sortType gencontent.SearchSortType) esp.EsCursor {
}
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter gencontent.SearchSortType) ([]*Post, int64, error) {
func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sopts *gencontent.SearchOption) ([]*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(SortTypeToCursorType(sorter)), popts)
p := esp.NewEsPaginator(pagination.NewRawStore(SortTypeToCursorType(sopts.SearchSortType)), popts)
filter := newPostFilter(fopts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
return nil, 0, err
}
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,
},
},
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,
}
}
req := utils.GetQuery(query, filter, s, sa, sopts)
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 @@ -185,7 +137,7 @@ func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
post.Score_ = float64(hit.Score_)
posts = append(posts, post)
}
if sorter != gencontent.SearchSortType_SynthesisSearchSortType {
if sopts.SearchSortType != gencontent.SearchSortType_SynthesisSearchSortType {
// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(posts)
Expand Down
Loading

0 comments on commit 15ac36c

Please sign in to comment.