feat(search): update nbHits count with filtered docs for placeholder search

This commit is contained in:
Balaji Sivaraman 2020-07-20 20:33:46 +05:30
parent 43df4a56c4
commit 75e22fc7f5
2 changed files with 59 additions and 3 deletions

View File

@ -225,10 +225,17 @@ impl<'c, 'f, 'd, 'i> QueryBuilder<'c, 'f, 'd, 'i> {
fn sort_result_from_docids(&self, docids: &[DocumentId], range: Range<usize>) -> SortResult { fn sort_result_from_docids(&self, docids: &[DocumentId], range: Range<usize>) -> SortResult {
let mut sort_result = SortResult::default(); let mut sort_result = SortResult::default();
let mut filtered_count = 0;
let mut result = match self.filter { let mut result = match self.filter {
Some(ref filter) => docids Some(ref filter) => docids
.iter() .iter()
.filter(|item| (filter)(**item)) .filter(|item| {
let accepted = (filter)(**item);
if !accepted {
filtered_count += 1;
}
accepted
})
.skip(range.start) .skip(range.start)
.take(range.end - range.start) .take(range.end - range.start)
.map(|&id| Document::from_highlights(id, &[])) .map(|&id| Document::from_highlights(id, &[]))
@ -248,15 +255,19 @@ impl<'c, 'f, 'd, 'i> QueryBuilder<'c, 'f, 'd, 'i> {
result.retain(|doc| { result.retain(|doc| {
let id = doc.id; let id = doc.id;
let key = (distinct)(id); let key = (distinct)(id);
match key { let distinct_accepted = match key {
Some(key) => distinct_map.register(key), Some(key) => distinct_map.register(key),
None => distinct_map.register_without_key(), None => distinct_map.register_without_key(),
};
if !distinct_accepted {
filtered_count += 1;
} }
distinct_accepted
}); });
} }
sort_result.documents = result; sort_result.documents = result;
sort_result.nb_hits = docids.len(); sort_result.nb_hits = docids.len() - filtered_count;
sort_result sort_result
} }

View File

@ -588,3 +588,48 @@ async fn placeholder_search_with_empty_query() {
assert_eq!(response["hits"].as_array().unwrap().len(), 3); assert_eq!(response["hits"].as_array().unwrap().len(), 3);
}); });
} }
#[actix_rt::test]
async fn test_filter_nb_hits_search_placeholder() {
let mut server = common::Server::with_uid("test");
let body = json!({
"uid": "test",
"primaryKey": "id",
});
server.create_index(body).await;
let documents = json!([
{
"id": 1,
"content": "a",
"color": "green",
"size": 1,
},
{
"id": 2,
"content": "a",
"color": "green",
"size": 2,
},
{
"id": 3,
"content": "a",
"color": "blue",
"size": 3,
},
]);
server.add_or_update_multiple_documents(documents).await;
let (response, _) = server.search_post(json!({})).await;
assert_eq!(response["nbHits"], 3);
server.update_distinct_attribute(json!("color")).await;
let (response, _) = server.search_post(json!({})).await;
assert_eq!(response["nbHits"], 2);
let (response, _) = server.search_post(json!({"filters": "size < 3"})).await;
println!("result: {}", response);
assert_eq!(response["nbHits"], 1);
}