File tree Expand file tree Collapse file tree 3 files changed +33
-8
lines changed
meilisearch-index-setting-macro/src Expand file tree Collapse file tree 3 files changed +33
-8
lines changed Original file line number Diff line number Diff line change @@ -315,7 +315,7 @@ update_settings_1: |-
315
315
"release_date:desc",
316
316
"rank:desc"
317
317
])
318
- .with_distinct_attribute("movie_id")
318
+ .with_distinct_attribute(Some( "movie_id") )
319
319
.with_searchable_attributes([
320
320
"title",
321
321
"overview",
Original file line number Diff line number Diff line change
1
+ use std:: fmt:: format;
2
+
1
3
use convert_case:: { Case , Casing } ;
2
4
use proc_macro2:: Ident ;
3
5
use quote:: quote;
@@ -124,7 +126,7 @@ fn get_index_config_implementation(
124
126
let searchable_attr_tokens =
125
127
get_settings_token_for_list ( & searchable_attributes, "with_searchable_attributes" ) ;
126
128
let distinct_attr_token =
127
- get_settings_token_for_string ( & distinct_key_attribute, "with_distinct_attribute" ) ;
129
+ get_settings_token_for_string_for_some_string ( & distinct_key_attribute, "with_distinct_attribute" ) ;
128
130
129
131
quote ! {
130
132
#[ :: meilisearch_sdk:: macro_helper:: async_trait( ?Send ) ]
@@ -187,3 +189,18 @@ fn get_settings_token_for_string(
187
189
}
188
190
}
189
191
}
192
+
193
+ fn get_settings_token_for_string_for_some_string (
194
+ field_name : & String ,
195
+ method_name : & str ,
196
+ ) -> proc_macro2:: TokenStream {
197
+ let method_ident = Ident :: new ( method_name, proc_macro2:: Span :: call_site ( ) ) ;
198
+
199
+ if field_name. is_empty ( ) {
200
+ proc_macro2:: TokenStream :: new ( )
201
+ } else {
202
+ quote ! {
203
+ . #method_ident( :: std:: option:: Option :: Some ( #field_name) )
204
+ }
205
+ }
206
+ }
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ pub struct Settings {
81
81
pub sortable_attributes : Option < Vec < String > > ,
82
82
/// Search returns documents with distinct (different) values of the given field.
83
83
#[ serde( skip_serializing_if = "Option::is_none" ) ]
84
- pub distinct_attribute : Option < String > ,
84
+ pub distinct_attribute : Option < Option < String > > ,
85
85
/// Fields in which to search for matching query words sorted by order of importance.
86
86
#[ serde( skip_serializing_if = "Option::is_none" ) ]
87
87
pub searchable_attributes : Option < Vec < String > > ,
@@ -217,11 +217,19 @@ impl Settings {
217
217
}
218
218
219
219
#[ must_use]
220
- pub fn with_distinct_attribute ( self , distinct_attribute : impl AsRef < str > ) -> Settings {
221
- Settings {
222
- distinct_attribute : Some ( distinct_attribute. as_ref ( ) . to_string ( ) ) ,
223
- ..self
224
- }
220
+ pub fn with_distinct_attribute ( self , distinct_attribute : Option < impl AsRef < str > > ) -> Settings {
221
+ if let Some ( distinct_attribute) = distinct_attribute {
222
+ Settings {
223
+ distinct_attribute : Some ( Some ( distinct_attribute. as_ref ( ) . to_string ( ) ) ) ,
224
+ ..self
225
+ }
226
+ } else {
227
+ Settings {
228
+ distinct_attribute : Some ( None ) ,
229
+ ..self
230
+ }
231
+
232
+ }
225
233
}
226
234
227
235
#[ must_use]
You can’t perform that action at this time.
0 commit comments