Skip to content

Commit eafbe2a

Browse files
committed
Tests passing for paginated search query
1 parent 69ac489 commit eafbe2a

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

Sources/ElasticsearchNIOClient/ElasticsearchClient+Requests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,12 @@ extension ElasticsearchClient {
133133

134134
public func searchDocumentsPaginated<Document: Decodable>(from indexName: String, searchTerm: String, size: Int = 10, offset: Int = 0, type: Document.Type = Document.self) -> EventLoopFuture<ESGetMultipleDocumentsResponse<Document>> {
135135
do {
136-
let url = try buildURL(path: "/\(indexName)/_search", queryItems: [URLQueryItem(name: "q", value: searchTerm)])
137-
return sendRequest(url: url, method: .GET, headers: .init())
136+
let url = try buildURL(path: "/\(indexName)/_search")
137+
let query = ESSearchRequest(searchQuery: searchTerm, size: size, from: offset)
138+
let body = try AWSPayload.data(self.jsonEncoder.encode(query))
139+
var headers = HTTPHeaders()
140+
headers.add(name: "content-type", value: "application/json")
141+
return sendRequest(url: url, method: .GET, headers: headers, body: body)
138142
} catch {
139143
return self.eventLoop.makeFailedFuture(error)
140144
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
struct ESSearchRequest: Codable {
4+
let from: Int
5+
let size: Int
6+
let query: ESSearchQueryString
7+
8+
init(searchQuery: String, size: Int, from: Int) {
9+
self.from = from
10+
self.size = size
11+
self.query = ESSearchQueryString(queryString: ESSearchQuery(query: searchQuery))
12+
}
13+
}
14+
15+
struct ESSearchQueryString: Codable {
16+
let queryString: ESSearchQuery
17+
18+
enum CodingKeys: String, CodingKey {
19+
case queryString = "query_string"
20+
}
21+
}
22+
23+
struct ESSearchQuery: Codable {
24+
let query: String
25+
}

Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ElasticSearchIntegrationTests: XCTestCase {
180180
let results: ESGetMultipleDocumentsResponse<SomeItem> = try client.searchDocumentsPaginated(from: indexName, searchTerm: "Apples", size: 20, offset: 10).wait()
181181
XCTAssertEqual(results.hits.hits.count, 20)
182182
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 11 Apples" }))
183-
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 39 Apples" }))
183+
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 29 Apples" }))
184184
}
185185

186186
func testSearchingItemsWithTypeProvidedPaginated() throws {
@@ -196,7 +196,7 @@ class ElasticSearchIntegrationTests: XCTestCase {
196196
let results = try client.searchDocumentsPaginated(from: indexName, searchTerm: "Apples", size: 20, offset: 10, type: SomeItem.self).wait()
197197
XCTAssertEqual(results.hits.hits.count, 20)
198198
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 11 Apples" }))
199-
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 39 Apples" }))
199+
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 29 Apples" }))
200200
}
201201

202202
// MARK: - Private

0 commit comments

Comments
 (0)