meilisearch-rust icon indicating copy to clipboard operation
meilisearch-rust copied to clipboard

Create a macro to ease the configuration of indexes

Open irevoire opened this issue 2 years ago • 1 comments

Description It would be cool if we could somehow create a kind of builder macro but with the settings of Meilisearch. Some settings do not apply here; you can't set your stopwords or synonyms from a document for example. But anything related to a field should be handled without any issue I think :thinking:

Basic example

#[derive(Serialize, Deserialize, Document)]
struct Movie {
  #[document(primary_key)]
  movie_id: u64,
  #[document(displayed, searchable)]
  title: String,
  #[document(displayed)]
  description: String,
  #[document(filterable, sortable, displayed)]
  release_date: String,
  #[document(filterable, displayed)]
  genres: Vec<String>,
}

I'm not sure Document is the best name here.

This should generate something like that;

// this trait is not generated, it's just hanging somewhere in our crate
pub trait Document {
  fn generate_settings() -> Settings;
  async fn generate_index() -> Result<Index>;
}

impl Document for Movie {
  fn generate_settings() -> Settings {
    Settings::new()
        .with_displayed_attributes(["title", "description", "release_date", "genres"])
        .with_searchable_attributes(["title"])
        .with_filterable_attributes(["release_date", "genres"])
        .with_sortable_attributes(["release_date"])
  }

  async fn generate_index(client: &Client) -> Result<Index> {
    client.create_index("movie", Some("movie_id"))?
        .wait_for_completion(client)?
        .try_make_index(client)
  }
}

The macro should check as many errors as possible at compile time. Like setting two primary keys or two distinct attributes.

I think that would be a great addition to the crate and make it way more accessible / easier to test.

Other

I don't think I'll have the time to work on this issue, so if someone else thinks it's a good addition and wants to implement it, please do not hesitate to ask questions when you're not sure about something!

Also, if you don't know anything about rust macros but would like to learn, you can start by following this workshop; https://github.com/dtolnay/proc-macro-workshop

irevoire avatar Apr 21 '22 13:04 irevoire

I'll try this.

romilpunetha avatar Oct 13 '22 12:10 romilpunetha

Could you point me to the Document trait mentioned in the description? I can see a mention in the lib.rs as well, but I'm unable to find its declaration in the code base. It's the only piece left in the implementation.

romilpunetha avatar Oct 16 '22 19:10 romilpunetha

Hey @romilpunetha :) Document is a structure that you define yourself containing the structure of your documents!

For example https://github.com/meilisearch/meilisearch-rust/blob/920cdd6975b243e37c934047e3d9f44cf212e0f8/src/search.rs#L399-L426

The mentioned lib provided the structures related to the documents API:

https://github.com/meilisearch/meilisearch-rust/blob/920cdd6975b243e37c934047e3d9f44cf212e0f8/src/documents.rs#L5-L20

bidoubiwa avatar Oct 17 '22 11:10 bidoubiwa

I don't think I did a good job of explaining the problem. Let me try again:

// this trait is not generated, it's just hanging somewhere in our crate
pub trait Document {
  fn generate_settings() -> Settings;
  async fn generate_index() -> Result<Index>;
}

This part in the description of the issue states that a Document trait exists somewhere in the crates. I couldn't find the trait and the requirement is that this trait needs to be implemented for the structs with the macro. If this trait does not exist, I'll create it. If it does, I'm unable to locate it. Or am I still missing something here?

romilpunetha avatar Oct 17 '22 15:10 romilpunetha

I think, from what I understand of the issue, is that Document does not exist and should be created with a better name.

@irevoire can you clarify the situation?

bidoubiwa avatar Oct 17 '22 15:10 bidoubiwa

Fixed by #358

bidoubiwa avatar Sep 25 '23 13:09 bidoubiwa