Skip to content

Commit

Permalink
Merge #414
Browse files Browse the repository at this point in the history
414: Add support for `attributesToSearchOn` in search API r=curquiza a=Sherlouk

# Pull Request

## Related issue
Fixes #397

## What does this PR do?
- Add support for `attributesToSearchOn` in search API

## 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 65d40d7 + d50736d commit ae90840
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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

0 comments on commit ae90840

Please sign in to comment.