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.

feat(search): macrodb filter out documents by file type

Will Hutchinson20417d6be56b
diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-0392a509a0896bd3acc01964a39a4477aeafb381e532657ccb7b11ad05b9038e.json b/rust/cloud-storage/macro_db_client/.sqlx/query-0392a509a0896bd3acc01964a39a4477aeafb381e532657ccb7b11ad05b9038e.json
new file mode 100644
index 0000000000..c99a6a2f83
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-0392a509a0896bd3acc01964a39a4477aeafb381e532657ccb7b11ad05b9038e.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n d.id\n FROM\n \"Document\" d\n WHERE\n d.id = ANY($1)\n AND d.\"fileType\" = ANY($2)\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "TextArray",
+ "TextArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "0392a509a0896bd3acc01964a39a4477aeafb381e532657ccb7b11ad05b9038e"
+}
diff --git a/rust/cloud-storage/macro_db_client/src/items/filter.rs b/rust/cloud-storage/macro_db_client/src/items/filter.rs
index 973929ab14..34c0628916 100644
--- a/rust/cloud-storage/macro_db_client/src/items/filter.rs
+++ b/rust/cloud-storage/macro_db_client/src/items/filter.rs
@@ -5,6 +5,34 @@ use model::item::ShareableItemType;
use crate::projects::get_project::get_sub_items::bulk_get_all_sub_project_ids;
+/// Given a list of document ids and a list of file types, this will
+/// return a subset list of items that are of the provided file types.
+#[tracing::instrument(skip(db), err)]
+pub async fn filter_documents_by_file_types(
+ db: &sqlx::PgPool,
+ documents: &[String],
+ file_types: &[String],
+) -> anyhow::Result<Vec<String>> {
+ let documents = sqlx::query!(
+ r#"
+ SELECT
+ d.id
+ FROM
+ "Document" d
+ WHERE
+ d.id = ANY($1)
+ AND d."fileType" = ANY($2)
+ "#,
+ documents,
+ file_types,
+ )
+ .map(|row| row.id)
+ .fetch_all(db)
+ .await?;
+
+ Ok(documents)
+}
+
/// Given a list of item ids, the item type and a list of project_ids, this will
/// return a subset list of items that are within the provided project ids and their sub-projects.
#[tracing::instrument(skip(db), err)]
diff --git a/rust/cloud-storage/opensearch_client/examples/query_document.rs b/rust/cloud-storage/opensearch_client/examples/query_document.rs
index 6dd4d896e6..c992883e7d 100644
--- a/rust/cloud-storage/opensearch_client/examples/query_document.rs
+++ b/rust/cloud-storage/opensearch_client/examples/query_document.rs
@@ -15,7 +15,6 @@ async fn main() -> anyhow::Result<()> {
terms: vec!["equation".to_string()],
user_id: "user".to_string(),
document_ids: Vec::new(),
- file_types: Vec::new(),
page: 0,
page_size: 10,
match_type: "partial".to_string(),
diff --git a/rust/cloud-storage/opensearch_client/src/search/documents.rs b/rust/cloud-storage/opensearch_client/src/search/documents.rs
index 666d521ac9..febe69f08b 100644
--- a/rust/cloud-storage/opensearch_client/src/search/documents.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/documents.rs
@@ -9,9 +9,7 @@ use crate::{
};
use crate::SearchOn;
-use opensearch_query_builder::{
- FieldSort, QueryType, SearchRequest, SortOrder, SortType, ToOpenSearchJson,
-};
+use opensearch_query_builder::{FieldSort, SearchRequest, SortOrder, SortType, ToOpenSearchJson};
use serde_json::Value;
#[derive(Clone)]
@@ -34,15 +32,12 @@ impl SearchQueryConfig for DocumentSearchConfig {
struct DocumentQueryBuilder {
inner: SearchQueryBuilder<DocumentSearchConfig>,
- /// File types to filter by
- file_types: Vec<String>,
}
impl DocumentQueryBuilder {
pub fn new(terms: Vec<String>) -> Self {
Self {
inner: SearchQueryBuilder::new(terms),
- file_types: Vec::new(),
}
}
@@ -58,28 +53,13 @@ impl DocumentQueryBuilder {
fn ids_only(ids_only: bool) -> Self;
}
- pub fn file_types(mut self, file_types: Vec<String>) -> Self {
- self.file_types = file_types;
- self
- }
-
fn build_search_request(self) -> Result<SearchRequest> {
- // Build the main bool query containing all terms and any other filters
- let mut bool_query = self.inner.build_bool_query()?;
-
- // CUSTOM ATTRIBUTES SECTION
-
- // If file types are provided, add them to the must query
- if !self.file_types.is_empty() {
- bool_query.must(QueryType::terms("file_type", self.file_types));
- }
-
- // END CUSTOM ATTRIBUTES SECTION
-
// Build the search request with the bool query
// This will automatically wrap the bool query in a function score if
// SearchOn::NameContent is used
- let search_request = self.inner.build_search_request(bool_query.build())?;
+ let search_request = self
+ .inner
+ .build_search_request(self.inner.build_bool_query()?.build())?;
Ok(search_request)
}
@@ -114,7 +94,6 @@ pub struct DocumentSearchArgs {
pub terms: Vec<String>,
pub user_id: String,
pub document_ids: Vec<String>,
- pub file_types: Vec<String>,
pub page: u32,
pub page_size: u32,
pub match_type: String,
@@ -131,7 +110,6 @@ impl DocumentSearchArgs {
.page(self.page)
.user_id(&self.user_id)
.ids(self.document_ids)
- .file_types(self.file_types)
.search_on(self.search_on)
.collapse(self.collapse)
.ids_only(self.ids_only)
diff --git a/rust/cloud-storage/opensearch_client/src/search/documents/test.rs b/rust/cloud-storage/opensearch_client/src/search/documents/test.rs
index bfbb4a3f4a..a32404872c 100644
--- a/rust/cloud-storage/opensearch_client/src/search/documents/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/documents/test.rs
@@ -10,8 +10,7 @@ fn test_build_search_request() -> anyhow::Result<()> {
.user_id("user123")
.search_on(SearchOn::Content)
.collapse(true)
- .ids(vec!["doc1".to_string(), "doc2".to_string()])
- .file_types(vec!["pdf".to_string(), "docx".to_string()]);
+ .ids(vec!["doc1".to_string(), "doc2".to_string()]);
let result = builder.build_search_request()?;
@@ -31,11 +30,6 @@ fn test_build_search_request() -> anyhow::Result<()> {
"content": "test"
}
},
- {
- "terms": {
- "file_type": ["pdf", "docx"]
- }
- }
],
"should": [
{
diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs
index 83a6b16bbb..822a2e1040 100644
--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs
+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs
@@ -110,7 +110,9 @@ pub(in crate::api::search) async fn search_documents(
} else {
// If both the project_ids and owners are empty, we want to get the list of everything the has access to but does not own
// Otherwise, we need a list of all items the user has access to including what they own
- let should_exclude_owner = filters.project_ids.is_empty() && filters.owners.is_empty();
+ let should_exclude_owner = filters.project_ids.is_empty()
+ && filters.owners.is_empty()
+ && filters.file_types.is_empty();
// No filters are provided, we want to get the list of everything the has access to but does not own
ctx.dss_client
@@ -133,7 +135,8 @@ pub(in crate::api::search) async fn search_documents(
// explicitly search over the ids provided in opensearch
let ids_only = !filters.document_ids.is_empty()
|| !filters.project_ids.is_empty()
- || !filters.owners.is_empty();
+ || !filters.owners.is_empty()
+ || !filters.file_types.is_empty();
// If project_ids are provided, we need to filter to ids that are within those projects
// or sub-projects of those projects
@@ -171,13 +174,28 @@ pub(in crate::api::search) async fn search_documents(
return Ok(Vec::new());
}
+ let document_ids = if !filters.file_types.is_empty() {
+ macro_db_client::items::filter::filter_documents_by_file_types(
+ &ctx.db,
+ &document_ids,
+ &filters.file_types,
+ )
+ .await
+ .map_err(SearchError::InternalError)?
+ } else {
+ document_ids
+ };
+
+ if document_ids.is_empty() && ids_only {
+ return Ok(Vec::new());
+ }
+
let results = ctx
.opensearch_client
.search_documents(DocumentSearchArgs {
terms,
user_id: user_id.to_string(),
document_ids,
- file_types: filters.file_types,
page,
page_size,
match_type: req.match_type.to_string(),