Skip to content

Commit

Permalink
Tests passing for paginated search query
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Mar 29, 2021
1 parent 69ac489 commit eafbe2a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ extension ElasticsearchClient {

public func searchDocumentsPaginated<Document: Decodable>(from indexName: String, searchTerm: String, size: Int = 10, offset: Int = 0, type: Document.Type = Document.self) -> EventLoopFuture<ESGetMultipleDocumentsResponse<Document>> {
do {
let url = try buildURL(path: "/\(indexName)/_search", queryItems: [URLQueryItem(name: "q", value: searchTerm)])
return sendRequest(url: url, method: .GET, headers: .init())
let url = try buildURL(path: "/\(indexName)/_search")
let query = ESSearchRequest(searchQuery: searchTerm, size: size, from: offset)
let body = try AWSPayload.data(self.jsonEncoder.encode(query))
var headers = HTTPHeaders()
headers.add(name: "content-type", value: "application/json")
return sendRequest(url: url, method: .GET, headers: headers, body: body)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/ElasticsearchNIOClient/Models/ESSearchRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

struct ESSearchRequest: Codable {
let from: Int
let size: Int
let query: ESSearchQueryString

init(searchQuery: String, size: Int, from: Int) {
self.from = from
self.size = size
self.query = ESSearchQueryString(queryString: ESSearchQuery(query: searchQuery))
}
}

struct ESSearchQueryString: Codable {
let queryString: ESSearchQuery

enum CodingKeys: String, CodingKey {
case queryString = "query_string"
}
}

struct ESSearchQuery: Codable {
let query: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class ElasticSearchIntegrationTests: XCTestCase {
let results: ESGetMultipleDocumentsResponse<SomeItem> = try client.searchDocumentsPaginated(from: indexName, searchTerm: "Apples", size: 20, offset: 10).wait()
XCTAssertEqual(results.hits.hits.count, 20)
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 11 Apples" }))
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 39 Apples" }))
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 29 Apples" }))
}

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

// MARK: - Private
Expand Down

0 comments on commit eafbe2a

Please sign in to comment.