Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 7 additions & 40 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ add_or_replace_documents_1: |-
], None).await.unwrap();
add_or_update_documents_1: |-
// Define the type of our documents
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct IncompleteMovie {
id: usize,
title: String
}
impl Document for IncompleteMovie {
type UIDType = usize;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}

let task: Task = client.index("movies").add_or_update(&[
IncompleteMovie {
Expand Down Expand Up @@ -369,7 +365,6 @@ settings_guide_sortable_1: |-
add_movies_json_1: |-
use meilisearch_sdk::{
indexes::*,
document::*,
client::*,
search::*,
settings::*
Expand All @@ -392,15 +387,11 @@ add_movies_json_1: |-
})}
documents_guide_add_movie_1: |-
// Define the type of our documents
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct IncompleteMovie {
id: String,
title: String
}
impl Document for IncompleteMovie {
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(&[
Expand All @@ -412,18 +403,14 @@ documents_guide_add_movie_1: |-
document_guide_create_index_primary_key: |-
client.create_index("movies", Some("reference_number")).await.unwrap();
document_guide_add_document_primary_key: |-
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct Movie {
id: String,
title: String,
poster: String,
overview: String,
release_date: String
}
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}

let task: Task = client.index("movies").add_documents(&[
Movie {
Expand All @@ -447,10 +434,9 @@ getting_started_add_documents_md: |-
```

Documents in the Rust library are strongly typed.
You have to implement the `Document` trait on a struct to be able to use it with Meilisearch.

```rust
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct Movie {
id: String,
title: String,
Expand All @@ -459,35 +445,25 @@ getting_started_add_documents_md: |-
release_date: i64,
genres: Vec<String>
}
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}
```

You will often need this `Movie` struct in other parts of this documentation. (you will have to change it a bit sometimes)
You can also use schemaless values, by putting a `serde_json::Value` inside your own struct like this:

```rust
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct Movie {
id: String,
#[serde(flatten)]
value: serde_json::Value,
}

impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}
```

Then, add documents into the index:

```rust
use meilisearch_sdk::{
indexes::*,
document::*,
client::*,
search::*,
settings::*
Expand Down Expand Up @@ -585,7 +561,7 @@ getting_started_add_meteorites: |-
use serde::Deserialize;
use std::fs::File;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
struct Geo {
lat: f64,
lon: f64
Expand All @@ -601,11 +577,6 @@ getting_started_add_meteorites: |-
_geo: Geo
}

impl Document for Meteorite {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}

let mut file = File::open("meteorites.json")?;
let meteorites: Vec<Meteorite> = serde_json::from_reader(file)?;

Expand Down Expand Up @@ -807,15 +778,11 @@ security_guide_delete_key_1: |-
landing_getting_started_1: |-
let client = Client::new("http://localhost:7700", "masterKey");

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize )]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space detected 🤖

struct Movie {
id: String,
title: String
}
impl Document for Movie {
type UIDType = String;
fn get_uid(&self) -> &Self::UIDType { &self.id }
}

client.index("movies").add_documents(&[
Movie { "id": "1".to_string(), "title": "Carol".to_string() },
Expand Down
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ NB: you can also download Meilisearch from **Homebrew** or **APT**.
#### Add Documents <!-- omit in TOC -->

```rust
use meilisearch_sdk::{document::*, client::*};
use meilisearch_sdk::client::*;
use serde::{Serialize, Deserialize};
use futures::executor::block_on;

Expand All @@ -99,14 +99,6 @@ struct Movie {
genres: Vec<String>,
}

// That trait is required to make a struct usable by an index
impl Document for Movie {
type UIDType = usize;

fn get_uid(&self) -> &Self::UIDType {
&self.id
}
}

fn main() { block_on(async move {
// Create a client (without sending any request so that can't fail)
Expand Down
9 changes: 0 additions & 9 deletions examples/web_app/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use meilisearch_sdk::document::Document;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use yew::prelude::*;
Expand All @@ -15,14 +14,6 @@ pub struct Crate {
}

// Implement the Document trait so that we can use our struct with Meilisearch
impl Document for Crate {
type UIDType = String;

fn get_uid(&self) -> &Self::UIDType {
&self.name
}
}

fn get_readable_download_count(this: &Map<String, Value>) -> String {
if let Some(downloads) = this["downloads"].as_f64() {
if downloads < 1000.0 {
Expand Down
17 changes: 5 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Client {
uid: impl AsRef<str>,
primary_key: Option<&str>,
) -> Result<Task, Error> {
Ok(request::<Value, Task>(
request::<Value, Task>(
&format!("{}/indexes", self.host),
&self.api_key,
Method::Post(json!({
Expand All @@ -185,19 +185,19 @@ impl Client {
})),
202,
)
.await?)
.await
}

/// Delete an index from its UID.
/// To delete an [Index], use the [Index::delete] method.
pub async fn delete_index(&self, uid: impl AsRef<str>) -> Result<Task, Error> {
Ok(request::<(), Task>(
request::<(), Task>(
&format!("{}/indexes/{}", self.host, uid.as_ref()),
&self.api_key,
Method::Delete,
202,
)
.await?)
.await
}

/// Alias for [Client::list_all_indexes].
Expand Down Expand Up @@ -467,7 +467,7 @@ impl Client {
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, document, indexes::*, tasks::Task};
/// # use meilisearch_sdk::{client::*, indexes::*, tasks::Task};
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Serialize, Deserialize, PartialEq)]
Expand All @@ -477,13 +477,6 @@ impl Client {
/// # kind: String,
/// # }
/// #
/// # impl document::Document for Document {
/// # type UIDType = usize;
/// #
/// # fn get_uid(&self) -> &Self::UIDType {
/// # &self.id
/// # }
/// # }
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
Expand Down
42 changes: 0 additions & 42 deletions src/document.rs

This file was deleted.

Loading