Skip to content
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
76 changes: 76 additions & 0 deletions Sources/ElasticsearchQueryBuilder/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,82 @@ extension esb {
}
}

/// Adds `term` block to the query syntax.
///
/// Excludes the component if value is nil.
public struct Term: DictComponent {
let field: String
let value: String?
public init(_ field: String, _ value: String?) {
self.field = field
self.value = value
}
public init<V: RawRepresentable>(_ field: String, _ value: V?) where V.RawValue == String {
self.field = field
self.value = value?.rawValue
}
public init<V: CustomStringConvertible>(_ field: String, describing value: V?) {
self.field = field
self.value = value?.description
}
public func makeDict() -> QueryDict {
guard let value = self.value else { return [:] }
return [ "term" : [ self.field : .string(value) ] ]
}
}

/// Adds multiple `term` block to the query syntax.
///
/// Excludes the component if values is empty.
public struct TermsAND: ArrayComponent {
let field: String
let values: [String]
public init(_ field: String, _ values: [String]) {
self.field = field
self.values = values
}
public init<V>(_ field: String, _ values: [V]) where V: RawRepresentable, V.RawValue == String {
self.field = field
self.values = values.map(\.rawValue)
}
public init<V>(_ field: String, describing values: [V]) where V: CustomStringConvertible {
self.field = field
self.values = values.map(\.description)
}
public func makeArray() -> [QueryDict] {
return self.values.map {
[ "term" : [ self.field : .string($0) ] ]
}
}
}

/// Adds a `terms` block to the query syntax.
///
/// Excludes the component if values is empty.
public struct TermsOR: DictComponent {
let field: String
let values: [String]
public init(_ field: String, _ values: [String]) {
self.field = field
self.values = values
}
public init<V>(_ field: String, _ values: [V]) where V: RawRepresentable, V.RawValue == String {
self.field = field
self.values = values.map(\.rawValue)
}
public init<V>(_ field: String, describing values: [V]) where V: CustomStringConvertible {
self.field = field
self.values = values.map(\.description)
}
public func makeDict() -> QueryDict {
if self.values.isEmpty {
return [:]
} else {
return [ "terms" : [ self.field : .array(self.values) ] ]
}
}
}

/// Adds `knn` block to the query syntax.
public struct kNearestNeighbor<Component: ArrayComponent>: DictComponent {
let field: String
Expand Down
58 changes: 58 additions & 0 deletions Tests/ElasticsearchQueryBuilderTests/ComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,64 @@ final class BoolTests: XCTestCase {
}
}

final class TermTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Term("name", "joe")
}
XCTAssertNoDifference(build().makeQuery(), [
"term": [ "name": "joe" ]
])
}
func testBuildEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Term("name", nil)
}
XCTAssertNoDifference(build().makeQuery(), [:])
}
}

final class TermsORTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.TermsOR("name", ["joe", "mary"])
}
XCTAssertNoDifference(build().makeQuery(), [
"terms": [ "name": ["joe", "mary"] ]
])
}
func testBuildEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.TermsOR("name", [])
}
XCTAssertNoDifference(build().makeQuery(), [:])
}
}

final class TermsANDTests: XCTestCase {
func testBuild() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Filter {
esb.TermsAND("name", ["joe", "mary"])
}
}
XCTAssertNoDifference(build().makeQuery(), [
"filter": [
[ "term": [ "name": "joe" ] ],
[ "term": [ "name": "mary" ] ],
]
])
}
func testBuildEmpty() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
esb.Filter {
esb.TermsAND("name", [])
}
}
XCTAssertNoDifference(build().makeQuery(), [:])
}
}

final class KNearestNeighborTests: XCTestCase {
func testBuildBasic() throws {
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
Expand Down