meilimelo-macros

Crates.iomeilimelo-macros
lib.rsmeilimelo-macros
version0.1.0
sourcesrc
created_at2020-10-19 18:54:08.24098
updated_at2020-10-19 18:54:08.24098
descriptionAPI client to MeiliSearch (macros)
homepagehttps://github.com/apognu/meilimelo
repository
max_upload_size
id303055
size3,499
Antoine POPINEAU (apognu)

documentation

README

meilimelo

Meilimelo is a simple library to perform queries against Meilisearch.

Example

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).

Querying

Most of MeiliSearch's query parameters are handled by meilimelo. They can all be added through the request builder:

Query, filter, limit and offset

meili
  .search("employees")
  .query("johnson")
  .filters("age > 23 AND location = Paris")
  .limit(10)
  .offset(5);

Facets

meili
  .search("employees")
  .facets(FacetBuilder::new("company", "ACME Corp").or("company", "Big Corp").and("roles", "CXM").build())
  .distribution(&["roles"]);

Output settings

meili
  .attributes(&["firstname", "lastname", "bio"])
  .crop(&[Crop::At("bio", 32)])
  .crop_length(10)
  .highlight(&["bio"])
  .matches(true);

Index management

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);
}

Document management

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?;
Commit count: 0

cargo fmt