forked from defensestation/osquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
153 lines (149 loc) · 3.81 KB
/
search_test.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
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
// Modified by DefenseStation on 2024-06-06
// Changes: Updated ElasticSearch client to OpenSearch client, changed package name to 'osquery',
// updated references to OpenSearch documentation, and modified examples accordingly.
package osquery
import (
"testing"
"time"
)
func TestSearchMaps(t *testing.T) {
runMapTests(t, []mapTest{
{
"a simple query with search after",
Search().SearchAfter("_id", "name"),
map[string]interface{}{
"search_after": []string{"_id", "name"},
},
},
{
"a simple match_all query with a size and no aggs",
Search().Query(MatchAll()).Size(20),
map[string]interface{}{
"query": map[string]interface{}{
"match_all": map[string]interface{}{},
},
"size": 20,
},
},
{
"a complex query with an aggregation and various other options",
Search().
Query(
Bool().
Must(
Range("date").
Gt("some time in the past").
Lte("now").
Relation(RangeContains).
TimeZone("Asia/Jerusalem").
Boost(2.3),
Match("author").
Query("some guy").
Analyzer("analyzer?").
Fuzziness("fuzz"),
).
Boost(3.1),
).
Aggs(
Sum("total_score", "score"),
StringStats("tag_stats", "tags").
ShowDistribution(true),
).
PostFilter(Range("score").Gt(0)).
Size(30).
From(5).
Explain(true).
Sort("field_1", OrderDesc).
Sort("field_2", OrderAsc).
SourceIncludes("field_1", "field_2").
SourceExcludes("field_3").
Timeout(time.Duration(20000000000)).
ScriptFields(
Script("distance").
Source("doc['coordinates'].arcDistance(params.lat,params.lon)").
Params(ScriptParams{"lat": 48.8566, "lon": 2.3522}),
Script("duration").
ID("duration").
Lang("painless"),
),
map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"range": map[string]interface{}{
"date": map[string]interface{}{
"gt": "some time in the past",
"lte": "now",
"relation": "CONTAINS",
"time_zone": "Asia/Jerusalem",
"boost": 2.3,
},
},
},
{
"match": map[string]interface{}{
"author": map[string]interface{}{
"query": "some guy",
"analyzer": "analyzer?",
"fuzziness": "fuzz",
},
},
},
},
"boost": 3.1,
},
},
"aggs": map[string]interface{}{
"total_score": map[string]interface{}{
"sum": map[string]interface{}{
"field": "score",
},
},
"tag_stats": map[string]interface{}{
"string_stats": map[string]interface{}{
"field": "tags",
"show_distribution": true,
},
},
},
"post_filter": map[string]interface{}{
"range": map[string]interface{}{
"score": map[string]interface{}{
"gt": 0,
},
},
},
"size": 30,
"from": 5,
"explain": true,
"timeout": "20s",
"sort": []map[string]interface{}{
{"field_1": map[string]interface{}{"order": "desc"}},
{"field_2": map[string]interface{}{"order": "asc"}},
},
"_source": map[string]interface{}{
"includes": []string{"field_1", "field_2"},
"excludes": []string{"field_3"},
},
"script_fields": map[string]interface{}{
"distance": map[string]interface{}{
"script": map[string]interface{}{
"source": "doc['coordinates'].arcDistance(params.lat,params.lon)",
"params": map[string]interface{}{
"lat": 48.8566,
"lon": 2.3522,
},
},
},
"duration": map[string]interface{}{
"script": map[string]interface{}{
"id": "duration",
"lang": "painless",
},
},
},
},
},
})
}