-
Notifications
You must be signed in to change notification settings - Fork 6
/
algo_score_sort.go
66 lines (59 loc) · 1.55 KB
/
algo_score_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sort
import (
"errors"
"fmt"
"github.com/alibaba/pairec/log"
"github.com/alibaba/pairec/module"
"github.com/alibaba/pairec/recconf"
"sort"
)
type AlgoScoreSort struct {
sortByField string
switchThreshold float64
}
func NewAlgoScoreSort(config recconf.SortConfig) *AlgoScoreSort {
if config.SortByField == "" {
log.Error(fmt.Sprintf("sort by fields is not configured for sort %s", config.Name))
config.SortByField = "current_score"
}
return &AlgoScoreSort{
sortByField: config.SortByField,
switchThreshold: config.SwitchThreshold,
}
}
func GetMaxScore(items []*module.Item) float64 {
maxScore := -1e300
for _, item := range items {
if item.Score > maxScore {
maxScore = item.Score
}
}
return maxScore
}
func (s *AlgoScoreSort) Sort(sortData *SortData) error {
items, ok := sortData.Data.([]*module.Item)
if !ok {
return errors.New("sort data type error")
}
sortByField := s.sortByField
maxRankScore := GetMaxScore(items)
if maxRankScore > s.switchThreshold {
sortByField = "current_score"
}
ctx := sortData.Context
sort.Slice(items, func(i, j int) bool {
iScore, err1 := items[i].FloatExprData(sortByField)
if err1 != nil {
iScore = items[i].Score
ctx.LogWarning(fmt.Sprintf("get sort field %s from item %s failed", sortByField, items[i].Id))
}
jScore, err2 := items[j].FloatExprData(sortByField)
if err2 != nil {
iScore = items[j].Score
ctx.LogWarning(fmt.Sprintf("get sort field %s from item %s failed", sortByField, items[j].Id))
}
return iScore > jScore
})
sortData.Data = items
return nil
}