Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting similar documents #600

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
93 changes: 92 additions & 1 deletion src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<Http: HttpClient> Index<Http> {
/// ```
pub async fn execute_query<T: 'static + DeserializeOwned + Send + Sync>(
&self,
body: &SearchQuery<'_, Http>,
body: &SearchQuery<'_, Http, Search<'_>>,
) -> Result<SearchResults<T>, Error> {
self.client
.http_client
Expand All @@ -238,6 +238,52 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// The /similar route uses AI-powered search to return a number of documents similar to a target document.
///
/// See also [`Index::search_similar_documents`].
///
/// # Example
///
/// ```no_run
/// # use serde::{Serialize, Deserialize};
/// # use meilisearch_sdk::{client::*, indexes::*, search::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// #[derive(Serialize, Deserialize, Debug)]
/// struct Movie {
/// name: String,
/// description: String,
/// }
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// let movies = client.index("execute_query_similar");
///
/// // add some documents
/// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")},Movie{name:String::from("Unknown"), description:String::from("Unknown")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
///
/// let query = SearchQuery::new_similar(&movies, "Interstellar").with_limit(5).build();
/// let results = movies.execute_query_similar::<Movie>(&query).await.unwrap();
///
/// assert!(results.hits.len() > 0);
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn execute_query_similar<T: 'static + DeserializeOwned + Send + Sync>(
&self,
body: &SearchQuery<'_, Http, Similar<'_>>,
) -> Result<SearchResults<T>, Error> {
self.client
.http_client
.request::<(), &SearchQuery<Http, Similar>, SearchResults<T>>(
&format!("{}/indexes/{}/similar", self.client.host, self.uid),
Method::Post { body, query: () },
200,
)
.await
}

/// Search for documents matching a specific query in the index.
///
/// See also [`Index::execute_query`].
Expand Down Expand Up @@ -279,6 +325,51 @@ impl<Http: HttpClient> Index<Http> {
SearchQuery::new(self)
}

/// Uses AI-powered search to return a number of documents similar to a target document.
///
/// See also [`Index::execute_query`].
///
/// # Example
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use meilisearch_sdk::{client::*, indexes::*, search::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// #[derive(Serialize, Deserialize, Debug)]
/// struct Movie {
/// name: String,
/// description: String,
/// }
///
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// meilisearch_sdk::features::ExperimentalFeatures::new(&client).set_vector_store(true).update().unwrap();
/// let mut movies = client.index("search_similar_documents");
///
/// # // add some documents
/// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")},Movie{name:String::from("Unknown"), description:String::from("Unknown")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
///
/// let results = movies.search_similar_documents()
/// .with_limit(5)
/// .execute::<Movie>()
/// .await
/// .unwrap();
///
/// assert!(results.hits.len() > 0);
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
#[must_use]
pub fn search_similar_documents<'a>(
&'a self,
id: &'a str,
) -> SearchQuery<'a, Http, Similar<'a>> {
SearchQuery::new_similar(self, id)
}

/// Get one document using its unique id.
///
/// Serde is needed. Add `serde = {version="1.0", features=["derive"]}` in the dependencies section of your Cargo.toml.
Expand Down
Loading
Loading