adds SearchQueryPost

This commit is contained in:
mpostma 2020-06-01 20:22:18 +02:00
parent f6795775e2
commit e5079004e1
3 changed files with 176 additions and 120 deletions

View File

@ -73,6 +73,7 @@ optional = true
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.7" tempdir = "0.3.7"
tokio = { version = "0.2.18", features = ["macros", "time"] } tokio = { version = "0.2.18", features = ["macros", "time"] }
serde_url_params = "0.2.0"
[dev-dependencies.assert-json-diff] [dev-dependencies.assert-json-diff]
git = "https://github.com/qdequele/assert-json-diff" git = "https://github.com/qdequele/assert-json-diff"

View File

@ -4,7 +4,7 @@ use log::warn;
use actix_web::web; use actix_web::web;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use actix_web_macros::{get, post}; use actix_web_macros::{get, post};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use crate::error::{Error, FacetCountError, ResponseError}; use crate::error::{Error, FacetCountError, ResponseError};
@ -21,13 +21,16 @@ pub fn services(cfg: &mut web::ServiceConfig) {
.service(search_with_url_query); .service(search_with_url_query);
} }
#[derive(Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SearchQuery { pub struct SearchQuery {
q: String, q: String,
offset: Option<usize>, offset: Option<usize>,
limit: Option<usize>, limit: Option<usize>,
attributes_to_retrieve: Option<String>, attributes_to_crop: Option<String>, crop_length: Option<usize>, attributes_to_highlight: Option<String>, attributes_to_retrieve: Option<String>,
attributes_to_crop: Option<String>,
crop_length: Option<usize>,
attributes_to_highlight: Option<String>,
filters: Option<String>, filters: Option<String>,
matches: Option<bool>, matches: Option<bool>,
facet_filters: Option<String>, facet_filters: Option<String>,
@ -44,13 +47,48 @@ async fn search_with_url_query(
Ok(HttpResponse::Ok().json(search_result)) Ok(HttpResponse::Ok().json(search_result))
} }
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SearchQueryPost {
q: String,
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<Vec<String>>,
attributes_to_crop: Option<Vec<String>>,
crop_length: Option<usize>,
attributes_to_highlight: Option<Vec<String>>,
filters: Option<String>,
matches: Option<bool>,
facet_filters: Option<Value>,
facets_distribution: Option<Vec<String>>,
}
impl From<SearchQueryPost> for SearchQuery {
fn from(other: SearchQueryPost) -> SearchQuery {
SearchQuery {
q: other.q,
offset: other.offset,
limit: other.limit,
attributes_to_retrieve: other.attributes_to_retrieve.map(|attrs| attrs.join(",")),
attributes_to_crop: other.attributes_to_crop.map(|attrs| attrs.join(",")),
crop_length: other.crop_length,
attributes_to_highlight: other.attributes_to_highlight.map(|attrs| attrs.join(",")),
filters: other.filters,
matches: other.matches,
facet_filters: other.facet_filters.map(|f| f.to_string()),
facets_distribution: other.facets_distribution.map(|f| format!("{:?}", f)),
}
}
}
#[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")] #[post("/indexes/{index_uid}/search", wrap = "Authentication::Public")]
async fn search_with_post( async fn search_with_post(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
params: web::Json<SearchQuery>, params: web::Json<SearchQueryPost>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let search_result = params.search(&path.index_uid, data)?; let query: SearchQuery = params.0.into();
let search_result = query.search(&path.index_uid, data)?;
Ok(HttpResponse::Ok().json(search_result)) Ok(HttpResponse::Ok().json(search_result))
} }

View File

