
⚡為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和集成工具。我們希望為所有人提供任何類型的項目的驚人搜索體驗。如果您想做出貢獻,提出建議或只是知道現在發生了什麼,請在集成指南存儲庫中訪問我們。