Crates.io | meilimelo-macros |
lib.rs | meilimelo-macros |
version | 0.1.0 |
source | src |
created_at | 2020-10-19 18:54:08.24098 |
updated_at | 2020-10-19 18:54:08.24098 |
description | API client to MeiliSearch (macros) |
homepage | https://github.com/apognu/meilimelo |
repository | |
max_upload_size | |
id | 303055 |
size | 3,499 |
Meilimelo is a simple library to perform queries against Meilisearch.
use meilimelo::prelude::*;
#[meilimelo::schema]
struct Employee {
firstname: String,
lastname: String,
roles: Vec<String>
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let meili = MeiliMelo::new("https://meilisearch.example.com:7700")
.with_secret_key("helloworld");
let employees = meili
.search("employees")
.query("johnson")
.run::<Employee>()
.await?;
println!("Hits: {}", people.hits);
for person in &people {
println!("{} {}", person.firstname, person.lastname);
}
Ok(())
}
The meilimelo::schema
attribute macro allows for deriving your schema to something that can be used as a MeiliSearch search result (for example, automatically adding the _formatted
sub-object when needed).
Most of MeiliSearch's query parameters are handled by meilimelo
. They can all be added through the request builder:
meili
.search("employees")
.query("johnson")
.filters("age > 23 AND location = Paris")
.limit(10)
.offset(5);
meili
.search("employees")
.facets(FacetBuilder::new("company", "ACME Corp").or("company", "Big Corp").and("roles", "CXM").build())
.distribution(&["roles"]);
meili
.attributes(&["firstname", "lastname", "bio"])
.crop(&[Crop::At("bio", 32)])
.crop_length(10)
.highlight(&["bio"])
.matches(true);
Basic operation on indexes can be performed:
// Creating index
meili.create_index("employees", "Employees")?;
// Listing indices
for index in &meili.indices().await? {
println!("{}", index.name);
}
You can index a collection of Serialize
documents like so (listing and deleting documents is also supported):
// Indexing documents
let doc = Employee {
firstname: "Luke".to_string(),
lastname: "Skywalker".to_string()
};
meili.insert("employees", vec![doc]);
// Looping over in-order documents
for doc in &meili.list_documents::<Employee>("employees").await? {
println!("{} {}", doc.firstname, doc.lastname);
}
// Getting document via primary key
let doc = meili.get_document::<Employee>("employees", "lskywalker").await?;
println!("{} {}", doc.firstname, doc.lastname);
// Deleting document
meili.delete_document("employees", "lskywalker").await?;