Skip to content
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

Make total hits optional #23

Merged
merged 3 commits into from
Nov 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct ESGetMultipleDocumentsResponse<Document: Decodable>: Decodable {
}
}

public let total: Total
public let total: Total?
public let hits: [ESGetSingleDocumentResponse<Document>]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class ElasticSearchIntegrationTests: XCTestCase {
Thread.sleep(forTimeInterval: 1.0)

let results = try client.searchDocuments(from: indexName, searchTerm: "Apples", type: SomeItem.self).wait()
XCTAssertEqual(results.hits.total.value, 100)
XCTAssertEqual(results.hits.total.relation, .eq)
XCTAssertEqual(results.hits.total!.value, 100)
XCTAssertEqual(results.hits.total!.relation, .eq)
}

func testCreateDocument() throws {
Expand Down Expand Up @@ -464,6 +464,7 @@ class ElasticSearchIntegrationTests: XCTestCase {

let results: ESGetMultipleDocumentsResponse<SomeItem> = try client.searchDocumentsPaginated(from: indexName, queryBody: queryBody, size: 20, offset: 10).wait()
XCTAssertEqual(results.hits.hits.count, 20)
XCTAssertEqual(results.hits.total!.value, 100)
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 11 Apples" }))
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 29 Apples" }))
}
Expand Down Expand Up @@ -502,6 +503,54 @@ class ElasticSearchIntegrationTests: XCTestCase {

let results: ESGetMultipleDocumentsResponse<SomeItem> = try client.customSearch(from: indexName, query: query).wait()
XCTAssertEqual(results.hits.hits.count, 20)
XCTAssertEqual(results.hits.total!.value, 100)
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 11 Apples" }))
XCTAssertTrue(results.hits.hits.contains(where: { $0.source.name == "Some 29 Apples" }))
}

func testCustomSearchWithTrackTotalHitsFalse() throws {
for index in 1...100 {
let name = "Some \(index) Apples"
let item = SomeItem(id: UUID(), name: name)
_ = try client.createDocument(item, in: self.indexName).wait()
}

// This is required for ES to settle and load the indexes to return the right results
Thread.sleep(forTimeInterval: 2.0)

struct Query: Encodable {
let query: QueryBody
let from: Int
let size: Int
let trackTotalHits: Bool

enum CodingKeys: String, CodingKey {
case query
case from
case size
case trackTotalHits = "track_total_hits"
}
}

struct QueryBody: Encodable {
let queryString: QueryString

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

struct QueryString: Encodable {
let query: String
}

let queryString = QueryString(query: "Apples")
let queryBody = QueryBody(queryString: queryString)
let query = Query(query: queryBody, from: 10, size: 20, trackTotalHits: false)

let results: ESGetMultipleDocumentsResponse<SomeItem> = try client.customSearch(from: indexName, query: query).wait()
XCTAssertNil(results.hits.total)
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 29 Apples" }))
}
Expand Down