meilisearch / meilisearch-rust

Rust wrapper for the Meilisearch API.

Home Page:https://www.meilisearch.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove Document trait asking for UIDType

bidoubiwa opened this issue · comments

Currently to communicate a primary key on document addition the document structure must implement the following trait:

// src/document.rs
pub trait Document: DeserializeOwned + std::fmt::Debug + Serialize {
    /// The type of the primary key
    type UIDType: Display;

    /// The method returning the primary key of the Document.
    ///
    /// **WARNING**! This method **MUST** only return an object that displays himself only using alphanumeric characters, '/' and '-'.
    /// Otherwise, the Meilisearch server will reject your document.
    fn get_uid(&self) -> &Self::UIDType;
}

Here UIDType is the primary key. This trait is not very intuitive and makes the code harder to use than it should be.
The naming is not very clear and it requires an additional step of implementation on all document addition even when the user does not need to communicate the primary key.

for example:

  // Define the type of our documents
  #[derive(Serialize, Deserialize, Debug)]
  struct Movie {
    id: String,
    title: String
  }
  impl Document for Movie {
    type UIDType = String;
    fn get_uid(&self) -> &Self::UIDType { &self.id }
  }

  // Add a document to our index
  let task: Task = client.index("movies").add_documents(&[
    Movie {
      id: "123sq178".to_string(),
      title: "Amélie Poulain".to_string(),
    }
  ], None).await.unwrap();

and when the user wants to communicate the primary key he still needs to give it as the second parameter of add_documents

add_documents(documents, Some("id"))
commented

Yep, I really don't see why we should have this trait instead of simply accepting anything that implements Serialize / Deserialize.
Also, the get_uid method is not even used to « guess » the primary key by meilisearch.
I don't know why we have this and I think we should remove it.

If you have the time I would love to hear what you think of it @sanders41 or @Mubelotix 😁

I don't know of any reason it is really needed. @Mubelotix may know some history that I am not aware of that makes it necessary.