45 个版本 (26 个破坏性更新)
| 0.27.1 | 2024 年 8 月 5 日 | 
|---|---|
| 0.27.0 | 2024 年 7 月 3 日 | 
| 0.26.1 | 2024 年 5 月 14 日 | 
| 0.25.0 | 2024 年 3 月 11 日 | 
| 0.2.0 | 2020 年 7 月 28 日 | 
#24 in 数据库接口
6,939 每月下载量
在  4 crates 中使用
475KB
 6.5K  SLoC
Meilisearch Rust SDK
Meilisearch | Meilisearch Cloud | 文档 | Discord | 路线图 | 网站 | 常见问题解答
⚡ 为 Rust 编写的 Meilisearch API 客户端 🦀
Meilisearch Rust 是面向 Rust 开发者的 Meilisearch API 客户端。
Meilisearch 是一个开源的搜索引擎。 了解更多关于 Meilisearch 的信息。
目录
📖 文档
此自述文件包含了您使用此 Meilisearch SDK 所需的所有文档。
有关如何使用 Meilisearch 的一般信息(例如我们的 API 参考、教程、指南和深入文章),请参阅我们的 主要文档网站。
⚡ 为您的 Meilisearch 体验增添动力
使用 Meilisearch Cloud 摆脱服务器部署和手动更新的烦恼。免费试用 14 天!无需信用卡。
🔧 安装
要使用 meilisearch-sdk,请将以下内容添加到您的 Cargo.toml
[dependencies]
meilisearch-sdk = "0.27.1"
以下可选依赖项也可能很有用
futures = "0.3" # To be able to block on async functions if you are not using an async runtime
serde = { version = "1.0", features = ["derive"] }
这个包是异步的(async),但你可以选择使用异步运行时,例如tokio,或者简单地阻塞future。你可以启用sync功能,使大多数结构体Sync。这可能会稍微慢一点。
使用这个包可以在不使用serde的情况下进行,但许多功能都需要serde。
运行 Meilisearch 实例
这个包需要 Meilisearch 服务器才能运行。
有很多简单的方法可以下载和运行 Meilisearch 实例。
例如,使用你的终端中的curl命令
# Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch --master-key=masterKey
注意:你还可以从Homebrew或APT下载 Meilisearch。
🚀 快速入门
添加文档
use meilisearch_sdk::client::*;
use serde::{Serialize, Deserialize};
use futures::executor::block_on;
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
    id: usize,
    title: String,
    genres: Vec<String>,
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
    // Create a client (without sending any request so that can't fail)
    let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
    // An index is where the documents are stored.
    let movies = client.index("movies");
    // Add some movies in the index. If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
    movies.add_documents(&[
        Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance".to_string(), "Drama".to_string()] },
        Movie { id: 2, title: String::from("Wonder Woman"), genres: vec!["Action".to_string(), "Adventure".to_string()] },
        Movie { id: 3, title: String::from("Life of Pi"), genres: vec!["Adventure".to_string(), "Drama".to_string()] },
        Movie { id: 4, title: String::from("Mad Max"), genres: vec!["Adventure".to_string(), "Science Fiction".to_string()] },
        Movie { id: 5, title: String::from("Moana"), genres: vec!["Fantasy".to_string(), "Action".to_string()] },
        Movie { id: 6, title: String::from("Philadelphia"), genres: vec!["Drama".to_string()] },
    ], Some("id")).await.unwrap();
}
使用uid,你可以通过任务检查你的文档添加状态(enqueued、canceled、processing、succeeded或failed)。
基本搜索
// Meilisearch is typo-tolerant:
println!("{:?}", client.index("movies_2").search().with_query("caorl").execute::<Movie>().await.unwrap().hits);
输出
[Movie { id: 1, title: String::from("Carol"), genres: vec!["Romance", "Drama"] }]
JSON 输出
{
  "hits": [{
    "id": 1,
    "title": "Carol",
    "genres": ["Romance", "Drama"]
  }],
  "offset": 0,
  "limit": 10,
  "processingTimeMs": 1,
  "query": "caorl"
}
自定义搜索
let search_result = client.index("movies_3")
  .search()
  .with_query("phil")
  .with_attributes_to_highlight(Selectors::Some(&["*"]))
  .execute::<Movie>()
  .await
  .unwrap();
println!("{:?}", search_result.hits);
JSON 输出
{
    "hits": [
        {
            "id": 6,
            "title": "Philadelphia",
            "_formatted": {
                "id": 6,
                "title": "<em>Phil</em>adelphia",
                "genre": ["Drama"]
            }
        }
    ],
    "offset": 0,
    "limit": 20,
    "processingTimeMs": 0,
    "query": "phil"
}
带有过滤器的自定义搜索
如果你想启用过滤,你必须将你的属性添加到filterableAttributes索引设置中。
let filterable_attributes = [
    "id",
    "genres",
];
client.index("movies_4").set_filterable_attributes(&filterable_attributes).await.unwrap();
你只需要执行这个操作一次。
请注意,每当更新filterableAttributes时,Meilisearch 都会重建你的索引。根据你的数据集大小,这可能需要一些时间。你可以使用任务跟踪这个过程。
然后,你可以进行搜索
let search_result = client.index("movies_5")
  .search()
  .with_query("wonder")
  .with_filter("id > 1 AND genres = Action")
  .execute::<Movie>()
  .await
  .unwrap();
println!("{:?}", search_result.hits);
JSON 输出
{
  "hits": [
    {
      "id": 2,
      "title": "Wonder Woman",
      "genres": ["Action", "Adventure"]
    }
  ],
  "offset": 0,
  "limit": 20,
  "estimatedTotalHits": 1,
  "processingTimeMs": 0,
  "query": "wonder"
}
自定义 HttpClient
默认情况下,SDK 使用 reqwest 来进行 HTTP 调用。SDK 允许你通过实现自己的 HttpClient 特性并使用 new_with_client 方法初始化 Client 来自定义 HTTP 客户端。你可能对 futures-unsend 功能感兴趣,它允许你指定一个非-Send HTTP 客户端。
Wasm 支持
SDK 通过 reqwest 支持 wasm。但是,在导入时,你需要启用 futures-unsend 功能。
🌐 使用 WASM 在浏览器中运行
这个包完全支持 WASM。
WASM 和原生版本之间的唯一区别是,原生版本在错误枚举中有更多一种变体(Error::Http)。这应该不会很重要,但我们可以在 WASM 中添加这个变体。
然而,制作一个打算在网页浏览器中运行的程序,需要与 CLI 程序截然不同的设计。要查看一个简单的 Rust 网页应用示例,请参阅我们的演示。
警告:meilisearch-sdk 如果没有可用窗口(例如,Web 扩展)将会崩溃。
🤖 与 Meilisearch 的兼容性
本包保证与Meilisearch v1.x 版本兼容,但某些功能可能不存在。请查看问题以获取更多信息。
⚙️ 贡献
我们非常欢迎在这个项目中做出任何新的贡献!
如果你想了解更多关于开发工作流程或想做出贡献,请访问我们的贡献指南以获取详细说明!
Meilisearch 提供并维护了许多类似这样的 SDK 和集成工具。我们希望为任何类型的项目提供 惊人的搜索体验。如果您想贡献力量、提出建议或只是了解当前的情况,请访问我们的 集成指南 仓库。
依赖关系
~6-20MB
~312K SLoC