Skip to content

Commit 6eb41e8

Browse files
authored
Merge pull request #675 from meilisearch/facet-search
Add facet_search API functionality
2 parents 90a153c + 81d74df commit 6eb41e8

File tree

5 files changed

+395
-5
lines changed

5 files changed

+395
-5
lines changed

.code-samples.meilisearch.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,21 @@ reset_proximity_precision_settings_1: |-
16631663
.reset_proximity_precision()
16641664
.await
16651665
.unwrap();
1666+
facet_search_1: |-
1667+
let res = client.index("books")
1668+
.facet_search("genres")
1669+
.with_facet_query("fiction")
1670+
.with_filter("rating > 3")
1671+
.execute()
1672+
.await
1673+
.unwrap();
1674+
facet_search_3: |-
1675+
let res = client.index("books")
1676+
.facet_search("genres")
1677+
.with_facet_query("c")
1678+
.execute()
1679+
.await
1680+
.unwrap();
16661681
get_search_cutoff_1: |-
16671682
let search_cutoff_ms: String = client
16681683
.index("movies")

src/indexes.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,57 @@ impl<Http: HttpClient> Index<Http> {
279279
SearchQuery::new(self)
280280
}
281281

282+
/// Returns the facet stats matching a specific query in the index.
283+
///
284+
/// See also [`Index::facet_search`].
285+
///
286+
/// # Example
287+
///
288+
/// ```
289+
/// # use serde::{Serialize, Deserialize};
290+
/// # use meilisearch_sdk::{client::*, indexes::*, search::*};
291+
/// #
292+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
293+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
294+
/// #
295+
/// #[derive(Serialize, Deserialize, Debug)]
296+
/// struct Movie {
297+
/// name: String,
298+
/// genre: String,
299+
/// }
300+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
301+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
302+
/// let movies = client.index("execute_query");
303+
///
304+
/// // add some documents
305+
/// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), genre:String::from("scifi")},Movie{name:String::from("Inception"), genre:String::from("drama")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
306+
/// # movies.set_filterable_attributes(["genre"]).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
307+
///
308+
/// let query = FacetSearchQuery::new(&movies, "genre").with_facet_query("scifi").build();
309+
/// let res = movies.execute_facet_query(&query).await.unwrap();
310+
///
311+
/// assert!(res.facet_hits.len() > 0);
312+
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
313+
/// # });
314+
/// ```
315+
pub async fn execute_facet_query(
316+
&self,
317+
body: &FacetSearchQuery<'_, Http>,
318+
) -> Result<FacetSearchResponse, Error> {
319+
self.client
320+
.http_client
321+
.request::<(), &FacetSearchQuery<Http>, FacetSearchResponse>(
322+
&format!("{}/indexes/{}/facet-search", self.client.host, self.uid),
323+
Method::Post { body, query: () },
324+
200,
325+
)
326+
.await
327+
}
328+
329+
pub fn facet_search<'a>(&'a self, facet_name: &'a str) -> FacetSearchQuery<'a, Http> {
330+
FacetSearchQuery::new(self, facet_name)
331+
}
332+
282333
/// Get one document using its unique id.
283334
///
284335
/// Serde is needed. Add `serde = {version="1.0", features=["derive"]}` in the dependencies section of your Cargo.toml.
@@ -484,7 +535,7 @@ impl<Http: HttpClient> Index<Http> {
484535
Error::MeilisearchCommunication(MeilisearchCommunicationError {
485536
status_code: error.status_code,
486537
url: error.url,
487-
message: Some(format!("{}.", MEILISEARCH_VERSION_HINT)),
538+
message: Some(format!("{MEILISEARCH_VERSION_HINT}.")),
488539
})
489540
}
490541
Error::Meilisearch(error) => Error::Meilisearch(MeilisearchError {

src/key.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ pub enum Action {
713713
/// Provides access to the [delete key](https://www.meilisearch.com/docs/reference/api/keys#delete-a-key) endpoint.
714714
#[serde(rename = "keys.delete")]
715715
KeyDelete,
716+
#[serde(rename = "chatCompletions")]
717+
ChatCompletions,
716718
}
717719

718720
#[derive(Debug, Clone, Deserialize)]

src/request.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ pub fn parse_response<Output: DeserializeOwned>(
122122
};
123123
}
124124

125-
warn!(
126-
"Expected response code {}, got {}",
127-
expected_status_code, status_code
128-
);
125+
warn!("Expected response code {expected_status_code}, got {status_code}");
129126

130127
match from_str::<MeilisearchError>(body) {
131128
Ok(e) => Err(Error::from(e)),

0 commit comments

Comments
 (0)