Skip to content

Commit d91aa65

Browse files
started working on similar documents
1 parent fd679c8 commit d91aa65

File tree

2 files changed

+310
-168
lines changed

2 files changed

+310
-168
lines changed

src/indexes.rs

+89-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<Http: HttpClient> Index<Http> {
226226
/// ```
227227
pub async fn execute_query<T: 'static + DeserializeOwned + Send + Sync>(
228228
&self,
229-
body: &SearchQuery<'_, Http>,
229+
body: &SearchQuery<'_, Http, Search<'_>>,
230230
) -> Result<SearchResults<T>, Error> {
231231
self.client
232232
.http_client
@@ -238,6 +238,52 @@ impl<Http: HttpClient> Index<Http> {
238238
.await
239239
}
240240

241+
/// The /similar route uses AI-powered search to return a number of documents similar to a target document.
242+
///
243+
/// See also [`Index::search_similar_documents`].
244+
///
245+
/// # Example
246+
///
247+
/// ```no_run
248+
/// # use serde::{Serialize, Deserialize};
249+
/// # use meilisearch_sdk::{client::*, indexes::*, search::*};
250+
/// #
251+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
252+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
253+
/// #
254+
/// #[derive(Serialize, Deserialize, Debug)]
255+
/// struct Movie {
256+
/// name: String,
257+
/// description: String,
258+
/// }
259+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
260+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
261+
/// let movies = client.index("execute_query_similar");
262+
///
263+
/// // add some documents
264+
/// # 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();
265+
///
266+
/// let query = SearchQuery::new_similar(&movies, "Interstellar").with_limit(5).build();
267+
/// let results = movies.execute_query_similar::<Movie>(&query).await.unwrap();
268+
///
269+
/// assert!(results.hits.len() > 0);
270+
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
271+
/// # });
272+
/// ```
273+
pub async fn execute_query_similar<T: 'static + DeserializeOwned + Send + Sync>(
274+
&self,
275+
body: &SearchQuery<'_, Http, Similar<'_>>,
276+
) -> Result<SearchResults<T>, Error> {
277+
self.client
278+
.http_client
279+
.request::<(), &SearchQuery<Http, Similar>, SearchResults<T>>(
280+
&format!("{}/indexes/{}/similar", self.client.host, self.uid),
281+
Method::Post { body, query: () },
282+
200,
283+
)
284+
.await
285+
}
286+
241287
/// Search for documents matching a specific query in the index.
242288
///
243289
/// See also [`Index::execute_query`].
@@ -279,6 +325,48 @@ impl<Http: HttpClient> Index<Http> {
279325
SearchQuery::new(self)
280326
}
281327

328+
/// Uses AI-powered search to return a number of documents similar to a target document.
329+
///
330+
/// See also [`Index::execute_query`].
331+
///
332+
/// # Example
333+
///
334+
/// ```
335+
/// # use serde::{Serialize, Deserialize};
336+
/// # use meilisearch_sdk::{client::*, indexes::*, search::*};
337+
/// #
338+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
339+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
340+
/// #
341+
/// #[derive(Serialize, Deserialize, Debug)]
342+
/// struct Movie {
343+
/// name: String,
344+
/// description: String,
345+
/// }
346+
///
347+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
348+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
349+
/// meilisearch_sdk::features::ExperimentalFeatures::new(&client).set_vector_store(true).update().unwrap();
350+
/// let mut movies = client.index("search_similar_documents");
351+
///
352+
/// # // add some documents
353+
/// # 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();
354+
///
355+
/// let results = movies.search_similar_documents()
356+
/// .with_limit(5)
357+
/// .execute::<Movie>()
358+
/// .await
359+
/// .unwrap();
360+
///
361+
/// assert!(results.hits.len() > 0);
362+
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
363+
/// # });
364+
/// ```
365+
#[must_use]
366+
pub fn search_similar_documents <'a>(&'a self, id: &'a str) -> SearchQuery<'a, Http, Similar<'a>> {
367+
SearchQuery::new_similar(self, id)
368+
}
369+
282370
/// Get one document using its unique id.
283371
///
284372
/// Serde is needed. Add `serde = {version="1.0", features=["derive"]}` in the dependencies section of your Cargo.toml.

0 commit comments

Comments
 (0)