Skip to content

Commit 26d978b

Browse files
committed
add Aggs types
1 parent 68813e2 commit 26d978b

File tree

2 files changed

+121
-16
lines changed

2 files changed

+121
-16
lines changed

Sources/ElasticsearchQueryBuilder/Components.swift

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ extension esb {
6060
}
6161
}
6262

63-
/// Adds `query` block to the query syntax.
64-
public struct Query<Component: DictComponent>: DictComponent {
65-
var component: Component
66-
public init(@QueryDictBuilder component: () -> Component) {
67-
self.component = component()
68-
}
69-
public func makeDict() -> QueryDict {
70-
let dict = self.component.makeDict()
71-
if dict.isEmpty {
72-
return [:]
73-
} else {
74-
return [ "query" : .dict(self.component.makeDict()) ]
75-
}
76-
}
77-
}
78-
7963
/// Adds a `key` with any type of value to the query syntax.
8064
public struct Key: DictComponent {
8165
var key: String
@@ -108,6 +92,73 @@ extension esb {
10892
}
10993
}
11094

95+
/// Adds a block to the syntax.
96+
public struct Dict<Component: DictComponent>: DictComponent {
97+
var key: String
98+
var component: Component
99+
public init(_ key: String, @QueryDictBuilder component: () -> Component) {
100+
self.key = key
101+
self.component = component()
102+
}
103+
public func makeDict() -> QueryDict {
104+
let dict = self.component.makeDict()
105+
if dict.isEmpty {
106+
return [:]
107+
} else {
108+
return [ key : .dict(self.component.makeDict()) ]
109+
}
110+
}
111+
}
112+
113+
/// Adds a `query` block to the syntax.
114+
public struct Query<Component: DictComponent>: DictComponent {
115+
var component: Component
116+
public init(@QueryDictBuilder component: () -> Component) {
117+
self.component = component()
118+
}
119+
public func makeDict() -> QueryDict {
120+
let dict = self.component.makeDict()
121+
if dict.isEmpty {
122+
return [:]
123+
} else {
124+
return [ "query" : .dict(self.component.makeDict()) ]
125+
}
126+
}
127+
}
128+
129+
/// Adds an `aggs` block to the syntax.
130+
public struct Aggs<Component: DictComponent>: DictComponent {
131+
var component: Component
132+
public init(@QueryDictBuilder component: () -> Component) {
133+
self.component = component()
134+
}
135+
public func makeDict() -> QueryDict {
136+
let dict = self.component.makeDict()
137+
if dict.isEmpty {
138+
return [:]
139+
} else {
140+
return [ "aggs" : .dict(self.component.makeDict()) ]
141+
}
142+
}
143+
}
144+
145+
/// Defines and named aggregate within `Aggs`
146+
public struct Agg: DictComponent {
147+
var name: String
148+
var term: QueryDict
149+
public init(_ name: String, field: String) {
150+
self.name = name
151+
self.term = [ "field" : .string(field) ]
152+
}
153+
public init(_ name: String, term: QueryDict) {
154+
self.name = name
155+
self.term = term
156+
}
157+
public func makeDict() -> QueryDict {
158+
return [ self.name : [ "terms" : .dict(self.term) ] ]
159+
}
160+
}
161+
111162
/// Adds `minimum_should_match` to the query syntax.
112163
public struct MinimumShouldMatch: DictComponent {
113164
var count: Int

Tests/ElasticsearchQueryBuilderTests/ComponentTests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ final class KeyTests: XCTestCase {
4343
}
4444
}
4545

46+
final class DictTests: XCTestCase {
47+
func testBuildDict() throws {
48+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
49+
esb.Dict("query") {
50+
esb.Key("match_bool_prefix") {
51+
[
52+
"message": "quick brown f"
53+
]
54+
}
55+
}
56+
}
57+
expectNoDifference(build().makeQuery(), [
58+
"query": [
59+
"match_bool_prefix": [
60+
"message": "quick brown f"
61+
]
62+
]
63+
])
64+
}
65+
func testBuildDictEmpty() throws {
66+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
67+
esb.Dict("query") {
68+
esb.Key("match", .dict([:]))
69+
}
70+
}
71+
expectNoDifference(build().makeQuery(), [:])
72+
}
73+
}
74+
4675
final class ComposableBuilderTests: XCTestCase {
4776
func testBuildDict() throws {
4877
@QueryDictBuilder func makeKey() -> some DictComponent {
@@ -153,6 +182,31 @@ final class QueryTests: XCTestCase {
153182
}
154183
}
155184

185+
final class AggsTests: XCTestCase {
186+
func testBuild() throws {
187+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
188+
esb.Aggs {
189+
esb.Agg("name", field: "name")
190+
}
191+
}
192+
expectNoDifference(build().makeQuery(), [
193+
"aggs": [
194+
"name": [
195+
"terms": [ "field": "name" ]
196+
]
197+
]
198+
])
199+
}
200+
func testBuildEmpty() throws {
201+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
202+
esb.Aggs {
203+
esb.Key("name", .dict([:]))
204+
}
205+
}
206+
expectNoDifference(build().makeQuery(), [:])
207+
}
208+
}
209+
156210
final class BoolTests: XCTestCase {
157211
func testBuild() throws {
158212
@ElasticsearchQueryBuilder func build(_ enabled: Bool) -> some esb.QueryDSL {

0 commit comments

Comments
 (0)