MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ
β‘ The MeiliSearch API client written for Swift π
MeiliSearch Swift is the MeiliSearch API client for Swift developers.
MeiliSearch is an open-source search engine. Discover what MeiliSearch is!
- π Documentation
- π§ Installation
- π¬ Getting Started
- π€ Compatibility with MeiliSearch
- π‘ Learn More
- βοΈ Development Workflow and Contributing
- π Demos
For more information about this API see our Swift documentation.
For more information about MeiliSearch see our Documentation or our API References.
CocoaPods is a dependency manager for Cocoa projects.
MeiliSearch-Swift is available through CocoaPods. To install it, add the following line to your Podfile:
pod 'MeiliSearch'
Then, run the following command:
pod install
This will download the latest version of MeiliSearch pod and prepare the xcworkspace
.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding MeiliSearch-Swift as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/meilisearch/meilisearch-swift.git")
]
There are many easy ways to download and run a MeiliSearch instance.
For example, using the curl
command in your Terminal:
#Install MeiliSearch
curl -L https://install.meilisearch.com | sh
# Launch MeiliSearch
./meilisearch --master-key=masterKey
NB: you can also download MeiliSearch from Homebrew or APT or even run it using Docker.
To do a simply search using the client, you can create a Swift script like this:
import MeiliSearch
// Create a new client instance of MeiliSearch.
let client = try! MeiliSearch(host: "http://localhost:7700")
struct Movie: Codable, Equatable {
let id: Int
let title: String
let genres: [String]
}
let movies: [Movie] = [
Movie(id: 1, title: "Carol", genres: ["Romance", "Drama"]),
Movie(id: 2, title: "Wonder Woman", genres: ["Action", "Adventure"]),
Movie(id: 3, title: "Life of Pi", genres: ["Adventure", "Drama"]),
Movie(id: 4, title: "Mad Max: Fury Road", genres: ["Adventure", "Science Fiction"]),
Movie(id: 5, title: "Moana", genres: ["Fantasy", "Action"]),
Movie(id: 6, title: "Philadelphia", genres: ["Drama"])
]
let semaphore = DispatchSemaphore(value: 0)
// An index is where the documents are stored.
// The uid is the unique identifier to that index.
let index = client.index("movies")
// If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
client.index.addDocuments(
documents: movies,
primaryKey: nil
) { result in
switch result {
case .success(let update):
print(update) // => Update(updateId: 0)
case .failure(let error):
print(error.localizedDescription)
}
semaphore.signal()
}
semaphore.wait()
With the updateId
, you can check the status (enqueued
, processing
, processed
or failed
) of your documents addition using the update endpoint.
let semaphore = DispatchSemaphore(value: 0)
// Typealias that represents the result from MeiliSearch.
typealias MeiliResult = Result<SearchResult<Movie>, Swift.Error>
// Call the function search and wait for the closure result.
client.index("movies").search(SearchParameters( query: "philoudelphia" )) { (result: MeiliResult) in
switch result {
case .success(let searchResult):
dump(searchResult)
case .failure(let error):
print(error.localizedDescription)
}
semaphore.signal()
}
semaphore.wait()
Output:
MeiliSearch.SearchResult<SwiftWork.(unknown context at $10d9e7f3c).Movie>
βΏ hits: 1 element
βΏ SwiftWork.(unknown context at $10d9e7f3c).Movie
- id: 6
- title: "Philadelphia"
βΏ genres: 1 element
- "Drama"
- offset: 0
- limit: 20
- nbHits: 1
βΏ exhaustiveNbHits: Optional(false)
- some: false
- facetsDistribution: nil
- exhaustiveFacetsCount: nil
βΏ processingTimeMs: Optional(1)
- some: 1
βΏ query: Optional("philoudelphia")
- some: "philoudelphia"
Since MeiliSearch is typo-tolerant, the movie philadelphia
is a valid search response to philoudelphia
.
This package only guarantees the compatibility with the version v0.24.0 of MeiliSearch.
The following sections may interest you:
- Manipulate documents: see the API references or read more about documents.
- Search: see the API references or follow our guide on search parameters.
- Manage the indexes: see the API references or read more about indexes.
- Configure the index settings: see the API references or follow our guide on settings parameters.
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!
To try out a demo you will need to go to its directory in Demos/
. In that directory you can either:
- Open the SwiftPM project in Xcode and press run, or
- Run
swift build
orswift run
in the terminal.
Please check the Vapor Demo source code here.
Please check the Perfect Demo source code here.
MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.