Skip to content

Commit

Permalink
Merge #426
Browse files Browse the repository at this point in the history
426: (Quick Wins) Fix Xcode 15, Reduce Search Requirements, Update README r=curquiza a=Sherlouk

# Pull Request

## Related issue
Fixes #421
Fixes #420 
Fixes #218 

## What does this PR do?
Nothing huge, new, or breaking. Just three real small issues on the project that this PR can quickly close down including a bug introduced in Xcode 15 - the test now passes locally but Xcode on GitHub defaults to Xcode 14.3 which doesn't have the issue.

## 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 Oct 11, 2023
2 parents 2329431 + 4d93a11 commit 25e5c5b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ To do a simple search using the client, you can create a Swift script like this:
import MeiliSearch

// Create a new client instance of Meilisearch.
// Note: You must provide a fully qualified URL including scheme.
let client = try! MeiliSearch(host: "http://localhost:7700")

struct Movie: Codable, Equatable {
Expand Down Expand Up @@ -182,6 +183,48 @@ Since Meilisearch is typo-tolerant, the movie `philadelphia` is a valid search r

> Note: All package APIs support closure-based results for backwards compatibility. Newer async/await variants are being added under [issue 332](https://github.com/meilisearch/meilisearch-swift/issues/332).
#### Custom Search With Filters <!-- omit in toc -->

If you want to enable filtering, you must add your attributes to the `filterableAttributes` index setting.

```swift
index.updateFilterableAttributes(["id", "genres"]) { result in
// Handle Result in Closure
}
```

You only need to perform this operation once.

Note that MeiliSearch will rebuild your index whenever you update `filterableAttributes`. Depending on the size of your dataset, this might take time. You can track the process using the [update status](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).

Then, you can perform the search:

```swift
let searchParameters = SearchParameters(
query: "wonder",
filter: "id > 1 AND genres = Action"
)

let response: Searchable<Meteorite> = try await index.search(searchParameters)
```

```json
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action","Adventure"]
}
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
```

## 🤖 Compatibility with Meilisearch

This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-swift/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Async/Indexes+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Indexes {
- Throws: Error if a failure occurred.
- Returns: On completion if the request was successful a `Searchable<T>` instance is returned containing the values.
*/
public func search<T: Codable & Equatable>(_ searchParameters: SearchParameters) async throws -> Searchable<T> {
public func search<T: Decodable>(_ searchParameters: SearchParameters) async throws -> Searchable<T> {
try await withCheckedThrowingContinuation { continuation in
self.search.search(self.uid, searchParameters) { result in
continuation.resume(with: result)
Expand Down
5 changes: 4 additions & 1 deletion Sources/MeiliSearch/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public class Config {
Validate if the Meilisearch provided host is a well formatted URL.
*/
func validate() throws -> Config {
guard URL(string: host) != nil else {
guard let url = URL(string: host) else {
throw MeiliSearch.Error.hostNotValid
}
if url.scheme == nil {
throw MeiliSearch.Error.hostNotValid
}
return self
Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public struct Indexes {
public func search<T>(
_ searchParameters: SearchParameters,
_ completion: @escaping (Result<Searchable<T>, Swift.Error>) -> Void)
where T: Codable, T: Equatable {
where T: Decodable {
self.search.search(self.uid, searchParameters, completion)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Search {
_ uid: String,
_ searchParameters: SearchParameters,
_ completion: @escaping (Result<Searchable<T>, Swift.Error>) -> Void)
where T: Codable, T: Equatable {
where T: Decodable {
let data: Data
do {
data = try JSONEncoder().encode(searchParameters)
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ struct Settings {
}
}

private func updateSetting(
private func updateSetting<T: Encodable>(
uid: String,
key: String?,
data: Encodable,
data: T,
completion: @escaping (Result<TaskInfo, Swift.Error>) -> Void
) {
let body: Data
Expand Down
4 changes: 3 additions & 1 deletion Tests/MeiliSearchUnitTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class ClientTests: XCTestCase {
}

func testWrongHostURL() {
XCTAssertNotNil(try MeiliSearch(host: "1234"))
XCTAssertThrowsError(try MeiliSearch(host: "1234")) { error in
XCTAssertEqual(error as! MeiliSearch.Error, MeiliSearch.Error.hostNotValid)
}
}

func testNotValidHostURL() {
Expand Down

0 comments on commit 25e5c5b

Please sign in to comment.