chore(search): rename in the search endpoint

Fix ##2376
This commit is contained in:
Irevoire 2022-05-18 13:17:56 +02:00 committed by Tamo
parent ae4e419db4
commit 4e9accdeb7
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
6 changed files with 50 additions and 67 deletions

View File

@ -363,7 +363,7 @@ pub struct SearchAggregator {
highlight_pre_tag: bool, highlight_pre_tag: bool,
highlight_post_tag: bool, highlight_post_tag: bool,
crop_marker: bool, crop_marker: bool,
matches: bool, show_matches_position: bool,
crop_length: bool, crop_length: bool,
} }
@ -419,7 +419,7 @@ impl SearchAggregator {
ret.highlight_post_tag = query.highlight_post_tag != DEFAULT_HIGHLIGHT_POST_TAG; ret.highlight_post_tag = query.highlight_post_tag != DEFAULT_HIGHLIGHT_POST_TAG;
ret.crop_marker = query.crop_marker != DEFAULT_CROP_MARKER; ret.crop_marker = query.crop_marker != DEFAULT_CROP_MARKER;
ret.crop_length = query.crop_length != DEFAULT_CROP_LENGTH; ret.crop_length = query.crop_length != DEFAULT_CROP_LENGTH;
ret.matches = query.matches; ret.show_matches_position = query.show_matches_position;
ret ret
} }
@ -472,7 +472,7 @@ impl SearchAggregator {
self.highlight_pre_tag |= other.highlight_pre_tag; self.highlight_pre_tag |= other.highlight_pre_tag;
self.highlight_post_tag |= other.highlight_post_tag; self.highlight_post_tag |= other.highlight_post_tag;
self.crop_marker |= other.crop_marker; self.crop_marker |= other.crop_marker;
self.matches |= other.matches; self.show_matches_position |= other.show_matches_position;
self.crop_length |= other.crop_length; self.crop_length |= other.crop_length;
} }
@ -515,7 +515,7 @@ impl SearchAggregator {
"highlight_pre_tag": self.highlight_pre_tag, "highlight_pre_tag": self.highlight_pre_tag,
"highlight_post_tag": self.highlight_post_tag, "highlight_post_tag": self.highlight_post_tag,
"crop_marker": self.crop_marker, "crop_marker": self.crop_marker,
"matches": self.matches, "show_matches_position": self.show_matches_position,
"crop_length": self.crop_length, "crop_length": self.crop_length,
}, },
}); });

View File

