From 6de1f88a3b2c81790149a30492047f355eeb0fcf Mon Sep 17 00:00:00 2001 From: Renato Almeida Date: Tue, 21 Nov 2023 15:07:54 +0000 Subject: [PATCH 1/2] Make total hits optional --- .../Models/ESMultipleDocumentResponse.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ElasticsearchNIOClient/Models/ESMultipleDocumentResponse.swift b/Sources/ElasticsearchNIOClient/Models/ESMultipleDocumentResponse.swift index 7e63c9f..dad9247 100644 --- a/Sources/ElasticsearchNIOClient/Models/ESMultipleDocumentResponse.swift +++ b/Sources/ElasticsearchNIOClient/Models/ESMultipleDocumentResponse.swift @@ -11,7 +11,7 @@ public struct ESGetMultipleDocumentsResponse: Decodable { } } - public let total: Total + public let total: Total? public let hits: [ESGetSingleDocumentResponse] } From 0abfe8107a14b96a918cee2afc0ac59eff8c17df Mon Sep 17 00:00:00 2001 From: Renato Almeida Date: Tue, 21 Nov 2023 16:28:43 +0000 Subject: [PATCH 2/2] tests --- .../ElasticsearchNIOClientTests.swift | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift b/Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift index 4bccb44..fb6329b 100644 --- a/Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift +++ b/Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift @@ -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 { @@ -464,6 +464,7 @@ class ElasticSearchIntegrationTests: XCTestCase { let results: ESGetMultipleDocumentsResponse = 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" })) } @@ -502,6 +503,54 @@ class ElasticSearchIntegrationTests: XCTestCase { let results: ESGetMultipleDocumentsResponse = 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 = 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" })) }