-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstanceboost.go
79 lines (69 loc) · 2.03 KB
/
instanceboost.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
67
68
69
70
71
72
73
74
75
76
77
78
79
package sajari
import pb "code.sajari.com/protogen-go/sajari/engine/query/v1"
type instanceBoosts []InstanceBoost
func (bs instanceBoosts) proto() ([]*pb.InstanceBoost, error) {
pbs := make([]*pb.InstanceBoost, 0, len(bs))
for _, b := range bs {
pb, err := b.proto()
if err != nil {
return nil, err
}
pbs = append(pbs, pb)
}
return pbs, nil
}
// InstanceBoost is an interface satisfied by instance-based boosting types defined
// in this package.
//
// InstanceBoosts are a way to influence the scoring of a record during a search
// by changing the importance of term instances in indexed records.
type InstanceBoost interface {
proto() (*pb.InstanceBoost, error)
}
type fieldInstanceBoost struct {
field string
value float64
}
func (fb fieldInstanceBoost) proto() (*pb.InstanceBoost, error) {
return &pb.InstanceBoost{
InstanceBoost: &pb.InstanceBoost_Field_{
Field: &pb.InstanceBoost_Field{
Field: fb.field,
Value: fb.value,
},
},
}, nil
}
// FieldInstanceBoost is a boost applied to index terms which originate in
// a particular field.
func FieldInstanceBoost(field string, value float64) InstanceBoost {
return &fieldInstanceBoost{
field: field,
value: value,
}
}
type scoreInstanceBoost struct {
minCount int
threshold float64
}
func (sib scoreInstanceBoost) proto() (*pb.InstanceBoost, error) {
return &pb.InstanceBoost{
InstanceBoost: &pb.InstanceBoost_Score_{
Score: &pb.InstanceBoost_Score{
MinCount: uint32(sib.minCount),
Threshold: sib.threshold,
},
},
}, nil
}
// ScoreInstanceBoost is a boost applied to index terms which have interaction
// data set. For an instance score boost to take effect, the instance must have received
// at least minCount score updates (i.e. count).
// If an item is performing as it should then its score will be 1.
// If the score is below threshold (0 < threshold < 1) then the score will be applied.
func ScoreInstanceBoost(minCount int, threshold float64) InstanceBoost {
return &scoreInstanceBoost{
minCount: minCount,
threshold: threshold,
}
}