@ -36,8 +36,8 @@ pub struct SearchQueryGet {
filter: Option<String>, filter: Option<String>,
sort: Option<String>, sort: Option<String>,
#[serde(default = "Default::default")] #[serde(default = "Default::default")]
matches: bool, show_matches_position: bool,
facets_distribution: Option<String>, facets: Option<String>,
#[serde(default = "default_highlight_pre_tag")] #[serde(default = "default_highlight_pre_tag")]
highlight_pre_tag: String, highlight_pre_tag: String,
#[serde(default = "default_highlight_post_tag")] #[serde(default = "default_highlight_post_tag")]
@ -60,8 +60,8 @@ impl From<SearchQueryGet> for SearchQuery {
.attributes_to_highlight .attributes_to_highlight
.map(|attrs| attrs.split(',').map(String::from).collect()); .map(|attrs| attrs.split(',').map(String::from).collect());
let facets_distribution = other let facets = other
.facets_distribution .facets
.map(|attrs| attrs.split(',').map(String::from).collect()); .map(|attrs| attrs.split(',').map(String::from).collect());
let filter = match other.filter { let filter = match other.filter {
@ -84,8 +84,8 @@ impl From<SearchQueryGet> for SearchQuery {
attributes_to_highlight, attributes_to_highlight,
filter, filter,
sort, sort,
matches: other.matches, show_matches_position: other.show_matches_position,
facets_distribution, facets,
highlight_pre_tag: other.highlight_pre_tag, highlight_pre_tag: other.highlight_pre_tag,
highlight_post_tag: other.highlight_post_tag, highlight_post_tag: other.highlight_post_tag,
crop_marker: other.crop_marker, crop_marker: other.crop_marker,
@ -169,10 +169,6 @@ pub async fn search_with_url_query(
let search_result = search_result?; let search_result = search_result?;
// Tests that the nb_hits is always set to false
#[cfg(test)]
assert!(!search_result.exhaustive_nb_hits);
debug!("returns: {:?}", search_result); debug!("returns: {:?}", search_result);
Ok(HttpResponse::Ok().json(search_result)) Ok(HttpResponse::Ok().json(search_result))
} }
@ -207,10 +203,6 @@ pub async fn search_with_post(
let search_result = search_result?; let search_result = search_result?;
// Tests that the nb_hits is always set to false
#[cfg(test)]
assert!(!search_result.exhaustive_nb_hits);
debug!("returns: {:?}", search_result); debug!("returns: {:?}", search_result);
Ok(HttpResponse::Ok().json(search_result)) Ok(HttpResponse::Ok().json(search_result))
} }

View File

@ -16,7 +16,7 @@ async fn formatted_contain_wildcard() {
index.wait_task(1).await; index.wait_task(1).await;
let (response, code) = index let (response, code) = index
.search_post(json!({ "q": "pesti", "attributesToRetrieve": ["father", "mother"], "attributesToHighlight": ["father", "mother", "*"], "attributesToCrop": ["doggos"], "matches": true })) .search_post(json!({ "q": "pesti", "attributesToRetrieve": ["father", "mother"], "attributesToHighlight": ["father", "mother", "*"], "attributesToCrop": ["doggos"], "showMatchesPosition": true }))
.await; .await;
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
assert_eq!( assert_eq!(
@ -26,7 +26,7 @@ async fn formatted_contain_wildcard() {
"id": "852", "id": "852",
"cattos": "<em>pesti</em>", "cattos": "<em>pesti</em>",
}, },
"_matchesInfo": {"cattos": [{"start": 0, "length": 5}]}, "_matchesPosition": {"cattos": [{"start": 0, "length": 5}]},
}) })
); );
@ -44,7 +44,7 @@ async fn formatted_contain_wildcard() {
let (response, code) = index let (response, code) = index
.search_post( .search_post(
json!({ "q": "pesti", "attributesToRetrieve": ["*"], "attributesToHighlight": ["id"], "matches": true }), json!({ "q": "pesti", "attributesToRetrieve": ["*"], "attributesToHighlight": ["id"], "showMatchesPosition": true }),
) )
.await; .await;
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -57,7 +57,7 @@ async fn formatted_contain_wildcard() {
"id": "852", "id": "852",
"cattos": "pesti", "cattos": "pesti",
}, },
"_matchesInfo": {"cattos": [{"start": 0, "length": 5}]}, "_matchesPosition": {"cattos": [{"start": 0, "length": 5}]},
}) })
); );
@ -145,7 +145,7 @@ async fn format_nested() {
let (response, code) = index let (response, code) = index
.search_post( .search_post(
json!({ "q": "bobby", "attributesToRetrieve": ["doggos.name"], "matches": true }), json!({ "q": "bobby", "attributesToRetrieve": ["doggos.name"], "showMatchesPosition": true }),
) )
.await; .await;
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
@ -160,7 +160,7 @@ async fn format_nested() {
"name": "buddy", "name": "buddy",
}, },
], ],
"_matchesInfo": {"doggos.name": [{"start": 0, "length": 5}]}, "_matchesPosition": {"doggos.name": [{"start": 0, "length": 5}]},
}) })
); );

View File

