forked from defensestation/osquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_nested.go
More file actions
65 lines (53 loc) · 2.01 KB
/
query_nested.go
File metadata and controls
65 lines (53 loc) · 2.01 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
// Modified by bforbhartiii on 2025-02-24
// Changes: Added nested query support
package osquery
import "github.com/fatih/structs"
type ScoreModeType string
const (
// Uses the average relevance score of all matching inner documents
ScoreModeAvg ScoreModeType = "avg"
// Assigns the highest relevance score from the matching inner documents to the parent.
ScoreModeMax ScoreModeType = "max"
// Assigns the lowest relevance score from the matching inner documents to the parent.
ScoreModeMin ScoreModeType = "min"
// Sums the relevance scores of all matching inner documents.
ScoreModeSum ScoreModeType = "sum"
// Ignores the relevance scores of inner documents and assigns a score of 0 to the parent document.
ScoreModeNone ScoreModeType = "none"
)
// NestedQuery represents a compound query of type "nested",
// as described in https://opensearch.org/docs/latest/query-dsl/joining/nested/
type NestedQuery struct {
path string
query Mappable
scoreMode string
innerHits map[string]interface{}
}
// Nested creates a new query of type "nested" with the provided path and query.
func Nested(path string, query Mappable) *NestedQuery {
return &NestedQuery{
path: path,
query: query,
}
}
// ScoreMode sets the score mode of the query.
func (q *NestedQuery) ScoreMode(mode ScoreModeType) *NestedQuery {
q.scoreMode = string(mode)
return q
}
// InnerHits sets the inner_hits field of the query.
func (q *NestedQuery) InnerHits(innerHits map[string]interface{}) *NestedQuery {
q.innerHits = innerHits
return q
}
// Map returns a map representation of the query, implementing the Mappable interface.
func (q *NestedQuery) Map() map[string]interface{} {
return map[string]interface{}{
"nested": structs.Map(struct {
Path string `structs:"path"`
Query map[string]interface{} `structs:"query"`
ScoreMode string `structs:"score_mode,omitempty"`
InnerHits map[string]interface{} `structs:"inner_hits,omitempty"`
}{q.path, q.query.Map(), q.scoreMode, q.innerHits}),
}
}