Skip to content

Commit 8a2bded

Browse files
committed
feat: similar query support(no testcase)
1 parent 90a153c commit 8a2bded

File tree

4 files changed

+480
-1
lines changed

4 files changed

+480
-1
lines changed

src/indexes.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
errors::{Error, MeilisearchCommunicationError, MeilisearchError, MEILISEARCH_VERSION_HINT},
55
request::*,
66
search::*,
7+
similar::*,
78
task_info::TaskInfo,
89
tasks::*,
910
DefaultHttpClient,
@@ -1622,6 +1623,50 @@ impl<Http: HttpClient> Index<Http> {
16221623
}
16231624
Ok(task)
16241625
}
1626+
1627+
/// Get similar documents in the index.
1628+
///
1629+
/// # Example
1630+
///
1631+
/// ```
1632+
/// # use serde::{Serialize, Deserialize};
1633+
/// # use meilisearch_sdk::{client::*, indexes::*, similar::*};
1634+
/// #
1635+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1636+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1637+
/// #
1638+
/// #[derive(Serialize, Deserialize, Debug)]
1639+
/// struct Movie {
1640+
/// name: String,
1641+
/// description: String,
1642+
/// }
1643+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1644+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1645+
/// let movies = client.index("execute_query");
1646+
///
1647+
/// // add some documents
1648+
/// # 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();
1649+
///
1650+
/// let query = SimilarQuery::new(&movies, "1", "default").build();
1651+
/// let results = query.similar_query::<Movie>(&query).await.unwrap();
1652+
///
1653+
/// assert!(results.hits.len() > 0);
1654+
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1655+
/// # });
1656+
/// ```
1657+
pub async fn similar_query<T: 'static + DeserializeOwned + Send + Sync>(
1658+
&self,
1659+
body: &SimilarQuery<'_, Http>,
1660+
) -> Result<SimilarResults<T>, Error> {
1661+
self.client
1662+
.http_client
1663+
.request::<(), &SimilarQuery<Http>, SimilarResults<T>>(
1664+
&format!("{}/indexes/{}/similar", self.client.host, self.uid),
1665+
Method::Post { body, query: () },
1666+
200,
1667+
)
1668+
.await
1669+
}
16251670
}
16261671

16271672
impl<Http: HttpClient> AsRef<str> for Index<Http> {

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ mod tenant_tokens;
261261
/// Module containing utilizes functions.
262262
mod utils;
263263

264+
/// Module related to similar queries and results.
265+
pub mod similar;
266+
264267
#[cfg(feature = "reqwest")]
265268
pub mod reqwest;
266269

src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub struct SearchResults<T> {
107107
pub index_uid: Option<String>,
108108
}
109109

110-
fn serialize_with_wildcard<S: Serializer, T: Serialize>(
110+
pub(crate) fn serialize_with_wildcard<S: Serializer, T: Serialize>(
111111
data: &Option<Selectors<T>>,
112112
s: S,
113113
) -> Result<S::Ok, S::Error> {

0 commit comments

Comments
 (0)