@ -420,11 +420,11 @@ async fn search_facet_distribution() {
index index
.search( .search(
json!({ json!({
"facetsDistribution": ["title"] "facets": ["title"]
}), }),
|response, code| { |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
let dist = response["facetsDistribution"].as_object().unwrap(); let dist = response["facetDistribution"].as_object().unwrap();
assert_eq!(dist.len(), 1); assert_eq!(dist.len(), 1);
assert!(dist.get("title").is_some()); assert!(dist.get("title").is_some());
}, },
@ -445,12 +445,12 @@ async fn search_facet_distribution() {
index index
.search( .search(
json!({ json!({
// "facetsDistribution": ["father", "doggos.name"] // "facets": ["father", "doggos.name"]
"facetsDistribution": ["father"] "facets": ["father"]
}), }),
|response, code| { |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
let dist = response["facetsDistribution"].as_object().unwrap(); let dist = response["facetDistribution"].as_object().unwrap();
assert_eq!(dist.len(), 1); assert_eq!(dist.len(), 1);
assert_eq!( assert_eq!(
dist["father"], dist["father"],
@ -474,11 +474,11 @@ async fn search_facet_distribution() {
index index
.search( .search(
json!({ json!({
"facetsDistribution": ["doggos.name"] "facets": ["doggos.name"]
}), }),
|response, code| { |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
let dist = response["facetsDistribution"].as_object().unwrap(); let dist = response["facetDistribution"].as_object().unwrap();
assert_eq!(dist.len(), 1); assert_eq!(dist.len(), 1);
assert_eq!( assert_eq!(
dist["doggos.name"], dist["doggos.name"],
@ -491,11 +491,11 @@ async fn search_facet_distribution() {
index index
.search( .search(
json!({ json!({
"facetsDistribution": ["doggos"] "facets": ["doggos"]
}), }),
|response, code| { |response, code| {
assert_eq!(code, 200, "{}", response); assert_eq!(code, 200, "{}", response);
let dist = response["facetsDistribution"].as_object().unwrap(); let dist = response["facetDistribution"].as_object().unwrap();
dbg!(&dist); dbg!(&dist);
assert_eq!(dist.len(), 3); assert_eq!(dist.len(), 3);
assert_eq!( assert_eq!(

View File

@ -18,7 +18,7 @@ use super::error::{IndexError, Result};
use super::index::Index; use super::index::Index;
pub type Document = serde_json::Map<String, Value>; pub type Document = serde_json::Map<String, Value>;
type MatchesInfo = BTreeMap<String, Vec<MatchBounds>>; type MatchesPosition = BTreeMap<String, Vec<MatchBounds>>;
pub const DEFAULT_SEARCH_LIMIT: usize = 20; pub const DEFAULT_SEARCH_LIMIT: usize = 20;
const fn default_search_limit() -> usize { const fn default_search_limit() -> usize {
@ -63,10 +63,10 @@ pub struct SearchQuery {
pub attributes_to_highlight: Option<HashSet<String>>, pub attributes_to_highlight: Option<HashSet<String>>,
// Default to false // Default to false
#[serde(default = "Default::default")] #[serde(default = "Default::default")]
pub matches: bool, pub show_matches_position: bool,
pub filter: Option<Value>, pub filter: Option<Value>,
pub sort: Option<Vec<String>>, pub sort: Option<Vec<String>>,
pub facets_distribution: Option<Vec<String>>, pub facets: Option<Vec<String>>,
#[serde(default = "default_highlight_pre_tag")] #[serde(default = "default_highlight_pre_tag")]
pub highlight_pre_tag: String, pub highlight_pre_tag: String,
#[serde(default = "default_highlight_post_tag")] #[serde(default = "default_highlight_post_tag")]
@ -81,24 +81,21 @@ pub struct SearchHit {
pub document: Document, pub document: Document,
#[serde(rename = "_formatted", skip_serializing_if = "Document::is_empty")] #[serde(rename = "_formatted", skip_serializing_if = "Document::is_empty")]
pub formatted: Document, pub formatted: Document,
#[serde(rename = "_matchesInfo", skip_serializing_if = "Option::is_none")] #[serde(rename = "_matchesPosition", skip_serializing_if = "Option::is_none")]
pub matches_info: Option<MatchesInfo>, pub matches_position: Option<MatchesPosition>,
} }
#[derive(Serialize, Debug, Clone, PartialEq)] #[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SearchResult { pub struct SearchResult {
pub hits: Vec<SearchHit>, pub hits: Vec<SearchHit>,
pub nb_hits: u64, pub estimated_total_hits: u64,
pub exhaustive_nb_hits: bool,
pub query: String, pub query: String,
pub limit: usize, pub limit: usize,
pub offset: usize, pub offset: usize,
pub processing_time_ms: u128, pub processing_time_ms: u128,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub facets_distribution: Option<BTreeMap<String, BTreeMap<String, u64>>>, pub facet_distribution: Option<BTreeMap<String, BTreeMap<String, u64>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exhaustive_facets_count: Option<bool>,
} }
impl Index { impl Index {
@ -222,13 +219,13 @@ impl Index {
let mut document = let mut document =
permissive_json_pointer::select_values(&displayed_document, attributes_to_retrieve); permissive_json_pointer::select_values(&displayed_document, attributes_to_retrieve);
let (matches_info, formatted) = format_fields( let (matches_position, formatted) = format_fields(
&displayed_document, &displayed_document,
&fields_ids_map, &fields_ids_map,
&formatter_builder, &formatter_builder,
&analyzer, &analyzer,
&formatted_options, &formatted_options,
query.matches, query.show_matches_position,
&displayed_ids, &displayed_ids,
)?; )?;
@ -239,38 +236,34 @@ impl Index {
let hit = SearchHit { let hit = SearchHit {
document, document,
formatted, formatted,
matches_info, matches_position,
}; };
documents.push(hit); documents.push(hit);
} }
let nb_hits = candidates.len(); let estimated_total_hits = candidates.len();
let facets_distribution = match query.facets_distribution { let facet_distribution = match query.facets {
Some(ref fields) => { Some(ref fields) => {
let mut facets_distribution = self.facets_distribution(&rtxn); let mut facet_distribution = self.facets_distribution(&rtxn);
if fields.iter().all(|f| f != "*") { if fields.iter().all(|f| f != "*") {
facets_distribution.facets(fields); facet_distribution.facets(fields);
} }
let distribution = facets_distribution.candidates(candidates).execute()?; let distribution = facet_distribution.candidates(candidates).execute()?;
Some(distribution) Some(distribution)
} }
None => None, None => None,
}; };
let exhaustive_facets_count = facets_distribution.as_ref().map(|_| false); // not implemented yet
let result = SearchResult { let result = SearchResult {
exhaustive_nb_hits: false, // not implemented yet
hits: documents, hits: documents,
nb_hits, estimated_total_hits,
query: query.q.clone().unwrap_or_default(), query: query.q.clone().unwrap_or_default(),
limit: query.limit, limit: query.limit,
offset: query.offset.unwrap_or_default(), offset: query.offset.unwrap_or_default(),
processing_time_ms: before_search.elapsed().as_millis(), processing_time_ms: before_search.elapsed().as_millis(),
facets_distribution, facet_distribution,
exhaustive_facets_count,
}; };
Ok(result) Ok(result)
} }
@ -445,8 +438,8 @@ fn format_fields<'a, A: AsRef<[u8]>>(
formatted_options: &BTreeMap<FieldId, FormatOptions>, formatted_options: &BTreeMap<FieldId, FormatOptions>,
compute_matches: bool, compute_matches: bool,
displayable_ids: &BTreeSet<FieldId>, displayable_ids: &BTreeSet<FieldId>,
) -> Result<(Option<MatchesInfo>, Document)> { ) -> Result<(Option<MatchesPosition>, Document)> {
let mut matches = compute_matches.then(BTreeMap::new); let mut matches_position = compute_matches.then(BTreeMap::new);
let mut document = document.clone(); let mut document = document.clone();
// select the attributes to retrieve // select the attributes to retrieve
@ -477,7 +470,7 @@ fn format_fields<'a, A: AsRef<[u8]>>(
compute_matches, compute_matches,
); );
if let Some(matches) = matches.as_mut() { if let Some(matches) = matches_position.as_mut() {
if !infos.is_empty() { if !infos.is_empty() {
matches.insert(key.to_owned(), infos); matches.insert(key.to_owned(), infos);
} }
@ -491,7 +484,7 @@ fn format_fields<'a, A: AsRef<[u8]>>(
.map(|&fid| field_ids_map.name(fid).unwrap()); .map(|&fid| field_ids_map.name(fid).unwrap());
let document = permissive_json_pointer::select_values(&document, selectors); let document = permissive_json_pointer::select_values(&document, selectors);
Ok((matches, document)) Ok((matches_position, document))
} }
fn format_value<'a, A: AsRef<[u8]>>( fn format_value<'a, A: AsRef<[u8]>>(

View File

@ -687,10 +687,10 @@ mod test {
attributes_to_crop: None, attributes_to_crop: None,
crop_length: 18, crop_length: 18,
attributes_to_highlight: None, attributes_to_highlight: None,
matches: true, show_matches_position: true,
filter: None, filter: None,
sort: None, sort: None,
facets_distribution: None, facets: None,
highlight_pre_tag: default_highlight_pre_tag(), highlight_pre_tag: default_highlight_pre_tag(),
highlight_post_tag: default_highlight_post_tag(), highlight_post_tag: default_highlight_post_tag(),
crop_marker: default_crop_marker(), crop_marker: default_crop_marker(),
@ -698,14 +698,12 @@ mod test {
let result = SearchResult { let result = SearchResult {
hits: vec![], hits: vec![],
nb_hits: 29, estimated_total_hits: 29,
exhaustive_nb_hits: true,
query: "hello world".to_string(), query: "hello world".to_string(),
limit: 24, limit: 24,
offset: 0, offset: 0,
processing_time_ms: 50, processing_time_ms: 50,
facets_distribution: None, facet_distribution: None,
exhaustive_facets_count: Some(true),
}; };
let mut uuid_store = MockIndexMetaStore::new(); let mut uuid_store = MockIndexMetaStore::new();