
⚡为Rust编写的Meilisearch API客户端?
Meilisearch Rust是Rust开发人员的Meilisearch API客户端。
Meilisearch是一种开源搜索引擎。了解有关Meilisearch的更多信息。
此读数包含您需要开始使用此Meilisearch SDK的所有文档。
有关如何使用Meilisearch(例如我们的API参考,教程,指南和深入文章)的一般信息,请参考我们的主要文档网站。
与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这样的异步运行时或仅在期货上阻止。您可以启用sync功能使大多数结构Sync 。可能会慢一些。
没有SERDE,可以使用此板条箱,但是很多功能都需要Serde。
该板条箱需要运行的Meilisearch服务器。
有许多简单的方法可以下载和运行Meilisearch实例。
例如,使用终端中的curl命令:
# Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch --master-key=masterKeyNB:您也可以从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 ,您可以使用该任务检查文档添加processing状态( enqueued , canceled , 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功能感兴趣,该功能使您可以指定非生物HTTP客户端。
SDK通过ReqWest支持WASM。不过,您需要在导入时启用futures-unsend功能。
该板条箱完全支持WASM。
WASM和本机版本之间的唯一区别是,本机版本在错误枚举中具有另外一个变体( Error::Http )。这应该没关系,但是我们也可以在WASM中添加这个变体。
但是,制作旨在在Web浏览器中运行的程序需要与CLI程序的设计截然不同。要使用Meilisearch查看一个简单的Rust Web应用程序的示例,请参阅我们的演示。
警告:如果没有可用的窗口,则meilisearch-sdk会感到恐慌(例如:Web扩展程序)。
此软件包保证了与Meilisearch版本V1.X版本的兼容性,但某些功能可能不存在。请检查问题以获取更多信息。
在这个项目中,任何新的贡献都非常受欢迎!
如果您想了解有关开发工作流程或想贡献的更多信息,请访问我们的贡献指南以获取详细说明!
Meilisearch提供并维护许多SDK和集成工具。我们希望为所有人提供任何类型的项目的惊人搜索体验。如果您想做出贡献,提出建议或只是知道现在发生了什么,请在集成指南存储库中访问我们。