From c6fb5913484216e12c0be1a1e6d986914f8c556b Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Thu, 9 Apr 2020 16:57:08 +0200 Subject: [PATCH] add * on attributesToRetrieve --- meilisearch-http/src/routes/search.rs | 21 +++++++++++++----- meilisearch-http/tests/search.rs | 32 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index a3a90391c..4d547d268 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -58,8 +58,20 @@ pub async fn search_with_url_query(ctx: Request) -> SResult { let mut restricted_attributes: HashSet<&str>; match &query.attributes_to_retrieve { Some(attributes_to_retrieve) => { - restricted_attributes = attributes_to_retrieve.split(',').collect(); - restricted_attributes.retain(|attr| available_attributes.contains(attr)); + let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect(); + if attributes_to_retrieve.contains("*") { + restricted_attributes = available_attributes.clone(); + } else { + restricted_attributes = HashSet::new(); + for attr in attributes_to_retrieve { + if available_attributes.contains(attr) { + restricted_attributes.insert(attr); + search_builder.add_retrievable_field(attr.to_string()); + } else { + warn!("The attributes {:?} present in attributesToCrop parameter doesn't exist", attr); + } + } + } }, None => { restricted_attributes = available_attributes.clone(); @@ -94,10 +106,9 @@ pub async fn search_with_url_query(ctx: Request) -> SResult { search_builder.attributes_to_crop(final_attributes); } - if let Some(inline_attributes) = query.attributes_to_highlight { + if let Some(attributes_to_highlight) = query.attributes_to_highlight { let mut final_attributes: HashSet = HashSet::new(); - - for attribute in inline_attributes.split(',') { + for attribute in attributes_to_highlight.split(',') { if attribute == "*" { for attr in &restricted_attributes { final_attributes.insert(attr.to_string()); diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index 174894f7a..6dbc9bc3e 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -458,6 +458,38 @@ fn search_with_attributes_to_retrieve() { assert_json_eq!(expected, response["hits"].clone(), ordered: false); } +// Search with attributes to retrieve wildcard +// q: Captain +// limit: 1 +// attributesToRetrieve: * +#[test] +fn search_with_attributes_to_retrieve_wildcard() { + let query = "q=captain&limit=1&attributesToRetrieve=*"; + + let expected = json!([ + { + "id": 299537, + "popularity": 44.726, + "vote_average": 7.0, + "title": "Captain Marvel", + "tagline": "Higher. Further. Faster.", + "overview": "The story follows Carol Danvers as she becomes one of the universe’s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.", + "director": "Ryan Fleck", + "producer": "Kevin Feige", + "genres": [ + "Action", + "Adventure", + "Science Fiction" + ], + "poster_path": "https://image.tmdb.org/t/p/w500/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg", + "vote_count": 7858 + } + ]); + + let (response, _status_code) = GLOBAL_SERVER.lock().unwrap().search(query); + assert_json_eq!(expected, response["hits"].clone(), ordered: false); +} + // Search with filter // q: Captain // limit: 1