@ -1,5 +1,6 @@
use std::convert::Into; use std::convert::Into;
use meilisearch_http::routes::search::{SearchQuery, SearchQueryPost};
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
use serde_json::json; use serde_json::json;
use serde_json::Value; use serde_json::Value;
@ -8,7 +9,9 @@ mod common;
macro_rules! test_post_get_search { macro_rules! test_post_get_search {
($server:expr, $query:expr, |$response:ident, $status_code:ident | $block:expr) => { ($server:expr, $query:expr, |$response:ident, $status_code:ident | $block:expr) => {
let get_query = ::serde_url_params::to_string(&$query).unwrap(); let post_query: SearchQueryPost = serde_json::from_str(&$query.clone().to_string()).unwrap();
let get_query: SearchQuery = post_query.into();
let get_query = ::serde_url_params::to_string(&get_query).unwrap();
let ($response, $status_code) = $server.search_get(&get_query).await; let ($response, $status_code) = $server.search_get(&get_query).await;
let _ =::std::panic::catch_unwind(|| $block) let _ =::std::panic::catch_unwind(|| $block)
.map_err(|e| panic!("panic in get route: {:?}", e.downcast_ref::<&str>().unwrap())); .map_err(|e| panic!("panic in get route: {:?}", e.downcast_ref::<&str>().unwrap()));
@ -81,7 +84,7 @@ async fn search_with_limit() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -152,7 +155,7 @@ async fn search_with_offset() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -165,7 +168,7 @@ async fn search_with_attribute_to_highlight_wildcard() {
let query = json!({ let query = json!({
"q": "Captain", "q": "Captain",
"limit": 1, "limit": 1,
"attributesToHighlight": "*" "attributesToHighlight": ["*"]
}); });
let expected = json!([ let expected = json!([
@ -205,7 +208,7 @@ async fn search_with_attribute_to_highlight_wildcard() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -218,7 +221,7 @@ async fn search_with_attribute_to_highlight_1() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToHighlight": "title" "attributesToHighlight": ["title"]
}); });
let expected = json!([ let expected = json!([
@ -258,7 +261,7 @@ async fn search_with_attribute_to_highlight_1() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -275,7 +278,7 @@ async fn search_with_attribute_to_highlight_title_tagline() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToHighlight": "title,tagline" "attributesToHighlight": ["title","tagline"]
}); });
let expected = json!([ let expected = json!([
@ -315,7 +318,7 @@ async fn search_with_attribute_to_highlight_title_tagline() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -328,7 +331,7 @@ async fn search_with_attribute_to_highlight_title_overview() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToHighlight": "title,overview" "attributesToHighlight": ["title","overview"]
}); });
let expected = json!([ let expected = json!([
@ -368,7 +371,7 @@ async fn search_with_attribute_to_highlight_title_overview() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -418,7 +421,7 @@ async fn search_with_matches() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -431,7 +434,7 @@ async fn search_witch_crop() {
let query = json!({ let query = json!({
"q": "Captain", "q": "Captain",
"limit": 1, "limit": 1,
"attributesToCrop": "overview", "attributesToCrop": ["overview"],
"cropLength": 20 "cropLength": 20
}); });
@ -472,7 +475,7 @@ async fn search_witch_crop() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -485,7 +488,7 @@ async fn search_with_attributes_to_retrieve() {
let query = json!({ let query = json!({
"q": "Captain", "q": "Captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,tagline,overview,poster_path" "attributesToRetrieve": ["title","tagline","overview","poster_path"],
}); });
let expected = json!([ let expected = json!([
@ -497,7 +500,7 @@ async fn search_with_attributes_to_retrieve() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -510,7 +513,7 @@ async fn search_with_attributes_to_retrieve_wildcard() {
let query = json!({ let query = json!({
"q": "Captain", "q": "Captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "*" "attributesToRetrieve": ["*"],
}); });
let expected = json!([ let expected = json!([
@ -533,7 +536,7 @@ async fn search_with_attributes_to_retrieve_wildcard() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -607,7 +610,7 @@ async fn search_with_filter() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
@ -636,7 +639,7 @@ async fn search_with_filter() {
"filters": "title='american pie 2'" "filters": "title='american pie 2'"
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
@ -681,7 +684,7 @@ async fn search_with_filter() {
"limit": 3, "limit": 3,
"filters": "director='Anthony Russo' AND (title='captain america: civil war' OR title='Captain America: The Winter Soldier')" "filters": "director='Anthony Russo' AND (title='captain america: civil war' OR title='Captain America: The Winter Soldier')"
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
@ -744,7 +747,7 @@ async fn search_with_filter() {
"limit": 3, "limit": 3,
"filters": "director='anthony russo' AND (title = 'captain america: civil war' OR vote_average > 8.0)" "filters": "director='anthony russo' AND (title = 'captain america: civil war' OR vote_average > 8.0)"
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
@ -807,7 +810,7 @@ async fn search_with_filter() {
"filters": "NOT director = 'anthony russo' AND vote_average > 7.5" "filters": "NOT director = 'anthony russo' AND vote_average > 7.5"
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
@ -817,7 +820,7 @@ async fn search_with_filter() {
"filters": "NOT director = 'anthony russo' AND title='Avengers: Endgame'" "filters": "NOT director = 'anthony russo' AND title='Avengers: Endgame'"
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -835,8 +838,8 @@ async fn search_with_attributes_to_highlight_and_matches() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToHighlight": "title,overview", "attributesToHighlight": ["title","overview"],
"matches": true "matches": true,
}); });
let expected = json!( [ let expected = json!( [
@ -890,7 +893,7 @@ async fn search_with_attributes_to_highlight_and_matches() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -903,9 +906,9 @@ async fn search_with_attributes_to_highlight_and_matches_and_crop() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToCrop": "overview", "attributesToCrop": ["overview"],
"cropLength": 20, "cropLength": 20,
"attributesToHighlight": "title,overview", "attributesToHighlight": ["title","overview"],
"matches": true "matches": true
}); });
@ -960,7 +963,7 @@ async fn search_with_attributes_to_highlight_and_matches_and_crop() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -973,8 +976,8 @@ async fn search_with_differents_attributes() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToHighlight": "title", "attributesToHighlight": ["title"],
}); });
let expected = json!([ let expected = json!([
@ -988,7 +991,7 @@ async fn search_with_differents_attributes() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1001,8 +1004,8 @@ async fn search_with_differents_attributes_2() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "overview", "attributesToCrop": ["overview"],
"cropLength": 10, "cropLength": 10,
}); });
@ -1017,7 +1020,7 @@ async fn search_with_differents_attributes_2() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1035,8 +1038,8 @@ async fn search_with_differents_attributes_3() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "overview:10", "attributesToCrop": ["overview:10"],
}); });
let expected = json!([ let expected = json!([
@ -1050,7 +1053,7 @@ async fn search_with_differents_attributes_3() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1068,8 +1071,8 @@ async fn search_with_differents_attributes_4() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "overview:10,title:0", "attributesToCrop": ["overview:10","title:0"],
}); });
let expected = json!([ let expected = json!([
@ -1084,7 +1087,7 @@ async fn search_with_differents_attributes_4() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1102,8 +1105,8 @@ async fn search_with_differents_attributes_5() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "*,overview:10", "attributesToCrop": ["*","overview:10"],
}); });
let expected = json!([ let expected = json!([
@ -1120,7 +1123,7 @@ async fn search_with_differents_attributes_5() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1139,9 +1142,9 @@ async fn search_with_differents_attributes_6() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "*,overview:10", "attributesToCrop": ["*","overview:10"],
"attributesToHighlight": "title" "attributesToHighlight": ["title"],
}); });
let expected = json!([ let expected = json!([
@ -1158,7 +1161,7 @@ async fn search_with_differents_attributes_6() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1177,9 +1180,9 @@ async fn search_with_differents_attributes_7() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "*,overview:10", "attributesToCrop": ["*","overview:10"],
"attributesToHighlight": "*" "attributesToHighlight": ["*"],
}); });
let expected = json!([ let expected = json!([
@ -1196,7 +1199,7 @@ async fn search_with_differents_attributes_7() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1215,9 +1218,9 @@ async fn search_with_differents_attributes_8() {
let query = json!({ let query = json!({
"q": "captain", "q": "captain",
"limit": 1, "limit": 1,
"attributesToRetrieve": "title,producer,director", "attributesToRetrieve": ["title","producer","director"],
"attributesToCrop": "*,overview:10", "attributesToCrop": ["*","overview:10"],
"attributesToHighlight": "*,tagline" "attributesToHighlight": ["*","tagline"],
}); });
let expected = json!([ let expected = json!([
@ -1235,7 +1238,7 @@ async fn search_with_differents_attributes_8() {
} }
]); ]);
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false); assert_json_eq!(expected.clone(), response["hits"].clone(), ordered: false);
}); });
} }
@ -1253,10 +1256,10 @@ async fn test_faceted_search_valid() {
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"color:green\"]" "facetFilters": ["color:green"]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1269,10 +1272,10 @@ async fn test_faceted_search_valid() {
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[[\"color:blue\"]]" "facetFilters": [["color:blue"]]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1285,10 +1288,10 @@ async fn test_faceted_search_valid() {
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"color:Blue\"]" "facetFilters": ["color:Blue"]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1308,9 +1311,9 @@ async fn test_faceted_search_valid() {
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"tags:bug\"]" "facetFilters": ["tags:bug"]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1324,9 +1327,9 @@ async fn test_faceted_search_valid() {
// test and: ["color:blue", "tags:bug"] // test and: ["color:blue", "tags:bug"]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"color:blue\", \"tags:bug\"]" "facetFilters": ["color:blue", "tags:bug"]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1343,9 +1346,9 @@ async fn test_faceted_search_valid() {
// test or: [["color:blue", "color:green"]] // test or: [["color:blue", "color:green"]]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[[\"color:blue\", \"color:green\"]]" "facetFilters": [["color:blue", "color:green"]]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1364,9 +1367,9 @@ async fn test_faceted_search_valid() {
// test and-or: ["tags:bug", ["color:blue", "color:green"]] // test and-or: ["tags:bug", ["color:blue", "color:green"]]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"tags:bug\", [\"color:blue\", \"color:green\"]]" "facetFilters": ["tags:bug", ["color:blue", "color:green"]]
}); });
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty()); assert!(!response.get("hits").unwrap().as_array().unwrap().is_empty());
assert!(response assert!(response
.get("hits") .get("hits")
@ -1398,9 +1401,10 @@ async fn test_faceted_search_invalid() {
//no faceted attributes set //no faceted attributes set
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"color:blue\"]" "facetFilters": ["color:blue"]
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
@ -1414,27 +1418,31 @@ async fn test_faceted_search_invalid() {
// [] // []
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[]" "facetFilters": []
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
// [[]] // [[]]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[[]]" "facetFilters": [[]]
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
// ["color:green", []] // ["color:green", []]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[\"color:green\", []]" "facetFilters": ["color:green", []]
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
@ -1443,27 +1451,32 @@ async fn test_faceted_search_invalid() {
// [[[]]] // [[[]]]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[[[]]]" "facetFilters": [[[]]]
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
// [["color:green", ["color:blue"]]] // [["color:green", ["color:blue"]]]
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "[[\"color:green\", [\"color:blue\"]]]" "facetFilters": [["color:green", ["color:blue"]]]
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
// "color:green" // "color:green"
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetFilters": "\"color:green\"" "facetFilters": "color:green"
}); });
test_post_get!(server, query, |response, status_code| {
test_post_get_search!(server, query, |response, status_code| {
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
assert_eq!(response["errorCode"], "invalid_facet"); assert_eq!(response["errorCode"], "invalid_facet");
}); });
@ -1477,7 +1490,7 @@ async fn test_facet_count() {
let query = json!({ let query = json!({
"q": "a", "q": "a",
}); });
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
assert!(response.get("exhaustiveFacetsCount").is_none()); assert!(response.get("exhaustiveFacetsCount").is_none());
assert!(response.get("facetsDistribution").is_none()); assert!(response.get("facetsDistribution").is_none());
}); });
@ -1485,9 +1498,9 @@ async fn test_facet_count() {
// test no facets set, search on color // test no facets set, search on color
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"color\"]" "facetsDistribution": ["color"]
}); });
test_post_get!(server, query.clone(), |_response, status_code|{ test_post_get_search!(server, query.clone(), |_response, status_code|{
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
}); });
@ -1496,7 +1509,7 @@ async fn test_facet_count() {
}); });
server.update_all_settings(body).await; server.update_all_settings(body).await;
// same as before, but now facets are set: // same as before, but now facets are set:
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
println!("{}", response); println!("{}", response);
assert!(response.get("exhaustiveFacetsCount").is_some()); assert!(response.get("exhaustiveFacetsCount").is_some());
assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 1); assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 1);
@ -1504,9 +1517,9 @@ async fn test_facet_count() {
// searching on color and tags // searching on color and tags
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"color\", \"tags\"]" "facetsDistribution": ["color", "tags"]
}); });
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
let facets = response.get("facetsDistribution").unwrap().as_object().unwrap(); let facets = response.get("facetsDistribution").unwrap().as_object().unwrap();
assert_eq!(facets.values().count(), 2); assert_eq!(facets.values().count(), 2);
assert_ne!(!facets.get("color").unwrap().as_object().unwrap().values().count(), 0); assert_ne!(!facets.get("color").unwrap().as_object().unwrap().values().count(), 0);
@ -1515,53 +1528,57 @@ async fn test_facet_count() {
// wildcard // wildcard
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"*\"]" "facetsDistribution": ["*"]
}); });
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 2); assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 2);
}); });
// wildcard with other attributes: // wildcard with other attributes:
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"color\", \"*\"]" "facetsDistribution": ["color", "*"]
}); });
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 2); assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 2);
}); });
// empty facet list // empty facet list
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[]" "facetsDistribution": []
}); });
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 0); assert_eq!(response.get("facetsDistribution").unwrap().as_object().unwrap().values().count(), 0);
}); });
// attr not set as facet passed: // attr not set as facet passed:
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"gender\"]" "facetsDistribution": ["gender"]
}); });
test_post_get!(server, query, |_response, status_code|{ test_post_get_search!(server, query, |_response, status_code|{
assert_eq!(status_code, 400); assert_eq!(status_code, 400);
}); });
}
#[actix_rt::test]
#[should_panic]
async fn test_bad_facet_distribution() {
let mut server = common::Server::test_server().await;
// string instead of array: // string instead of array:
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "\"color\"" "facetsDistribution": "color"
});
test_post_get!(server, query, |_response, status_code|{
assert_eq!(status_code, 400);
}); });
test_post_get_search!(server, query, |_response, _status_code| {});
// invalid value in array: // invalid value in array:
let query = json!({ let query = json!({
"q": "a", "q": "a",
"facetsDistribution": "[\"color\", true]" "facetsDistribution": ["color", true]
});
test_post_get!(server, query, |_response, status_code|{
assert_eq!(status_code, 400);
}); });
test_post_get_search!(server, query, |_response, _status_code| {});
} }
#[actix_rt::test] #[actix_rt::test]
@ -1590,12 +1607,12 @@ async fn highlight_cropped_text() {
//let query = "q=insert&attributesToHighlight=*&attributesToCrop=body&cropLength=30"; //let query = "q=insert&attributesToHighlight=*&attributesToCrop=body&cropLength=30";
let query = json!({ let query = json!({
"q": "insert", "q": "insert",
"attributesToHighlight": "*", "attributesToHighlight": ["*"],
"attributesToCrop": "body", "attributesToCrop": ["body"],
"cropLength": 30, "cropLength": 30,
}); });
let expected_response = "that, try the following: \n1. <em>insert</em> your trip\n2. google your"; let expected_response = "that, try the following: \n1. <em>insert</em> your trip\n2. google your";
test_post_get!(server, query, |response, _status_code|{ test_post_get_search!(server, query, |response, _status_code|{
assert_eq!(response assert_eq!(response
.get("hits") .get("hits")
.unwrap() .unwrap()
@ -1617,12 +1634,12 @@ async fn highlight_cropped_text() {
//let query = "q=insert&attributesToHighlight=*&attributesToCrop=body&cropLength=80"; //let query = "q=insert&attributesToHighlight=*&attributesToCrop=body&cropLength=80";
let query = json!({ let query = json!({
"q": "insert", "q": "insert",
"attributesToHighlight": "*", "attributesToHighlight": ["*"],
"attributesToCrop": "body", "attributesToCrop": ["body"],
"cropLength": 80, "cropLength": 80,
}); });
let expected_response = "well, it may not work like that, try the following: \n1. <em>insert</em> your trip\n2. google your `searchQuery`\n3. find a solution \n> say hello"; let expected_response = "well, it may not work like that, try the following: \n1. <em>insert</em> your trip\n2. google your `searchQuery`\n3. find a solution \n> say hello";
test_post_get!(server, query, |response, _status_code| { test_post_get_search!(server, query, |response, _status_code| {
assert_eq!(response assert_eq!(response
.get("hits") .get("hits")
.unwrap() .unwrap()