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(opensearch_client): error on invalid query creation (#663)

Will Hutchinson83b15d6d7df5
diff --git a/rust/cloud-storage/opensearch_client/src/error.rs b/rust/cloud-storage/opensearch_client/src/error.rs
index c04de995df..0ea0b10dc3 100644
--- a/rust/cloud-storage/opensearch_client/src/error.rs
+++ b/rust/cloud-storage/opensearch_client/src/error.rs
@@ -1,3 +1,4 @@
+use models_opensearch::SearchEntityType;
use opensearch::{Error, http::response::Response};
#[derive(thiserror::Error, Debug, serde::Serialize, PartialEq)]
@@ -47,6 +48,9 @@ pub enum OpensearchClientError {
#[error("bool query not built")]
BoolQueryNotBuilt,
+
+ #[error("empty ids with ids only for {0}")]
+ EmptyIdsWithIdsOnly(SearchEntityType),
}
impl From<anyhow::Error> for OpensearchClientError {
diff --git a/rust/cloud-storage/opensearch_client/src/search/builder.rs b/rust/cloud-storage/opensearch_client/src/search/builder.rs
index 57d3839cbd..be012fd51a 100644
--- a/rust/cloud-storage/opensearch_client/src/search/builder.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/builder.rs
@@ -201,6 +201,13 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
pub fn build_content_and_name_bool_query<'a>(
&'a self,
) -> Result<ContentAndNameBoolQueries<'a>> {
+ // FIXES: https://linear.app/macro-eng/issue/BAC-173/sending-invalid-queries-to-opensearch
+ // If we try and build queries with ids only = true and no ids provided we should
+ // error out as it creates an invalid query since we have minimum_should_match = 1.
+ if self.ids_only && self.ids.is_empty() {
+ return Err(OpensearchClientError::EmptyIdsWithIdsOnly(T::ENTITY_INDEX));
+ }
+
let content_bool_query = match self.search_on {
SearchOn::Name => None,
SearchOn::NameContent | SearchOn::Content => {
diff --git a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs b/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
index dcf48f9073..96b4820b00 100644
--- a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
@@ -190,6 +190,49 @@ fn test_build_search_request() -> anyhow::Result<()> {
Ok(())
}
+#[test]
+fn test_build_content_and_name_query_empty_ids() -> anyhow::Result<()> {
+ // Empty ids ok with ids only false
+ let term = vec!["test".to_string()];
+ let ids: Vec<String> = vec![];
+ let user_id = "user123";
+ let page = 1;
+ let page_size = 20;
+
+ let builder = SearchQueryBuilder::<TestSearchConfig>::new(term.clone())
+ .match_type("exact")
+ .page_size(page_size)
+ .page(page)
+ .user_id(user_id)
+ .search_on(SearchOn::Content)
+ .ids(ids.clone())
+ .ids_only(false);
+
+ let result = builder.build_content_and_name_bool_query()?;
+
+ assert!(result.name_bool_query.is_none());
+ assert!(result.content_bool_query.is_some());
+
+ // Empty ids fails with ids only true
+ let builder = SearchQueryBuilder::<TestSearchConfig>::new(term.clone())
+ .match_type("exact")
+ .page_size(page_size)
+ .page(page)
+ .user_id(user_id)
+ .search_on(SearchOn::Content)
+ .ids(ids.clone())
+ .ids_only(true);
+
+ let error = builder.build_content_and_name_bool_query().err().unwrap();
+
+ assert_eq!(
+ OpensearchClientError::EmptyIdsWithIdsOnly(SearchEntityType::Documents),
+ error
+ );
+
+ Ok(())
+}
+
#[test]
fn test_build_bool_query() -> anyhow::Result<()> {
let terms = vec!["test".to_string()];