Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(search): sort by updated at (#716)

Will Hutchinsonfd28d12bdec6
diff --git a/rust/cloud-storage/models_search/src/unified.rs b/rust/cloud-storage/models_search/src/unified.rs
index 71a28ac961..a25c093d2d 100644
--- a/rust/cloud-storage/models_search/src/unified.rs
+++ b/rust/cloud-storage/models_search/src/unified.rs
@@ -1,11 +1,11 @@
use std::collections::HashSet;
+use uuid::Uuid;
use crate::channel::ChannelSearchResponseItemWithMetadata;
use crate::chat::ChatSearchResponseItemWithMetadata;
use crate::document::DocumentSearchResponseItemWithMetadata;
use crate::email::EmailSearchResponseItemWithMetadata;
use crate::project::ProjectSearchResponseItemWithMetadata;
-use crate::score::calculate_average;
use crate::{
MatchType, SearchOn, channel::SimpleChannelSearchReponseBaseItem,
chat::SimpleChatSearchResponseBaseItem, document::SimpleDocumentSearchResponseBaseItem,
@@ -132,14 +132,39 @@ pub enum UnifiedSearchResponseItem {
}
impl UnifiedSearchResponseItem {
- /// Calculate the average score from search_results
- pub fn average_score(&self) -> f64 {
+ pub fn entity_id(&self) -> Uuid {
match self {
- Self::Document(item) => calculate_average(&item.extra.document_search_results),
- Self::Chat(item) => calculate_average(&item.extra.chat_search_results),
- Self::Email(item) => calculate_average(&item.extra.email_message_search_results),
- Self::Channel(item) => calculate_average(&item.extra.channel_message_search_results),
- Self::Project(item) => calculate_average(&item.extra.project_search_results),
+ Self::Document(item) => item.extra.id,
+ Self::Chat(item) => item.extra.id,
+ Self::Email(item) => item.extra.id,
+ Self::Channel(item) => item.extra.id,
+ Self::Project(item) => item.extra.id,
+ }
+ }
+ /// Get the updated_at timestamp for each item
+ pub fn updated_at(&self) -> i64 {
+ match self {
+ Self::Document(item) => item
+ .metadata
+ .as_ref()
+ .map(|m| m.updated_at)
+ .unwrap_or_default(),
+ Self::Chat(item) => item
+ .metadata
+ .as_ref()
+ .map(|m| m.updated_at)
+ .unwrap_or_default(),
+ Self::Email(item) => item.updated_at,
+ Self::Channel(item) => item
+ .metadata
+ .as_ref()
+ .map(|m| m.updated_at)
+ .unwrap_or_default(),
+ Self::Project(item) => item
+ .metadata
+ .as_ref()
+ .map(|m| m.updated_at)
+ .unwrap_or_default(),
}
}
}
diff --git a/rust/cloud-storage/search_service/src/api/search/unified.rs b/rust/cloud-storage/search_service/src/api/search/unified.rs
index 7332b8b6f8..9dd5f650d6 100644
--- a/rust/cloud-storage/search_service/src/api/search/unified.rs
+++ b/rust/cloud-storage/search_service/src/api/search/unified.rs
@@ -13,10 +13,13 @@ use axum::{
response::{IntoResponse, Json, Response},
};
use model::{response::ErrorResponse, user::UserContext};
-use models_search::unified::{UnifiedSearchRequest, UnifiedSearchResponse};
+use models_search::unified::{
+ UnifiedSearchRequest, UnifiedSearchResponse, UnifiedSearchResponseItem,
+};
use opensearch_client::search::unified::{
SplitUnifiedSearchResponse, SplitUnifiedSearchResponseValues,
};
+use std::cmp::Ordering;
/// Perform a search through all items
#[utoipa::path(
@@ -102,8 +105,25 @@ pub async fn handler(
results.extend(enriched_project_results);
results.extend(enriched_email_results);
- // Sort the results by their average score
- results.sort_by(|a, b| b.average_score().total_cmp(&a.average_score()));
+ results = sort_unified_search_results(results);
Ok((StatusCode::OK, Json(UnifiedSearchResponse { results })).into_response())
}
+
+/// Sorts the unified results
+/// This method is so we can more easily test sorting
+fn sort_unified_search_results(
+ mut results: Vec<UnifiedSearchResponseItem>,
+) -> Vec<UnifiedSearchResponseItem> {
+ // Sort the results by their updated_at
+ results.sort_by(|a, b| {
+ b.updated_at()
+ .partial_cmp(&a.updated_at())
+ .unwrap_or(Ordering::Equal)
+ });
+
+ results
+}
+
+#[cfg(test)]
+mod test;
diff --git a/rust/cloud-storage/search_service/src/api/search/unified/test.rs b/rust/cloud-storage/search_service/src/api/search/unified/test.rs
new file mode 100644
index 0000000000..f2bc4e5b56
--- /dev/null
+++ b/rust/cloud-storage/search_service/src/api/search/unified/test.rs
@@ -0,0 +1,133 @@
+use super::*;
+use models_search::{
+ chat::{ChatMetadata, ChatSearchResponseItem, ChatSearchResponseItemWithMetadata},
+ document::{
+ DocumentMetadata, DocumentSearchResponseItem, DocumentSearchResponseItemWithMetadata,
+ },
+ email::{EmailSearchResponseItem, EmailSearchResponseItemWithMetadata},
+ project::{ProjectMetadata, ProjectSearchResponseItem, ProjectSearchResponseItemWithMetadata},
+};
+use sqlx::types::Uuid;
+
+#[test]
+fn test_sort_unified_search_results() {
+ // Create test UUIDs
+ let doc_id = Uuid::new_v4();
+ let chat_id = Uuid::new_v4();
+ let email_id = Uuid::new_v4();
+ let project_id = Uuid::new_v4();
+ let doc2_id = Uuid::new_v4();
+
+ // Create items with different updated_at timestamps
+ // These are intentionally out of order to test sorting
+ let results: Vec<UnifiedSearchResponseItem> = vec![
+ // Document with updated_at = 1000 (oldest)
+ UnifiedSearchResponseItem::Document(DocumentSearchResponseItemWithMetadata {
+ metadata: Some(DocumentMetadata {
+ created_at: 900,
+ updated_at: 1000,
+ viewed_at: None,
+ project_id: None,
+ deleted_at: None,
+ }),
+ extra: DocumentSearchResponseItem {
+ id: doc_id,
+ name: "Old Document".to_string(),
+ owner_id: "owner1".to_string(),
+ document_id: doc_id,
+ document_name: "Old Document".to_string(),
+ file_type: Some("pdf".to_string()),
+ sub_type: None,
+ document_search_results: vec![],
+ },
+ }),
+ // Chat with updated_at = 3000 (newest)
+ UnifiedSearchResponseItem::Chat(ChatSearchResponseItemWithMetadata {
+ metadata: Some(ChatMetadata {
+ created_at: 2900,
+ updated_at: 3000,
+ viewed_at: None,
+ project_id: None,
+ deleted_at: None,
+ }),
+ extra: ChatSearchResponseItem {
+ id: chat_id,
+ name: "Newest Chat".to_string(),
+ owner_id: "owner1".to_string(),
+ chat_id,
+ user_id: "user1".to_string(),
+ chat_search_results: vec![],
+ },
+ }),
+ // Email with updated_at = 1500 (middle)
+ UnifiedSearchResponseItem::Email(EmailSearchResponseItemWithMetadata {
+ created_at: 1400,
+ updated_at: 1500,
+ viewed_at: None,
+ snippet: None,
+ extra: EmailSearchResponseItem {
+ id: email_id,
+ name: Some("Middle Email".to_string()),
+ owner_id: "owner1".to_string(),
+ subject: Some("Email Subject".to_string()),
+ thread_id: email_id,
+ user_id: "user1".to_string(),
+ email_message_search_results: vec![],
+ },
+ }),
+ // Project with updated_at = 2000 (second newest)
+ UnifiedSearchResponseItem::Project(ProjectSearchResponseItemWithMetadata {
+ metadata: Some(ProjectMetadata {
+ created_at: 1900,
+ updated_at: 2000,
+ viewed_at: None,
+ parent_project_id: None,
+ deleted_at: None,
+ }),
+ extra: ProjectSearchResponseItem {
+ id: project_id,
+ name: "Recent Project".to_string(),
+ owner_id: "owner1".to_string(),
+ updated_at: 2000,
+ created_at: 1900,
+ project_search_results: vec![],
+ },
+ }),
+ // Another Document with updated_at = 2500 (second)
+ UnifiedSearchResponseItem::Document(DocumentSearchResponseItemWithMetadata {
+ metadata: Some(DocumentMetadata {
+ created_at: 2400,
+ updated_at: 2500,
+ viewed_at: None,
+ project_id: None,
+ deleted_at: None,
+ }),
+ extra: DocumentSearchResponseItem {
+ id: doc2_id,
+ name: "Recent Document".to_string(),
+ owner_id: "owner1".to_string(),
+ document_id: doc2_id,
+ document_name: "Recent Document".to_string(),
+ file_type: Some("docx".to_string()),
+ sub_type: None,
+ document_search_results: vec![],
+ },
+ }),
+ ];
+
+ // Expected order after sorting by updated_at descending (newest first)
+ let expected_ids: Vec<Uuid> = vec![
+ chat_id, // 3000 - newest
+ doc2_id, // 2500
+ project_id, // 2000
+ email_id, // 1500
+ doc_id, // 1000 - oldest
+ ];
+
+ let results = sort_unified_search_results(results);
+
+ assert_eq!(
+ results.iter().map(|r| r.entity_id()).collect::<Vec<Uuid>>(),
+ expected_ids
+ );
+}