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

Add support for attributesToSearchOn in search API #414

Merged
merged 2 commits into from
Sep 21, 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
10 changes: 10 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ search_parameter_guide_page_1: |-
print(error)
}
}
search_parameter_guide_attributes_to_search_on_1: |-
let searchParameters = SearchParameters(query: "adventure", attributesToSearchOn: ["overview"])
client.index("movies").search(searchParameters) { (result: Result<Searchable<Movie>, Swift.Error>) in
switch result {
case .success(let searchResult):
print(searchResult)
case .failure(let error):
print(error)
}
}
authorization_header_1: |-
client = try MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey")
client.getKeys { result in
Expand Down
6 changes: 6 additions & 0 deletions Sources/MeiliSearch/Model/SearchParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public struct SearchParameters: Codable, Equatable {
/// Which attributes to crop.
public let attributesToCrop: [String]?

/// Attributes to search on.
public let attributesToSearchOn: [String]?

/// Limit length at which to crop specified attributes.
public let cropLength: Int?

Expand Down Expand Up @@ -69,6 +72,7 @@ public struct SearchParameters: Codable, Equatable {
hitsPerPage: Int? = nil,
attributesToRetrieve: [String]? = nil,
attributesToCrop: [String]? = nil,
attributesToSearchOn: [String]? = nil,
cropLength: Int? = nil,
cropMarker: String? = nil,
attributesToHighlight: [String]? = nil,
Expand All @@ -85,6 +89,7 @@ public struct SearchParameters: Codable, Equatable {
self.page = page
self.attributesToRetrieve = attributesToRetrieve
self.attributesToCrop = attributesToCrop
self.attributesToSearchOn = attributesToSearchOn
self.cropLength = cropLength
self.cropMarker = cropMarker
self.attributesToHighlight = attributesToHighlight
Expand Down Expand Up @@ -116,6 +121,7 @@ public struct SearchParameters: Codable, Equatable {
case limit
case attributesToRetrieve
case attributesToCrop
case attributesToSearchOn
case cropLength
case cropMarker
case attributesToHighlight
Expand Down
37 changes: 36 additions & 1 deletion Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ private let books: [Book] = [
Book(id: 1344, title: "The Hobbit", comment: "An awesome book", genres: ["High fantasy"]),
Book(id: 4, title: "Harry Potter and the Half-Blood Prince", comment: "The best book", genres: ["Fantasy"]),
Book(id: 42, title: "The Hitchhiker's Guide to the Galaxy", genres: ["Novel"]),
Book(id: 1844, title: "A Moreninha", comment: "A Book from Joaquim Manuel de Macedo", genres: ["Novel"])
Book(id: 1844, title: "A Moreninha", comment: "A Book from Joaquim Manuel de Macedo", genres: ["Novel"]),
Book(id: 94, title: "The Book Thief", comment: "By Markus Zusak", genres: ["Fictional"])
]

// swiftlint:disable force_unwrapping
Expand Down Expand Up @@ -102,6 +103,40 @@ class SearchTests: XCTestCase {
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Attributes to search on
func testAttributesSearchOn() {
let expectation = XCTestExpectation(description: "Search for Books with limited attributes")

typealias MeiliResult = Result<Searchable<Book>, Swift.Error>
let query = "book"
// even though multiple books have the word "book" in the comment, only one has "book" in the title.

self.index.search(SearchParameters(query: query, attributesToSearchOn: ["title"])) { (result: MeiliResult) in
switch result {
case .success(let response):
let result = response as! SearchResult<Book>

XCTAssertEqual(result.estimatedTotalHits, 1)
XCTAssertEqual(result.query, query)
XCTAssertEqual(result.limit, 20)
XCTAssertEqual(result.hits.count, 1)
if response.hits.count > 0 {
XCTAssertEqual("The Book Thief", response.hits[0].title)
XCTAssertNil(response.hits[0].formatted)
} else {
XCTFail("Failed to find hits in the response")
}
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to search with testAttributesSearchOn")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Basic search with finite pagination
func testBasicSearchWithFinitePagination() {
let expectation = XCTestExpectation(description: "Search for Books with finite pagination")
Expand Down