Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(search) sort by updated at seconds (#710)
Will Hutchinsonb58f15fa51b1
diff --git a/rust/cloud-storage/opensearch_client/src/search/builder.rs b/rust/cloud-storage/opensearch_client/src/search/builder.rsindex 38df6b589f..db4546a643 100644--- a/rust/cloud-storage/opensearch_client/src/search/builder.rs+++ b/rust/cloud-storage/opensearch_client/src/search/builder.rs@@ -58,6 +58,25 @@ pub(crate) fn create_highlight_field<'a>(.number_of_fragments(number_of_fragments)}+/// Creates sort vec to sort by the updated_at with a fallback to score sort+pub(crate) fn updated_at_sort<'a>() -> Vec<SortType<'a>> {+ vec![+ SortType::ScriptSort(ScriptSort::new(+ Script::new(+ r#"if (doc.containsKey('sent_at_seconds') && doc['sent_at_seconds'].size() > 0) {+ return doc['sent_at_seconds'].value.toInstant().toEpochMilli();+ } else if (doc.containsKey('updated_at_seconds') && doc['updated_at_seconds'].size() > 0) {+ return doc['updated_at_seconds'].value.toInstant().toEpochMilli();+ } else {+ return 0L; // Or Long.MAX_VALUE to push to end+ }"#,+ ),+ ScriptSortType::Number,+ SortOrder::Desc,+ )),+ SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),+ ]+}pub trait SearchQueryConfig {/// Key for item idconst ID_KEY: &'static str = "entity_id";@@ -73,10 +92,9 @@ pub trait SearchQueryConfig {/// Returns the default sort types that are used on the search query./// Override this method if you need custom sort logicfn default_sort_types<'a>() -> Vec<SortType<'a>> {- vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new(Self::ID_KEY, SortOrder::Asc)),- ]+ println!("CALLED");+ // Use the updated_at_sort by default+ updated_at_sort()}/// Override this method if you need custom highlight logicdiff --git a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs b/rust/cloud-storage/opensearch_client/src/search/builder/test.rsindex fe6f8902e4..f46b4ad6a4 100644--- a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/builder/test.rs@@ -138,14 +138,7 @@ fn test_build_search_request() -> anyhow::Result<()> {"collapse": {"field": "entity_id"},- "sort": [- {- "_score": "desc"- },- {- "entity_id": "asc"- }- ],+ "sort": TestSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),"highlight": {"require_field_match": false,"fields": {@@ -213,14 +206,7 @@ fn test_build_search_request() -> anyhow::Result<()> {"collapse": {"field": "entity_id"},- "sort": [- {- "_score": "desc"- },- {- "entity_id": "asc"- }- ],+ "sort": TestSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),"highlight": {"require_field_match": false,"fields": {diff --git a/rust/cloud-storage/opensearch_client/src/search/channels.rs b/rust/cloud-storage/opensearch_client/src/search/channels.rsindex 15275e240e..336ecb03dc 100644--- a/rust/cloud-storage/opensearch_client/src/search/channels.rs+++ b/rust/cloud-storage/opensearch_client/src/search/channels.rs@@ -13,10 +13,7 @@ use crate::{use crate::SearchOn;use models_opensearch::{SearchEntityType, SearchIndex};-use opensearch_query_builder::{- BoolQueryBuilder, FieldSort, QueryType, ScoreWithOrderSort, SearchRequest, SortOrder, SortType,- ToOpenSearchJson,-};+use opensearch_query_builder::{BoolQueryBuilder, QueryType, SearchRequest, ToOpenSearchJson};use serde_json::Value;#[derive(Debug, serde::Serialize, serde::Deserialize)]@@ -48,14 +45,6 @@ impl SearchQueryConfig for ChannelMessageSearchConfig {const TITLE_KEY: &'static str = "name";const ENTITY_INDEX: SearchEntityType = SearchEntityType::Channels;- fn default_sort_types<'a>() -> Vec<SortType<'a>> {- vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new(Self::ID_KEY, SortOrder::Asc)),- SortType::Field(FieldSort::new("message_id", SortOrder::Asc)),- ]- }-fn append_owner_highlights<'a>(highlight: opensearch_query_builder::Highlight<'a>,) -> opensearch_query_builder::Highlight<'a> {diff --git a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs b/rust/cloud-storage/opensearch_client/src/search/channels/test.rsindex 0f5bf30ece..7e68e4044c 100644--- a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/channels/test.rs@@ -18,77 +18,67 @@ fn test_build_search_request() -> anyhow::Result<()> {let result = builder.build_search_request()?;let expected = serde_json::json!({- "collapse": {- "field": "entity_id"- },- "from": 20,- "highlight": ChannelMessageSearchConfig::append_owner_highlights(ChannelMessageSearchConfig::default_highlight()).to_json(),- "query": {- "bool": {- "must": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {- "wildcard": {- "sender_id": {- "case_insensitive": true,- "value": "macro|test*",- "boost": 5000.0- }- }- },- {- "match_phrase": {- "content": "test"- }- }- ]- }- },- ],- "filter": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {"terms": {"entity_id": ["id1", "id2"]}},- {"term": {"sender_id": "user123"}}- ]- }- },- {"term": {"_index": "channels"}},- {- "terms": {- "thread_id": ["thread1", "thread2"]- }- },- {- "terms": {- "mentions": ["mention1", "mention2"]- }- },- {- "terms": {- "sender_id": ["sender1", "sender2"]- }- }- ]- }- },- "size": 20,- "sort": [- {- "_score": "desc"- },- {- "entity_id": "asc"- },- {- "message_id": "asc"- }- ]+ "collapse": {+ "field": "entity_id"+ },+ "from": 20,+ "highlight": ChannelMessageSearchConfig::append_owner_highlights(ChannelMessageSearchConfig::default_highlight()).to_json(),+ "query": {+ "bool": {+ "must": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "wildcard": {+ "sender_id": {+ "case_insensitive": true,+ "value": "macro|test*",+ "boost": 5000.0+ }+ }+ },+ {+ "match_phrase": {+ "content": "test"+ }+ }+ ]+ }+ },+ ],+ "filter": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {"terms": {"entity_id": ["id1", "id2"]}},+ {"term": {"sender_id": "user123"}}+ ]+ }+ },+ {"term": {"_index": "channels"}},+ {+ "terms": {+ "thread_id": ["thread1", "thread2"]+ }+ },+ {+ "terms": {+ "mentions": ["mention1", "mention2"]+ }+ },+ {+ "terms": {+ "sender_id": ["sender1", "sender2"]+ }+ }+ ]+ }+ },+ "size": 20,+ "sort": ChannelMessageSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),});assert_eq!(result.to_json(), expected);diff --git a/rust/cloud-storage/opensearch_client/src/search/chats.rs b/rust/cloud-storage/opensearch_client/src/search/chats.rsindex c8e8576e16..546cc26b1a 100644--- a/rust/cloud-storage/opensearch_client/src/search/chats.rs+++ b/rust/cloud-storage/opensearch_client/src/search/chats.rs@@ -14,10 +14,7 @@ use crate::{use crate::SearchOn;use models_opensearch::{SearchEntityType, SearchIndex};-use opensearch_query_builder::{- BoolQueryBuilder, FieldSort, ScoreWithOrderSort, SearchRequest, SortOrder, SortType,- ToOpenSearchJson,-};+use opensearch_query_builder::{BoolQueryBuilder, SearchRequest, ToOpenSearchJson};use serde_json::Value;#[derive(Debug, serde::Serialize, serde::Deserialize)]@@ -43,14 +40,6 @@ impl SearchQueryConfig for ChatSearchConfig {const USER_ID_KEY: &'static str = "user_id";const TITLE_KEY: &'static str = "name";const ENTITY_INDEX: SearchEntityType = SearchEntityType::Chats;-- fn default_sort_types<'a>() -> Vec<SortType<'a>> {- vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new(Self::ID_KEY, SortOrder::Asc)),- SortType::Field(FieldSort::new("chat_message_id", SortOrder::Asc)),- ]- }}pub(crate) struct ChatQueryBuilder {diff --git a/rust/cloud-storage/opensearch_client/src/search/documents.rs b/rust/cloud-storage/opensearch_client/src/search/documents.rsindex b6817eb62c..b4cbe019fa 100644--- a/rust/cloud-storage/opensearch_client/src/search/documents.rs+++ b/rust/cloud-storage/opensearch_client/src/search/documents.rs@@ -13,10 +13,7 @@ use crate::{use crate::SearchOn;use models_opensearch::{SearchEntityType, SearchIndex};-use opensearch_query_builder::{- BoolQueryBuilder, FieldSort, ScoreWithOrderSort, SearchRequest, SortOrder, SortType,- ToOpenSearchJson,-};+use opensearch_query_builder::{BoolQueryBuilder, SearchRequest, ToOpenSearchJson};use serde_json::Value;#[derive(Clone)]@@ -27,14 +24,6 @@ impl SearchQueryConfig for DocumentSearchConfig {const TITLE_KEY: &'static str = "name";const ENTITY_INDEX: SearchEntityType = SearchEntityType::Documents;- fn default_sort_types<'a>() -> Vec<SortType<'a>> {- vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new(Self::ID_KEY, SortOrder::Asc)),- SortType::Field(FieldSort::new("node_id", SortOrder::Asc)),- ]- }-fn append_owner_highlights<'a>(highlight: opensearch_query_builder::Highlight<'a>,) -> opensearch_query_builder::Highlight<'a> {diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rsindex df87b4bae1..3dc5601f9c 100644--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs@@ -11,10 +11,7 @@ use crate::{use crate::SearchOn;use models_opensearch::{SearchEntityType, SearchIndex};-use opensearch_query_builder::{- BoolQueryBuilder, FieldSort, QueryType, ScoreWithOrderSort, SearchRequest, SortOrder, SortType,- ToOpenSearchJson,-};+use opensearch_query_builder::{BoolQueryBuilder, QueryType, SearchRequest, ToOpenSearchJson};use crate::search::model::DefaultSearchResponse;use serde_json::Value;@@ -26,13 +23,6 @@ impl SearchQueryConfig for EmailSearchConfig {const TITLE_KEY: &'static str = "name";const ENTITY_INDEX: SearchEntityType = SearchEntityType::Emails;- fn default_sort_types<'a>() -> Vec<SortType<'a>> {- vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new(Self::ID_KEY, SortOrder::Asc)),- ]- }-fn append_owner_highlights<'a>(highlight: opensearch_query_builder::Highlight<'a>,) -> opensearch_query_builder::Highlight<'a> {diff --git a/rust/cloud-storage/opensearch_client/src/search/unified.rs b/rust/cloud-storage/opensearch_client/src/search/unified.rsindex 5cb6b9cb01..c1380bd16c 100644--- a/rust/cloud-storage/opensearch_client/src/search/unified.rs+++ b/rust/cloud-storage/opensearch_client/src/search/unified.rs@@ -4,7 +4,7 @@ use crate::{Result,error::{OpensearchClientError, ResponseExt},search::{- builder::{SearchQueryConfig, create_highlight_field},+ builder::{SearchQueryConfig, create_highlight_field, updated_at_sort},channels::{ChannelMessageIndex, ChannelMessageQueryBuilder, ChannelMessageSearchArgs,ChannelMessageSearchConfig,@@ -460,10 +460,7 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques}// Build sort- let sort = vec![- SortType::ScoreWithOrder(ScoreWithOrderSort::new(SortOrder::Desc)),- SortType::Field(FieldSort::new("entity_id", SortOrder::Desc)),- ];+ let sort = updated_at_sort();for sort in sort {search_request_builder.add_sort(sort);diff --git a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs b/rust/cloud-storage/opensearch_client/src/search/unified/test.rsindex bd85a16c6f..fef3802d4c 100644--- a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/unified/test.rs@@ -858,14 +858,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {}},"size": 20,- "sort": [- {- "_score": "desc"- },- {- "entity_id": "desc"- }- ]+ "sort": updated_at_sort().iter().map(|s| s.to_json()).collect::<Vec<_>>(),});assert_eq!(result.to_json(), expected);@@ -924,202 +917,193 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {let result = build_unified_search_request(&unified_search_args)?;- let expected = serde_json::json!(- {- "collapse": {- "field": "entity_id"- },- "from": 20,- "highlight": {- "fields": {- "name": {- "number_of_fragments": 1,- "post_tags": ["</macro_em>"],- "pre_tags": ["<macro_em>"],- "type": "plain"- },- "project_name": {- "number_of_fragments": 1,- "post_tags": ["</macro_em>"],- "pre_tags": ["<macro_em>"],- "type": "plain"- }- },- "require_field_match": true- },- "query": {- "bool": {- "minimum_should_match": 1,- "should": [- {- "bool": {- "filter": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {- "terms": {- "entity_id": ["id1", "id2"]- }- },- {- "term": {- "user_id": "user"- }- }- ]- }- },- {- "term": {- "_index": "names"- }- },- {- "term": {- "entity_type": "documents"- }- }- ],- "must": [- {- "match_phrase": {- "name": "test"- }- }- ]- }- },- {- "bool": {- "filter": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {- "terms": {- "entity_id": ["id1", "id2"]- }- },- {- "term": {- "user_id": "user"- }- }- ]- }- },- {- "term": {- "_index": "names"- }- },- {- "term": {- "entity_type": "emails"- }- }- ],- "must": [- {- "match_phrase": {- "name": "test"- }- }- ]- }- },- {- "bool": {- "filter": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {- "terms": {- "entity_id": ["id1", "id2"]- }- },- {- "term": {- "user_id": "user"- }- }- ]- }- },- {- "term": {- "_index": "names"- }- },- {- "term": {- "entity_type": "chats"- }- }- ],- "must": [- {- "match_phrase": {- "name": "test"- }- }- ]- }- },- {- "bool": {- "filter": [- {- "bool": {- "minimum_should_match": 1,- "should": [- {- "terms": {- "entity_id": ["id1", "id2"]- }- },- {- "term": {- "user_id": "user"- }- }- ]- }- },- {- "term": {- "_index": "projects"- }- }- ],- "must": [- {- "match_phrase": {- "project_name": "test"- }- }- ]- }- }- ]- }- },- "size": 20,- "sort": [- {- "_score": "desc"- },- {- "entity_id": "desc"- }- ]- }- );+ let expected = serde_json::json!({+ "collapse": {+ "field": "entity_id"+ },+ "from": 20,+ "highlight": {+ "fields": {+ "name": {+ "number_of_fragments": 1,+ "post_tags": ["</macro_em>"],+ "pre_tags": ["<macro_em>"],+ "type": "plain"+ },+ "project_name": {+ "number_of_fragments": 1,+ "post_tags": ["</macro_em>"],+ "pre_tags": ["<macro_em>"],+ "type": "plain"+ }+ },+ "require_field_match": true+ },+ "query": {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "bool": {+ "filter": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "terms": {+ "entity_id": ["id1", "id2"]+ }+ },+ {+ "term": {+ "user_id": "user"+ }+ }+ ]+ }+ },+ {+ "term": {+ "_index": "names"+ }+ },+ {+ "term": {+ "entity_type": "documents"+ }+ }+ ],+ "must": [+ {+ "match_phrase": {+ "name": "test"+ }+ }+ ]+ }+ },+ {+ "bool": {+ "filter": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "terms": {+ "entity_id": ["id1", "id2"]+ }+ },+ {+ "term": {+ "user_id": "user"+ }+ }+ ]+ }+ },+ {+ "term": {+ "_index": "names"+ }+ },+ {+ "term": {+ "entity_type": "emails"+ }+ }+ ],+ "must": [+ {+ "match_phrase": {+ "name": "test"+ }+ }+ ]+ }+ },+ {+ "bool": {+ "filter": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "terms": {+ "entity_id": ["id1", "id2"]+ }+ },+ {+ "term": {+ "user_id": "user"+ }+ }+ ]+ }+ },+ {+ "term": {+ "_index": "names"+ }+ },+ {+ "term": {+ "entity_type": "chats"+ }+ }+ ],+ "must": [+ {+ "match_phrase": {+ "name": "test"+ }+ }+ ]+ }+ },+ {+ "bool": {+ "filter": [+ {+ "bool": {+ "minimum_should_match": 1,+ "should": [+ {+ "terms": {+ "entity_id": ["id1", "id2"]+ }+ },+ {+ "term": {+ "user_id": "user"+ }+ }+ ]+ }+ },+ {+ "term": {+ "_index": "projects"+ }+ }+ ],+ "must": [+ {+ "match_phrase": {+ "project_name": "test"+ }+ }+ ]+ }+ }+ ]+ }+ },+ "size": 20,+ "sort": updated_at_sort().iter().map(|s| s.to_json()).collect::<Vec<_>>(),+ });assert_eq!(result.to_json(), expected);@@ -1867,14 +1851,7 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {}},"size": 20,- "sort": [- {- "_score": "desc"- },- {- "entity_id": "desc"- }- ]+ "sort": updated_at_sort().iter().map(|s| s.to_json()).collect::<Vec<_>>(),});assert_eq!(result.to_json(), expected);@@ -2002,16 +1979,8 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {}},"size": 20,- "sort": [- {- "_score": "desc"- },- {- "entity_id": "desc"- }- ]- }- );+ "sort": updated_at_sort().iter().map(|s| s.to_json()).collect::<Vec<_>>(),+ });assert_eq!(result.to_json(), expected);