Skip to content

Commit

Permalink
Merge #415
Browse files Browse the repository at this point in the history
415: Add support for facetStats in search response r=curquiza a=Sherlouk

# Pull Request

## Related issue
Fixes #380

## What does this PR do?
- Add support for facetStats in search response

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: James Sherlock <15193942+Sherlouk@users.noreply.github.com>
  • Loading branch information
meili-bors[bot] and Sherlouk authored Sep 21, 2023
2 parents ae90840 + 9519fcd commit 4c6d2f4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Sources/MeiliSearch/Model/FacetStats.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

/**
`FacetStats` instance represents the minimum and maximum values received for a provided facet
*/
public struct FacetStats: Codable, Equatable {
// MARK: Properties

/// The minimum value found in the given facet
public let min: Double

Check warning on line 11 in Sources/MeiliSearch/Model/FacetStats.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check warning on line 11 in Sources/MeiliSearch/Model/FacetStats.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/// The maximum value found in the given facet
public let max: Double
}
4 changes: 4 additions & 0 deletions Sources/MeiliSearch/Model/SearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Searchable<T>: Equatable, Codable where T: Codable, T: Equatable {

/// Distribution of the given facets.
public var facetDistribution: [String: [String: Int]]?

Check warning on line 18 in Sources/MeiliSearch/Model/SearchResult.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check warning on line 18 in Sources/MeiliSearch/Model/SearchResult.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/// Maximum & minimum stats of a numeric facet.
public var facetStats: [String: FacetStats]?

/// Time, in milliseconds, to process the query.
public var processingTimeMs: Int?
Expand All @@ -25,6 +28,7 @@ public class Searchable<T>: Equatable, Codable where T: Codable, T: Equatable {
public enum CodingKeys: String, CodingKey {
case hits
case facetDistribution
case facetStats
case processingTimeMs
case query
}
Expand Down
45 changes: 45 additions & 0 deletions Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SearchTests: XCTestCase {
XCTAssertEqual(result.query, query)
XCTAssertEqual(result.limit, 20)
XCTAssertEqual(result.hits.count, 1)
XCTAssertNil(result.facetStats)
if response.hits.count > 0 {
XCTAssertEqual("A Moreninha", response.hits[0].title)
XCTAssertNil(response.hits[0].formatted)
Expand Down Expand Up @@ -936,6 +937,7 @@ class SearchTests: XCTestCase {

XCTAssertEqual(facetDistribution["genres"]?.keys.sorted(), expected["genres"]?.keys.sorted())
XCTAssertEqual(facetDistribution["genres"]?.values.sorted(), expected["genres"]?.values.sorted())
XCTAssertEqual(documents.facetStats, [:])

expectation.fulfill()
case .failure(let error):
Expand Down Expand Up @@ -993,6 +995,49 @@ class SearchTests: XCTestCase {
}
self.wait(for: [expectation], timeout: 2.0)
}

// MARK: Facet stats

func testSearchFacetStats() {
let expectation = XCTestExpectation(description: "Search for Books using facets")

configureFilters { result in
switch result {
case .success:
typealias MeiliResult = Result<Searchable<Book>, Swift.Error>
let limit = 5
let query = "A"
let facets = ["id"]
let parameters = SearchParameters(query: query, limit: limit, facets: facets)

self.index.search(parameters) { (result: MeiliResult) in
switch result {
case .success(let documents):
let result = documents as! SearchResult<Book>

XCTAssertEqual(documents.query, query)
XCTAssertEqual(result.limit, limit)
XCTAssertEqual(documents.hits.count, limit)

Check warning on line 1021 in Tests/MeiliSearchIntegrationTests/SearchTests.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check warning on line 1021 in Tests/MeiliSearchIntegrationTests/SearchTests.swift

View workflow job for this annotation

GitHub Actions / linter-check

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
XCTAssertEqual(documents.facetStats?["id"]?.min, 1)
XCTAssertEqual(documents.facetStats?["id"]?.max, 1844)

expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to search with testSearchFacetStats")
expectation.fulfill()
}
}
case .failure(let error):
dump(error)
XCTFail("Could not update settings")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: 2.0)
}
}
// swiftlint:enable force_unwrapping
// swiftlint:enable force_try

0 comments on commit 4c6d2f4

Please sign in to comment.