forked from defensestation/osquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_score.go
More file actions
155 lines (125 loc) · 3.11 KB
/
Copy pathfunction_score.go
File metadata and controls
155 lines (125 loc) · 3.11 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Package osquery Modified by harshit98 on 2025-05-10
// Changes: Added function score support
package osquery
type FunctionScoreQuery struct {
query Mappable
functions []Function
boostMode string
scoreMode string
maxBoost *float32
minScore *float32
boost *float32
}
type Function interface {
Map() map[string]interface{}
}
type RandomScoreFunction struct {
seed *int64
field string
}
type ScriptScoreFunction struct {
script *ScriptField
}
// Additional functions can be added as per usecase in future like:
// - ScriptScoreFunction
// - DecayFunction (with variants for geo, date, numeric)
// - WeightFunction
//
// For ref: https://docs.opensearch.org/docs/latest/query-dsl/compound/function-score/
func FunctionScore(query Mappable) *FunctionScoreQuery {
return &FunctionScoreQuery{query: query}
}
func (q *FunctionScoreQuery) Function(f Function) *FunctionScoreQuery {
q.functions = append(q.functions, f)
return q
}
func (q *FunctionScoreQuery) BoostMode(boostMode string) *FunctionScoreQuery {
q.boostMode = boostMode
return q
}
func (q *FunctionScoreQuery) ScoreMode(scoreMode string) *FunctionScoreQuery {
q.scoreMode = scoreMode
return q
}
func (q *FunctionScoreQuery) MaxBoost(maxBoost float32) *FunctionScoreQuery {
q.maxBoost = &maxBoost
return q
}
func (q *FunctionScoreQuery) MinScore(minScore float32) *FunctionScoreQuery {
q.minScore = &minScore
return q
}
func (q *FunctionScoreQuery) Boost(boost float32) *FunctionScoreQuery {
q.boost = &boost
return q
}
func (q *FunctionScoreQuery) Map() map[string]interface{} {
m := make(map[string]interface{})
if q.query != nil {
m["query"] = q.query.Map()
}
if len(q.functions) > 0 {
funcs := make([]map[string]interface{}, len(q.functions))
for i, f := range q.functions {
funcs[i] = f.Map()
}
m["functions"] = funcs
}
if q.boostMode != "" {
m["boost_mode"] = q.boostMode
}
if q.maxBoost != nil {
m["max_boost"] = *q.maxBoost
}
if q.scoreMode != "" {
m["score_mode"] = q.scoreMode
}
if q.minScore != nil {
m["min_score"] = *q.minScore
}
if q.boost != nil {
m["boost"] = *q.boost
}
return map[string]interface{}{
"function_score": m,
}
}
func RandomScore() *RandomScoreFunction {
return &RandomScoreFunction{}
}
func (f *RandomScoreFunction) Seed(seed int64) *RandomScoreFunction {
f.seed = &seed
return f
}
func (f *RandomScoreFunction) Field(field string) *RandomScoreFunction {
f.field = field
return f
}
func (f *RandomScoreFunction) Map() map[string]interface{} {
m := make(map[string]interface{})
if f.seed != nil {
m["seed"] = *f.seed
}
if f.field != "" {
m["field"] = f.field
}
return map[string]interface{}{
"random_score": m,
}
}
func FunctionScriptScore(script *ScriptField) *ScriptScoreFunction {
return &ScriptScoreFunction{script: script}
}
func (f *ScriptScoreFunction) Map() map[string]interface{} {
if f.script == nil {
return map[string]interface{}{
"script_score": map[string]interface{}{},
}
}
scriptMap := f.script.Map()["script"].(map[string]interface{})
return map[string]interface{}{
"script_score": map[string]interface{}{
"script": scriptMap,
},
}
}