Skip to content

Add Aggs types #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 67 additions & 16 deletions Sources/ElasticsearchQueryBuilder/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ extension esb {
}
}

/// Adds `query` block to the query syntax.
public struct Query<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "query" : .dict(self.component.makeDict()) ]
}
}
}

/// Adds a `key` with any type of value to the query syntax.
public struct Key: DictComponent {
var key: String
Expand Down Expand Up @@ -108,6 +92,73 @@ extension esb {
}
}

/// Adds a block to the syntax.
public struct Dict<Component: DictComponent>: DictComponent {
var key: String
var component: Component
public init(_ key: String, @QueryDictBuilder component: () -> Component) {
self.key = key
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ key : .dict(self.component.makeDict()) ]
}
}
}

/// Adds a `query` block to the syntax.
public struct Query<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "query" : .dict(self.component.makeDict()) ]
}
}
}

/// Adds an `aggs` block to the syntax.
public struct Aggs<Component: DictComponent>: DictComponent {
var component: Component
public init(@QueryDictBuilder component: () -> Component) {
self.component = component()
}
public func makeDict() -> QueryDict {
let dict = self.component.makeDict()
if dict.isEmpty {
return [:]
} else {
return [ "aggs" : .dict(self.component.makeDict()) ]
}
}
}

/// Defines and named aggregate within `Aggs`
public struct Agg: DictComponent {
var name: String
var term: QueryDict
public init(_ name: String, field: String) {
self.name = name
self.term = [ "field" : .string(field) ]
}
public init(_ name: String, term: QueryDict) {
self.name = name
self.term = term
}
public func makeDict() -> QueryDict {
return [ self.name : [ "terms" : .dict(self.term) ] ]
}
}

/// Adds `minimum_should_match` to the query syntax.
public struct MinimumShouldMatch: DictComponent {
var count: Int
Expand Down
54 changes: 54 additions & 0 deletions Tests/ElasticsearchQueryBuilderTests/ComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ final class KeyTests: XCTestCase {
}
}

final class DictTests: XCTestCase {
func testBuildDict() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Dict("query") {
esb.Key("match_bool_prefix") {
[
"message": "quick brown f"
]
}
}
}
expectNoDifference(build().makeQuery(), [
"query": [
"match_bool_prefix": [
"message": "quick brown f"
]
]
])
}
func testBuildDictEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Dict("query") {
esb.Key("match", .dict([:]))
}
}
expectNoDifference(build().makeQuery(), [:])
}
}

final class ComposableBuilderTests: XCTestCase {
func testBuildDict() throws {
@QueryDictBuilder func makeKey() -> some DictComponent {
Expand Down Expand Up @@ -153,6 +182,31 @@ final class QueryTests: XCTestCase {
}
}

final class AggsTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Aggs {
esb.Agg("name", field: "name")
}
}
expectNoDifference(build().makeQuery(), [
"aggs": [
"name": [
"terms": [ "field": "name" ]
]
]
])
}
func testBuildEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Aggs {
esb.Key("name", .dict([:]))
}
}
expectNoDifference(build().makeQuery(), [:])
}
}

final class BoolTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build(_ enabled: Bool) -> some esb.QueryDSL {
Expand Down
Loading