Skip to content
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
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ Output:
- "Drama"
- offset: 0
- limit: 20
- nbHits: 1
▿ exhaustiveNbHits: Optional(false)
- some: false
- facetsDistribution: nil
- exhaustiveFacetsCount: nil
- estimatedTotalHits: 1
- facetDistribution: nil
▿ processingTimeMs: Optional(1)
- some: 1
▿ query: Optional("philoudelphia")
Expand Down
16 changes: 8 additions & 8 deletions Sources/MeiliSearch/Model/SearchParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public struct SearchParameters: Codable, Equatable {
public let sort: [String]?

/// Retrieve the count of matching terms for each facets.
public let facetsDistribution: [String]?
public let facets: [String]?

/// Whether to return the raw matches or not.
public let matches: Bool?
public let showMatchesPosition: Bool?

// MARK: Initializers

Expand All @@ -65,8 +65,8 @@ public struct SearchParameters: Codable, Equatable {
highlightPostTag: String? = nil,
filter: String? = nil,
sort: [String]? = nil,
facetsDistribution: [String]? = nil,
matches: Bool? = nil) {
facets: [String]? = nil,
showMatchesPosition: Bool? = nil) {
self.query = query
self.offset = offset
self.limit = limit
Expand All @@ -79,8 +79,8 @@ public struct SearchParameters: Codable, Equatable {
self.highlightPostTag = highlightPostTag
self.filter = filter
self.sort = sort
self.facetsDistribution = facetsDistribution
self.matches = matches
self.facets = facets
self.showMatchesPosition = showMatchesPosition
}

// MARK: Query Initializers
Expand Down Expand Up @@ -110,7 +110,7 @@ public struct SearchParameters: Codable, Equatable {
case highlightPostTag
case filter
case sort
case facetsDistribution
case matches
case facets
case showMatchesPosition
}
}
10 changes: 2 additions & 8 deletions Sources/MeiliSearch/Model/SearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ public struct SearchResult<T>: Codable, Equatable where T: Codable, T: Equatable
public let limit: Int

/// Total number of matches,
public let nbHits: Int

/// Whether `nbHits` is exhaustive.
public let exhaustiveNbHits: Bool?
public let estimatedTotalHits: Int

/// Distribution of the given facets.
public let facetsDistribution: [String: [String: Int]]?

/// Whether facetDistribution is exhaustive.
public let exhaustiveFacetsCount: Bool?
public let facetDistribution: [String: [String: Int]]?

/// Time, in milliseconds, to process the query.
public let processingTimeMs: Int?
Expand Down
48 changes: 24 additions & 24 deletions Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class SearchTests: XCTestCase {
XCTAssertEqual(documents.limit, limit)
XCTAssertEqual(documents.hits.count, 1)
let book: Book = documents.hits[0]
XCTAssertEqual("…Joaquim Manuel de Macedo", book.formatted!.comment!)
XCTAssertEqual("…from Joaquim Manuel de Macedo", book.formatted!.comment!)
expectation.fulfill()
case .failure(let error):
dump(error)
Expand Down Expand Up @@ -451,7 +451,7 @@ class SearchTests: XCTestCase {
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description above

    let expectation = XCTestExpectation(description: "Search for Books using matches")

should become using showMatchesPosition. Sorry I can't make a suggestion on it :(

let limit = 5
let query = "Moreninha"
let parameters = SearchParameters(query: query, limit: limit, matches: true)
let parameters = SearchParameters(query: query, limit: limit, showMatchesPosition: true)

self.index.search(parameters) { (result: MeiliResult) in
switch result {
Expand Down Expand Up @@ -585,7 +585,7 @@ class SearchTests: XCTestCase {
distinctAttribute: nil,
filterableAttributes: filterableAttributes,
sortableAttributes: ["id"]
)
)

self.index.updateSettings(settings) { result in
switch result {
Expand Down Expand Up @@ -720,7 +720,7 @@ class SearchTests: XCTestCase {
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
let limit = 5
let query = "A"
let filter = "genres = Novel"
let filter = "genres = Fantasy"
let parameters = SearchParameters(query: query, limit: limit, filter: filter)

self.index.search(parameters) { (result: MeiliResult) in
Expand All @@ -729,10 +729,9 @@ class SearchTests: XCTestCase {
XCTAssertEqual(documents.query, query)
XCTAssertEqual(documents.limit, limit)
XCTAssertEqual(documents.hits.count, 2)
let moreninhaBook: Book = documents.hits.first { book in book.id == 1844 }!
XCTAssertEqual("A Moreninha", moreninhaBook.title)
let petitBook: Book = documents.hits.first { book in book.id == 456 }!
XCTAssertEqual("Le Petit Prince", petitBook.title)

XCTAssertEqual(documents.hits.compactMap { $0.title }, ["Alice In Wonderland", "Harry Potter and the Half-Blood Prince"])

expectation.fulfill()
case .failure(let error):
dump(error)
Expand Down Expand Up @@ -791,9 +790,9 @@ class SearchTests: XCTestCase {
wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Facets distribution
// MARK: Facet distribution

func testSearchFacetsDistribution() {
func testSearchFacetDistribution() {
let expectation = XCTestExpectation(description: "Search for Books using facets distribution")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let expectation = XCTestExpectation(description: "Search for Books using facets distribution")
let expectation = XCTestExpectation(description: "Search for Books using facets")


configureFilters { result in
Expand All @@ -802,8 +801,8 @@ class SearchTests: XCTestCase {
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
let limit = 5
let query = "A"
let facetsDistribution = ["genres"]
let parameters = SearchParameters(query: query, limit: limit, facetsDistribution: facetsDistribution)
let facets = ["genres"]
let parameters = SearchParameters(query: query, limit: limit, facets: facets)

self.index.search(parameters) { (result: MeiliResult) in
switch result {
Expand All @@ -812,21 +811,22 @@ class SearchTests: XCTestCase {
XCTAssertEqual(documents.limit, limit)
XCTAssertEqual(documents.hits.count, limit)

let facetsDistribution = documents.facetsDistribution!
let facetDistribution = documents.facetDistribution!
let expected: [String: [String: Int]] = [
"genres": [
"Bildungsroman": 1,
"Classic Regency nove": 1,
"High fantasy": 1,
"Fantasy": 2,
"Novel": 2,
"Bildungsroman": 1
"High fantasy": 1
]
]
XCTAssertEqual(expected, facetsDistribution)

XCTAssertEqual(facetDistribution["genres"]?.keys.sorted(), expected["genres"]?.keys.sorted())
XCTAssertEqual(facetDistribution["genres"]?.values.sorted(), expected["genres"]?.values.sorted())
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to search with testSearchFacetsDistribution")
XCTFail("Failed to search with testSearchFacetDistribution")
expectation.fulfill()
}
}
Expand All @@ -840,7 +840,7 @@ class SearchTests: XCTestCase {
self.wait(for: [expectation], timeout: 2.0)
}

func testSearchFacetsDistributionNullValue() {
func testSearchFacetDistributionNullValue() {
let expectation = XCTestExpectation(description: "Search for Books using facets distribution with 0 value")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the description still relevant?


configureFilters { result in
Expand All @@ -849,23 +849,23 @@ class SearchTests: XCTestCase {
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
let limit = 5
let query = "Petit Prince"
let facetsDistribution = ["genres"]
let facets = ["genres"]
let filter = "genres = comedy"
let parameters = SearchParameters(query: query, limit: limit, filter: filter, facetsDistribution: facetsDistribution)
let parameters = SearchParameters(query: query, limit: limit, filter: filter, facets: facets)

self.index.search(parameters) { (result: MeiliResult) in
switch result {
case .success(let documents):
XCTAssertEqual(documents.query, query)
XCTAssertEqual(documents.limit, limit)
XCTAssertEqual(documents.hits.count, 0)
let facetsDistribution = documents.facetsDistribution!
XCTAssertEqual(["genres": [:]], facetsDistribution)
let facetDistribution = documents.facetDistribution!
XCTAssertEqual(["genres": [:]], facetDistribution)

expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to search with testSearchFacetsDistribution")
XCTFail("Failed to search with testSearchFacetDistribution")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
XCTFail("Failed to search with testSearchFacetDistribution")
XCTFail("Failed to search with testSearchFacetDistributionNullValue")

expectation.fulfill()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MeiliSearchIntegrationTests/Support/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct Book: Codable, Equatable {
case comment
case genres
case formatted = "_formatted"
case matchesInfo = "_matchesInfo"
case matchesInfo = "_matchesPosition"
}

init(id: Int, title: String, comment: String? = nil, genres: [String] = [], formatted: FormattedBook? = nil, matchesInfo: MatchesInfoBook? = nil) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/MeiliSearchUnitTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SearchTests: XCTestCase {
"offset": 0,
"limit": 20,
"processingTimeMs": 2,
"nbHits": 2,
"estimatedTotalHits": 2,
"query": "botman"
}
"""
Expand Down Expand Up @@ -103,7 +103,7 @@ class SearchTests: XCTestCase {
"offset": 0,
"limit": 20,
"processingTimeMs": 2,
"nbHits": 2,
"estimatedTotalHits": 2,
"query": "botman"
}
"""
Expand Down Expand Up @@ -207,7 +207,7 @@ class SearchTests: XCTestCase {
"offset": 0,
"limit": 20,
"processingTimeMs": 2,
"nbHits": 1,
"estimatedTotalHits": 1,
"query": "h",
"genre": "sci fi"
}
Expand Down Expand Up @@ -235,7 +235,7 @@ class SearchTests: XCTestCase {
switch result {
case .success(let searchResult):
XCTAssertEqual(stubSearchResult, searchResult)
XCTAssertEqual(searchResult.nbHits, 1)
XCTAssertEqual(searchResult.estimatedTotalHits, 1)
expectation.fulfill()
case .failure:
XCTFail("Failed to search for botman")
Expand Down