fix:result caching issue (#4065)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Improved sorting logic for search responses, enhancing clarity and
maintainability.
  
- **Bug Fixes**
- Simplified conditional logic for cache checks, improving efficiency
and reducing complexity.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Ashish Kolhe
2024-07-25 19:47:24 +05:30
committed by GitHub
parent 32442a1756
commit 4e78db3538
2 changed files with 21 additions and 16 deletions
+5 -6
View File
@@ -71,12 +71,11 @@ pub async fn check_cache(
// skip the count queries & queries first order by is not _timestamp field
let order_by = meta.meta.order_by;
if (sql_mode.eq(&SqlMode::Full) && req.query.track_total_hits)
|| (order_by.is_empty()
|| (order_by.first().as_ref().unwrap().0 != cfg.common.column_timestamp
&& (result_ts_col.is_none()
|| (result_ts_col.is_some()
&& result_ts_col.as_ref().unwrap()
!= &order_by.first().as_ref().unwrap().0))))
|| (!order_by.is_empty()
&& order_by.first().as_ref().unwrap().0 != cfg.common.column_timestamp
&& (result_ts_col.is_none()
|| (result_ts_col.is_some()
&& result_ts_col.as_ref().unwrap() != &order_by.first().as_ref().unwrap().0)))
{
return CachedQueryResponse::default();
}
+16 -10
View File
@@ -231,7 +231,9 @@ pub async fn search(
);
c_resp.cached_response
} else {
results[0].clone()
let mut reps = results[0].clone();
sort_response(c_resp.is_descending, &mut reps, &c_resp.ts_column);
reps
}
};
@@ -343,15 +345,7 @@ fn merge_response(
}
cache_response.hits.extend(res.hits.clone());
}
if is_descending {
cache_response
.hits
.sort_by_key(|b| std::cmp::Reverse(get_ts_value(ts_column, b)));
} else {
cache_response
.hits
.sort_by_key(|a| get_ts_value(ts_column, a));
}
sort_response(is_descending, cache_response, ts_column);
if cache_response.hits.len() > limit as usize {
cache_response.hits.truncate(limit as usize);
@@ -366,6 +360,18 @@ fn merge_response(
(cache_hits_len as f64 * 100_f64 / (result_cache_len + cache_hits_len) as f64) as usize;
}
fn sort_response(is_descending: bool, cache_response: &mut search::Response, ts_column: &str) {
if is_descending {
cache_response
.hits
.sort_by_key(|b| std::cmp::Reverse(get_ts_value(ts_column, b)));
} else {
cache_response
.hits
.sort_by_key(|a| get_ts_value(ts_column, a));
}
}
#[allow(clippy::too_many_arguments)]
async fn write_results(
trace_id: &str,