add * on attributesToRetrieve

This commit is contained in:
Quentin de Quelen 2020-04-09 16:57:08 +02:00
parent 644e78df89
commit c6fb591348
2 changed files with 48 additions and 5 deletions

View File

@ -58,8 +58,20 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
let mut restricted_attributes: HashSet<&str>; let mut restricted_attributes: HashSet<&str>;
match &query.attributes_to_retrieve { match &query.attributes_to_retrieve {
Some(attributes_to_retrieve) => { Some(attributes_to_retrieve) => {
restricted_attributes = attributes_to_retrieve.split(',').collect(); let attributes_to_retrieve: HashSet<&str> = attributes_to_retrieve.split(',').collect();
restricted_attributes.retain(|attr| available_attributes.contains(attr)); 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 => { None => {
restricted_attributes = available_attributes.clone(); restricted_attributes = available_attributes.clone();
@ -94,10 +106,9 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
search_builder.attributes_to_crop(final_attributes); 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<String> = HashSet::new(); let mut final_attributes: HashSet<String> = HashSet::new();
for attribute in attributes_to_highlight.split(',') {
for attribute in inline_attributes.split(',') {
if attribute == "*" { if attribute == "*" {
for attr in &restricted_attributes { for attr in &restricted_attributes {
final_attributes.insert(attr.to_string()); final_attributes.insert(attr.to_string());

View File

@ -458,6 +458,38 @@ fn search_with_attributes_to_retrieve() {
assert_json_eq!(expected, response["hits"].clone(), ordered: false); 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 universes 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 // Search with filter
// q: Captain // q: Captain
// limit: 1 // limit: 1