Meilisearch | Meilisearch Cloud | 文档 | Discord | 路线图 | 网站 | 常见问题
⚡ 为 Swift 编写的 Meilisearch API 客户端 🍎
Meilisearch Swift 是为 Swift 开发者提供的 Meilisearch API 客户端。
Meilisearch 是一个开源的搜索引擎。 了解更多关于 Meilisearch。
关于此 API 的更多信息,请参阅我们的 Swift 文档。
关于 Meilisearch 的更多信息,请参阅我们的 文档 或我们的 API 参考。
告别服务器部署和手动更新,使用 Meilisearch Cloud。 开始于 14 天免费试用期!无需信用卡。
CocoaPods 是 Cocoa 项目的依赖管理器。
Meilisearch-Swift 通过 CocoaPods 提供。要安装它,请将以下行添加到 Podfile
pod 'MeiliSearch'
然后,运行以下命令
pod install
这将下载最新版本的 Meilisearch pod 并准备 xcworkspace
。
Swift 包管理器 是一个用于自动分发 Swift 代码的工具,并且集成到 swift
编译器中。
一旦您设置了 Swift 包,将 Meilisearch-Swift 添加为依赖项就像将其添加到 Package.swift
的 dependencies
值一样简单。
dependencies: [
.package(url: "https://github.com/meilisearch/meilisearch-swift.git", from: "0.16.0")
]
有许多简单的方法可以 下载和运行 Meilisearch 实例。
例如,使用 终端 中的 curl
命令
#Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch --master-key=masterKey
注意:您还可以从 Homebrew 或 APT 下载 Meilisearch,甚至可以将其作为 Docker 运行。
要使用客户端进行简单搜索,您可以创建一个类似于以下的 Swift 脚本
import MeiliSearch
// Create a new client instance of Meilisearch.
// Note: You must provide a fully qualified URL including scheme.
let client = try! MeiliSearch(host: "https://: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.
index.addDocuments(
documents: movies,
primaryKey: nil
) { result in
switch result {
case .success(let task):
print(task) // => Task(uid: 0, status: Task.Status.enqueued, ...)
case .failure(let error):
print(error.localizedDescription)
}
semaphore.signal()
}
semaphore.wait()
使用任务的 uid
,您可以使用更新端点检查您文档添加的状态(enqueued
、canceled
、processing
、succeeded
或 failed
)。
do {
// Call the search function and wait for the result.
let result: SearchResult<Movie> = try await client.index("movies").search(SearchParameters(query: "philoudelphia"))
dump(result)
} catch {
print(error.localizedDescription)
}
输出
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
- estimatedTotalHits: 1
- facetDistribution: nil
▿ processingTimeMs: Optional(1)
- some: 1
▿ query: Optional("philoudelphia")
- some: "philoudelphia"
由于 Meilisearch 具有容错性,所以电影 philadelphia
是对 philoudelphia
的有效搜索结果。
注意:所有包 API 都支持基于闭包的结果以实现向后兼容。新的 async/await 变体正在 问题 332 下添加。
如果您想启用过滤,必须将您的属性添加到 filterableAttributes
索引设置中。
index.updateFilterableAttributes(["id", "genres"]) { result in
// Handle Result in Closure
}
您只需要执行此操作一次。
请注意,每次您更新 filterableAttributes
时,MeiliSearch 都将重建您的索引。根据您的数据集大小,这可能需要一些时间。您可以使用 更新状态 来跟踪此过程。
然后,您可以执行搜索
let searchParameters = SearchParameters(
query: "wonder",
filter: "id > 1 AND genres = Action"
)
let response: Searchable<Meteorite> = try await index.search(searchParameters)
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action","Adventure"]
}
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
本包保证了与 Meilisearch v1.x 版本 的兼容性,但某些功能可能不存在。请查看 问题 获取更多信息。
以下是我们主要文档网站上的一些章节可能对您感兴趣
- 操作文档:查看 API 参考 或了解更多关于 文档 的信息。
- 搜索:查看 API 参考 或遵循我们关于 搜索参数 的指南。
- 管理索引:查看 API 参考 或阅读有关 索引 的更多信息。
- 配置索引设置:查看 API 参考 或阅读有关 设置参数 的指南。
本项目乐于欢迎任何新的贡献!
如果您想了解更多关于开发工作流程或想要做出贡献,请访问我们的 贡献指南 获取详细说明!
要尝试演示,您需要转到 Demos/
目录。在该目录中,您可以
- 在 Xcode 中打开 SwiftPM 项目并按运行,或在终端中运行
swift build
或swift run
。
请在这里查看 Vapor 演示代码 。
请在这里查看 Perfect 演示代码 。
Meilisearch 提供并维护了许多如本例那样的 SDK 和集成工具。我们希望为各类项目提供 令人惊艳的搜索体验。如果您想做出贡献、提出建议或只是想了解当前状况,请访问我们的 集成指南 仓库。