diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index d403c95d..b718918e 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -315,7 +315,7 @@ update_settings_1: |- "release_date:desc", "rank:desc" ]) - .with_distinct_attribute("movie_id") + .with_distinct_attribute(Some("movie_id")) .with_searchable_attributes([ "title", "overview", diff --git a/meilisearch-index-setting-macro/src/lib.rs b/meilisearch-index-setting-macro/src/lib.rs index 06d3d811..43150932 100644 --- a/meilisearch-index-setting-macro/src/lib.rs +++ b/meilisearch-index-setting-macro/src/lib.rs @@ -1,3 +1,5 @@ +use std::fmt::format; + use convert_case::{Case, Casing}; use proc_macro2::Ident; use quote::quote; @@ -124,7 +126,7 @@ fn get_index_config_implementation( let searchable_attr_tokens = get_settings_token_for_list(&searchable_attributes, "with_searchable_attributes"); let distinct_attr_token = - get_settings_token_for_string(&distinct_key_attribute, "with_distinct_attribute"); + get_settings_token_for_string_for_some_string(&distinct_key_attribute, "with_distinct_attribute"); quote! { #[::meilisearch_sdk::macro_helper::async_trait(?Send)] @@ -187,3 +189,18 @@ fn get_settings_token_for_string( } } } + +fn get_settings_token_for_string_for_some_string( + field_name: &String, + method_name: &str, +) -> proc_macro2::TokenStream { + let method_ident = Ident::new(method_name, proc_macro2::Span::call_site()); + + if field_name.is_empty() { + proc_macro2::TokenStream::new() + } else { + quote! { + .#method_ident(::std::option::Option::Some(#field_name)) + } + } +} diff --git a/src/settings.rs b/src/settings.rs index de5aae70..f6ba1650 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -81,7 +81,7 @@ pub struct Settings { pub sortable_attributes: Option>, /// Search returns documents with distinct (different) values of the given field. #[serde(skip_serializing_if = "Option::is_none")] - pub distinct_attribute: Option, + pub distinct_attribute: Option>, /// Fields in which to search for matching query words sorted by order of importance. #[serde(skip_serializing_if = "Option::is_none")] pub searchable_attributes: Option>, @@ -217,11 +217,19 @@ impl Settings { } #[must_use] - pub fn with_distinct_attribute(self, distinct_attribute: impl AsRef) -> Settings { - Settings { - distinct_attribute: Some(distinct_attribute.as_ref().to_string()), - ..self - } + pub fn with_distinct_attribute(self, distinct_attribute: Option< impl AsRef>) -> Settings { + if let Some(distinct_attribute) = distinct_attribute { + Settings { + distinct_attribute: Some(Some(distinct_attribute.as_ref().to_string())), + ..self + } + } else { + Settings { + distinct_attribute: Some(None), + ..self + } + + } } #[must_use]