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) update search service opensearch queries to use filter (#709)

Will Hutchinsond36b9489cb5f
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 2754373242..2f0fe98b46 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -6015,9 +6015,9 @@ dependencies = [
[[package]]
name = "opensearch_query_builder"
-version = "0.2.4"
+version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2be0ec85f59a03d8bc068a79c4397f48695d89d6129a3bfec8712b378bc5b73c"
+checksum = "88471616dad533248e2ed35df90a076887afcbc000b0913739797fa7fea5df1a"
dependencies = [
"serde",
"serde_json",
diff --git a/rust/cloud-storage/opensearch_client/Cargo.toml b/rust/cloud-storage/opensearch_client/Cargo.toml
index 37ba0c8292..b9433fd6b0 100644
--- a/rust/cloud-storage/opensearch_client/Cargo.toml
+++ b/rust/cloud-storage/opensearch_client/Cargo.toml
@@ -14,13 +14,13 @@ uuid = { workspace = true }
[dependencies]
anyhow = { workspace = true }
+models_opensearch = { path = "../models_opensearch" }
opensearch = { version = "2.3.0" }
-opensearch_query_builder = { version = "0.2.4", default-features = false }
+opensearch_query_builder = { version = "0.2.8", default-features = false }
serde = { workspace = true }
serde_json = { workspace = true }
+strum = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
unicode-segmentation = "1.9.0"
-strum = { workspace = true }
-models_opensearch = { path = "../models_opensearch" }
uuid = { workspace = true }
diff --git a/rust/cloud-storage/opensearch_client/src/search/builder.rs b/rust/cloud-storage/opensearch_client/src/search/builder.rs
index be012fd51a..38df6b589f 100644
--- a/rust/cloud-storage/opensearch_client/src/search/builder.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/builder.rs
@@ -197,6 +197,45 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
self
}
+ /// Creates the filter query to filter results to only those a user has
+ /// access to/has requested.
+ /// This could either be a single term/terms query for ids_only or just user_id
+ /// Or a bool query that contains both of these items
+ fn build_filter_query<'a>(&'a self, user_id_key: &str) -> Result<QueryType<'a>> {
+ if self.ids_only {
+ // We only need to search over the entity ids provided
+ if self.ids.is_empty() {
+ return Err(OpensearchClientError::EmptyIdsWithIdsOnly(T::ENTITY_INDEX));
+ }
+
+ // Return just the id query to filter over
+ Ok(QueryType::terms(T::ID_KEY.to_string(), self.ids.to_vec()))
+ } else {
+ let user_id_query = QueryType::term(user_id_key.to_string(), self.user_id.clone());
+
+ // If there are no ids provided we can return only the user id query to filter over
+ if self.ids.is_empty() {
+ return Ok(user_id_query);
+ }
+
+ // otherwise we need to build the filter bool query to contain both entity ids and user_id
+ // Create a filter bool query that will ensure we only search over items that the user has access to
+ let mut filter_bool_query = BoolQueryBuilder::new();
+
+ // We should have either an entity_id match OR a user_id match
+ filter_bool_query.minimum_should_match(1);
+
+ filter_bool_query.should(QueryType::terms(T::ID_KEY.to_string(), self.ids.to_vec()));
+
+ filter_bool_query.should(QueryType::term(
+ user_id_key.to_string(),
+ self.user_id.clone(),
+ ));
+
+ Ok(filter_bool_query.build().into())
+ }
+ }
+
/// Builds a content and name bool query
pub fn build_content_and_name_bool_query<'a>(
&'a self,
@@ -213,10 +252,6 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
SearchOn::NameContent | SearchOn::Content => {
let mut access_bool_query = BoolQueryBuilder::new();
- // Currently, the minimum should match is always one.
- // This should of the bool query contains the ids and potentially the user_id
- access_bool_query.minimum_should_match(1);
-
// For name OR content queries, we can build a much more simple bool query
let term_must_array: Vec<QueryType<'a>> =
self.build_must_term_query(SearchOn::Content)?;
@@ -305,21 +340,12 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
// Add in the inner bool query on content + owner to must of access bool query
access_bool_query.must(inner_bool_query.build().into());
- // Add any ids to the should array if provided
- if !self.ids.is_empty() {
- access_bool_query
- .should(QueryType::terms(T::ID_KEY.to_string(), self.ids.to_vec()));
- }
-
- // If we are not searching over the ids, we need to add the user_id to the should array
- if !self.ids_only {
- access_bool_query.should(QueryType::term(
- T::USER_ID_KEY.to_string(),
- self.user_id.clone(),
- ));
- }
+ // Filter over only items you have access to
+ let filter_bool_query = self.build_filter_query(T::USER_ID_KEY)?;
+ access_bool_query.filter(filter_bool_query);
- access_bool_query.must(QueryType::term("_index", T::ENTITY_INDEX.as_ref()));
+ // Only search on the provided index
+ access_bool_query.filter(QueryType::term("_index", T::ENTITY_INDEX.as_ref()));
Some(access_bool_query)
}
@@ -330,10 +356,6 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
SearchOn::Name | SearchOn::NameContent => {
let mut bool_query = BoolQueryBuilder::new();
- // Currently, the minimum should match is always one.
- // This should of the bool query contains the ids and potentially the user_id
- bool_query.minimum_should_match(1);
-
// For name OR content queries, we can build a much more simple bool query
let term_must_array: Vec<QueryType<'a>> =
self.build_must_term_query(SearchOn::Name)?;
@@ -343,26 +365,19 @@ impl<T: SearchQueryConfig> SearchQueryBuilder<T> {
bool_query.must(must);
}
- // Add any ids to the should array if provided
- if !self.ids.is_empty() {
- bool_query.should(QueryType::terms(T::ID_KEY.to_string(), self.ids.to_vec()));
- }
-
- // If we are not searching over the ids, we need to add the user_id to the should array
- if !self.ids_only {
- bool_query.should(QueryType::term(
- "user_id", // the names index only has user_id
- self.user_id.clone(),
- ));
- }
+ // Filter over only items you have access to
+ // For name index queries we want to always use user_id as the user id key
+ let filter_bool_query = self.build_filter_query("user_id")?;
+ bool_query.filter(filter_bool_query);
match T::ENTITY_INDEX {
SearchEntityType::Projects => {
- bool_query.must(QueryType::term("_index", SearchIndex::Projects.as_ref()));
+ bool_query
+ .filter(QueryType::term("_index", SearchIndex::Projects.as_ref()));
}
_ => {
- bool_query.must(QueryType::term("_index", SearchIndex::Names.as_ref()));
- bool_query.must(QueryType::term("entity_type", T::ENTITY_INDEX.as_ref()));
+ bool_query.filter(QueryType::term("_index", SearchIndex::Names.as_ref()));
+ bool_query.filter(QueryType::term("entity_type", T::ENTITY_INDEX.as_ref()));
}
}
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 96b4820b00..fe6f8902e4 100644
--- a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
@@ -12,12 +12,76 @@ impl SearchQueryConfig for TestSearchConfig {
fn append_owner_highlights<'a>(
highlight: opensearch_query_builder::Highlight<'a>,
) -> opensearch_query_builder::Highlight<'a> {
- let highlight = highlight.field("test_user_id", create_highlight_field("plain", 1));
-
- highlight
+ highlight.field("test_user_id", create_highlight_field("plain", 1))
}
}
+#[test]
+fn test_build_filter_query() -> anyhow::Result<()> {
+ // Test ids only
+ let ids = vec!["id1".to_string(), "id2".to_string()];
+ let builder = SearchQueryBuilder::<TestSearchConfig>::new(vec!["test".to_string()])
+ .user_id("user123")
+ .ids(ids.clone())
+ .ids_only(true);
+
+ let result = builder.build_filter_query(TestSearchConfig::USER_ID_KEY)?;
+
+ let expected = serde_json::json!({
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ });
+
+ assert_eq!(result.to_json(), expected);
+
+ // Test !ids_only with no ids
+ let builder = SearchQueryBuilder::<TestSearchConfig>::new(vec!["test".to_string()])
+ .user_id("user123")
+ .ids(vec![])
+ .ids_only(false);
+
+ let result = builder.build_filter_query(TestSearchConfig::USER_ID_KEY)?;
+
+ let expected = serde_json::json!({
+ "term": {
+ "test_user_id": "user123"
+ }
+ });
+
+ assert_eq!(result.to_json(), expected);
+
+ // Test !ids_only with ids
+ let builder = SearchQueryBuilder::<TestSearchConfig>::new(vec!["test".to_string()])
+ .user_id("user123")
+ .ids(ids.clone())
+ .ids_only(false);
+
+ let result = builder.build_filter_query(TestSearchConfig::USER_ID_KEY)?;
+
+ let expected = serde_json::json!({
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "test_user_id": "user123"
+ }
+ }
+ ]
+ }
+ });
+
+ assert_eq!(result.to_json(), expected);
+
+ Ok(())
+}
+
#[test]
fn test_build_search_request() -> anyhow::Result<()> {
let bool_query = QueryType::bool_query().build();
@@ -275,21 +339,19 @@ fn test_build_bool_query() -> anyhow::Result<()> {
],
}
},
- {"term": {"_index": "documents"}},
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
- {
- "term": {
- "test_user_id": "user123"
- }
- }
],
- "minimum_should_match": 1,
+ "filter": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {"terms": {"entity_id": ["id1", "id2"]}},
+ {"term": {"test_user_id": "user123"}}
+ ]
+ }
+ },
+ {"term": {"_index": "documents"}}
+ ]
}
});
@@ -332,17 +394,12 @@ fn test_build_bool_query() -> anyhow::Result<()> {
],
}
},
- {"term": {"_index": "documents"}},
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
],
- "minimum_should_match": 1,
- }
+ "filter": [
+ {"terms": {"entity_id": ["id1", "id2"]}},
+ {"term": {"_index": "documents"}}
+ ]
+ }
});
assert_eq!(query.build().to_json(), expected);
@@ -364,7 +421,6 @@ fn test_build_bool_query() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
"must": [
{
"bool": {
@@ -389,24 +445,23 @@ fn test_build_bool_query() -> anyhow::Result<()> {
]
}
},
- {
- "term": {
- "_index": "documents"
- }
- }
],
- "should": [
+ "filter": [
{
"terms": {
"entity_id": ["id1", "id2"]
}
+ },
+ {
+ "term": {
+ "_index": "documents",
+ }
}
]
}
},
{
"bool": {
- "minimum_should_match": 1,
"must": [
{
"match_phrase_prefix": {
@@ -415,25 +470,25 @@ fn test_build_bool_query() -> anyhow::Result<()> {
}
}
},
+ ],
+ "filter": [
{
- "term": {
- "_index": "names"
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "term": {
- "entity_type": "documents"
- }
- }
- ],
- "should": [
+ "term": {
+ "_index": "names",
+ }
+ },
{
- "terms": {
- "entity_id": ["id1", "id2"]
- }
+ "term": {
+ "entity_type": "documents"
+ }
}
]
- }
+ }
}
]
}
diff --git a/rust/cloud-storage/opensearch_client/src/search/channels.rs b/rust/cloud-storage/opensearch_client/src/search/channels.rs
index 4a867cdb94..15275e240e 100644
--- a/rust/cloud-storage/opensearch_client/src/search/channels.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/channels.rs
@@ -121,17 +121,17 @@ impl ChannelMessageQueryBuilder {
// Add thread_ids to must clause if provided
if !self.thread_ids.is_empty() {
- bool_query.must(QueryType::terms("thread_id", self.thread_ids.clone()));
+ bool_query.filter(QueryType::terms("thread_id", self.thread_ids.clone()));
}
// Add mentions to must clause if provided
if !self.mentions.is_empty() {
- bool_query.must(QueryType::terms("mentions", self.mentions.clone()));
+ bool_query.filter(QueryType::terms("mentions", self.mentions.clone()));
}
// Add sender_ids to must clause if provided
if !self.sender_ids.is_empty() {
- bool_query.must(QueryType::terms("sender_id", self.sender_ids.clone()));
+ bool_query.filter(QueryType::terms("sender_id", self.sender_ids.clone()));
}
content_and_name_bool_queries.content_bool_query = Some(bool_query);
diff --git a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs b/rust/cloud-storage/opensearch_client/src/search/channels/test.rs
index 220cff9c7d..0f5bf30ece 100644
--- a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/channels/test.rs
@@ -25,7 +25,6 @@ fn test_build_search_request() -> anyhow::Result<()> {
"highlight": ChannelMessageSearchConfig::append_owner_highlights(ChannelMessageSearchConfig::default_highlight()).to_json(),
"query": {
"bool": {
- "minimum_should_match": 1,
"must": [
{
"bool": {
@@ -48,12 +47,19 @@ fn test_build_search_request() -> anyhow::Result<()> {
]
}
},
- {
- "term": {
- "_index": "channels"
- }
- },
- {
+ ],
+ "filter": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {"terms": {"entity_id": ["id1", "id2"]}},
+ {"term": {"sender_id": "user123"}}
+ ]
+ }
+ },
+ {"term": {"_index": "channels"}},
+ {
"terms": {
"thread_id": ["thread1", "thread2"]
}
@@ -68,18 +74,6 @@ fn test_build_search_request() -> anyhow::Result<()> {
"sender_id": ["sender1", "sender2"]
}
}
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
- {
- "term": {
- "sender_id": "user123"
- }
- }
]
}
},
diff --git a/rust/cloud-storage/opensearch_client/src/search/chats.rs b/rust/cloud-storage/opensearch_client/src/search/chats.rs
index afe48bf265..c8e8576e16 100644
--- a/rust/cloud-storage/opensearch_client/src/search/chats.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/chats.rs
@@ -99,7 +99,7 @@ impl ChatQueryBuilder {
// Add role to must clause if provided
if !self.role.is_empty() {
let should_query = should_wildcard_field_query_builder("role", &self.role);
- bool_query.must(should_query);
+ bool_query.filter(should_query);
}
content_and_name_bool_queries.content_bool_query = Some(bool_query);
diff --git a/rust/cloud-storage/opensearch_client/src/search/chats/test.rs b/rust/cloud-storage/opensearch_client/src/search/chats/test.rs
index 771c61d6a2..5f01127610 100644
--- a/rust/cloud-storage/opensearch_client/src/search/chats/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/chats/test.rs
@@ -16,78 +16,88 @@ fn test_build_search_request() -> anyhow::Result<()> {
let result = builder.build_search_request()?;
let expected = serde_json::json!({
- "from": 20,
- "size": 20,
- "collapse": {
- "field": "entity_id"
- },
- "sort": ChatSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
- "highlight": ChatSearchConfig::append_owner_highlights(ChatSearchConfig::default_highlight()).to_json(),
- "query": {
+ "from": 20,
+ "size": 20,
+ "collapse": {
+ "field": "entity_id"
+ },
+ "sort": ChatSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
+ "highlight": ChatSearchConfig::append_owner_highlights(ChatSearchConfig::default_highlight()).to_json(),
+ "query": {
+ "bool": {
+ "filter": [
+ {
"bool": {
- "must": [
+ "minimum_should_match": 1,
+ "should": [
{
- "bool": {
- "minimum_should_match": 1,
- "should": [
- {
- "wildcard": {
- "user_id": {
- "value": "macro|test*",
- "case_insensitive": true,
- "boost": 5000.0
- }
- }
- },
- {
- "match_phrase": {
- "content": "test"
- }
- }
- ]
+ "terms": {
+ "entity_id": ["chat1", "chat2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user123"
+ }
+ }
+ ]
+ }
+ },
+ {
+ "term": {
+ "_index": "chats"
+ }
+ },
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "role": {
+ "case_insensitive": true,
+ "value": "*user*"
}
+ }
},
- {"term": {"_index": "chats"}},
- {
- "bool": {
- "minimum_should_match": 1,
- "should": [
- {
- "wildcard": {
- "role": {
- "case_insensitive": true,
- "value": "*user*"
- }
- }
- },
- {
- "wildcard": {
- "role": {
- "case_insensitive": true,
- "value": "*assistant*"
- }
- }
- }
- ]
- }
+ {
+ "wildcard": {
+ "role": {
+ "case_insensitive": true,
+ "value": "*assistant*"
}
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["chat1", "chat2"]
- }
- },
- {
- "term": {
- "user_id": "user123"
- }
+ }
+ }
+ ]
+ }
+ }
+ ],
+ "must": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "user_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
}
- ],
- "minimum_should_match": 1,
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
- }
- });
+ }
+ ]
+ }
+ },
+ });
assert_eq!(result.to_json(), expected);
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 4db3684c90..d64e90d558 100644
--- a/rust/cloud-storage/opensearch_client/src/search/documents/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/documents/test.rs
@@ -15,54 +15,64 @@ fn test_build_search_request() -> anyhow::Result<()> {
let result = builder.build_search_request()?;
let expected = serde_json::json!({
- "from": 20,
- "size": 20,
- "collapse": {
- "field": "entity_id"
- },
- "sort": DocumentSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
- "highlight": DocumentSearchConfig::append_owner_highlights(DocumentSearchConfig::default_highlight()).to_json(),
- "query": {
- "bool": {
- "must": [
- {
- "bool": {
- "minimum_should_match": 1,
- "should": [
- {
- "wildcard": {
- "owner_id": {
- "value": "macro|test*",
- "case_insensitive": true,
- "boost": 5000.0
- }
- }
- },
- {
- "match_phrase": {
- "content": "test"
- }
- }
- ]
- }
- },
- {"term": {"_index": "documents"}},
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["doc1", "doc2"]
- }
- },
- {
- "term": {
- "owner_id": "user123"
- }
- }
- ],
- "minimum_should_match": 1,
- }
- }
+ "from": 20,
+ "size": 20,
+ "collapse": {
+ "field": "entity_id"
+ },
+ "sort": DocumentSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
+ "highlight": DocumentSearchConfig::append_owner_highlights(DocumentSearchConfig::default_highlight()).to_json(),
+ "query": {
+ "bool": {
+ "filter": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["doc1", "doc2"]
+ }
+ },
+ {
+ "term": {
+ "owner_id": "user123"
+ }
+ }
+ ]
+ }
+ },
+ {
+ "term": {
+ "_index": "documents"
+ }
+ }
+ ],
+ "must": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "owner_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+ },
});
assert_eq!(result.to_json(), expected);
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rs
index 47d30c30c6..df87b4bae1 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs
@@ -124,31 +124,31 @@ impl EmailQueryBuilder {
// If link_ids are provided, add them to the query
if !self.link_ids.is_empty() {
- bool_query.must(QueryType::terms("link_id", self.link_ids.clone()));
+ bool_query.filter(QueryType::terms("link_id", self.link_ids.clone()));
}
if !self.sender.is_empty() {
// Create new query for senders
let senders_query = should_wildcard_field_query_builder("sender", &self.sender);
- bool_query.must(senders_query);
+ bool_query.filter(senders_query);
}
if !self.cc.is_empty() {
let ccs_query = should_wildcard_field_query_builder("cc", &self.cc);
- bool_query.must(ccs_query);
+ bool_query.filter(ccs_query);
}
if !self.bcc.is_empty() {
// Create new query for bccs
let bccs_query = should_wildcard_field_query_builder("bcc", &self.bcc);
- bool_query.must(bccs_query);
+ bool_query.filter(bccs_query);
}
if !self.recipients.is_empty() {
// Create new query for recipients
let recipients_query =
should_wildcard_field_query_builder("recipients", &self.recipients);
- bool_query.must(recipients_query);
+ bool_query.filter(recipients_query);
}
content_and_name_bool_queries.content_bool_query = Some(bool_query);
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
index ccd5ed9fcf..3309489c9a 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
@@ -20,61 +20,28 @@ fn test_build_search_request() -> anyhow::Result<()> {
let result = builder.build_search_request()?;
let expected = serde_json::json!({
- "from": 20,
- "size": 20,
- "collapse": {
- "field": "entity_id"
- },
- "sort": EmailSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
- "highlight": EmailSearchConfig::append_owner_highlights(EmailSearchConfig::default_highlight()).to_json(),
- "query": {
+ "from": 20,
+ "size": 20,
+ "collapse": {
+ "field": "entity_id"
+ },
+ "sort": EmailSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
+ "highlight": EmailSearchConfig::append_owner_highlights(EmailSearchConfig::default_highlight()).to_json(),
+ "query": {
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "sender": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
-
- }
+ "terms": {
+ "entity_id": ["thread1", "thread2"]
}
},
{
- "wildcard": {
- "cc": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
- }
- },
- {
- "wildcard": {
- "bcc": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
- }
- },
- {
- "wildcard": {
- "recipients": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
- }
- },
- {
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user123"
}
}
]
@@ -151,28 +118,66 @@ fn test_build_search_request() -> anyhow::Result<()> {
}
}
],
- "must_not": [
+ "must": [
{
- "term": {
- "labels": "TRASH",
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "sender": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "cc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "bcc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "recipients": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["thread1", "thread2"]
- }
- },
+ "must_not": [
{
"term": {
- "user_id": "user123"
+ "labels": "TRASH"
}
}
]
}
},
- });
+ });
assert_eq!(result.to_json(), expected);
diff --git a/rust/cloud-storage/opensearch_client/src/search/projects/test.rs b/rust/cloud-storage/opensearch_client/src/search/projects/test.rs
index 577d11f5d2..d897931ab3 100644
--- a/rust/cloud-storage/opensearch_client/src/search/projects/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/projects/test.rs
@@ -15,39 +15,49 @@ fn test_build_search_request() -> anyhow::Result<()> {
let result = builder.build_search_request()?;
let expected = serde_json::json!({
- "from": 20,
- "size": 20,
- "collapse": {
- "field": "entity_id"
- },
- "sort": ProjectSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
- "highlight": ProjectSearchConfig::append_owner_highlights(ProjectSearchConfig::default_highlight()).to_json(),
- "query": {
+ "from": 20,
+ "size": 20,
+ "collapse": {
+ "field": "entity_id"
+ },
+ "sort": ProjectSearchConfig::default_sort_types().iter().map(|s| s.to_json()).collect::<Vec<_>>(),
+ "highlight": ProjectSearchConfig::append_owner_highlights(ProjectSearchConfig::default_highlight()).to_json(),
+ "query": {
+ "bool": {
+ "filter": [
+ {
"bool": {
- "must": [
- {
- "match_phrase": {
- "project_name": "test"
- }
- },
- {"term": {"_index": "projects"}},
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["proj1", "proj2"]
- }
- },
- {
- "term": {
- "user_id": "user123"
- }
- }
- ],
- "minimum_should_match": 1,
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["proj1", "proj2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user123"
+ }
+ }
+ ]
}
- }
- });
+ },
+ {
+ "term": {
+ "_index": "projects"
+ }
+ }
+ ],
+ "must": [
+ {
+ "match_phrase": {
+ "project_name": "test"
+ }
+ }
+ ]
+ }
+ },
+ });
assert_eq!(result.to_json(), expected);
diff --git a/rust/cloud-storage/opensearch_client/src/search/unified.rs b/rust/cloud-storage/opensearch_client/src/search/unified.rs
index bba3c00d4e..5cb6b9cb01 100644
--- a/rust/cloud-storage/opensearch_client/src/search/unified.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/unified.rs
@@ -400,6 +400,8 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques
let title_keys: Vec<&'static str> = title_keys.into_iter().collect();
let mut bool_query = BoolQueryBuilder::new();
+
+ // There will always be 1 query as the indices are never empty
bool_query.minimum_should_match(1);
if args.search_indices.contains(&SearchEntityType::Documents) {
@@ -527,8 +529,10 @@ fn build_unified_search_request(args: &UnifiedSearchArgs) -> Result<SearchReques
// fields
match args.search_on {
SearchOn::Content | SearchOn::NameContent => {
- highlight = highlight.field("user_id", create_highlight_field("plain", 1));
- highlight = highlight.field("owner_id", create_highlight_field("plain", 1));
+ // Commenting out for now as it's causing highlights on the user_id of filter matches
+ // Need to think of a different way to surface the owner matches
+ // highlight = highlight.field("user_id", create_highlight_field("plain", 1));
+ // highlight = highlight.field("owner_id", create_highlight_field("plain", 1));
highlight = highlight.field("sender", create_highlight_field("plain", 1));
highlight = highlight.field("recipients", create_highlight_field("plain", 1));
highlight = highlight.field("cc", create_highlight_field("plain", 1));
@@ -555,6 +559,8 @@ pub(crate) async fn search_unified(
) -> Result<Vec<SearchHit>> {
let search_request = build_unified_search_request(&args)?.to_json();
+ tracing::trace!("search request {:?}", search_request);
+
let mut search_indices: Vec<&str> = args.search_indices.iter().map(|i| i.as_ref()).collect();
match args.search_on {
diff --git a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs b/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
index 94c0c3ba76..bd85a16c6f 100644
--- a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
@@ -449,34 +449,27 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
let result = build_unified_search_request(&unified_search_args)?;
- let expected = serde_json::json!(
- {
+ let expected = serde_json::json!({
"collapse": {
"field": "entity_id"
},
"from": 20,
"highlight": {
"fields": {
- "content": {
- "number_of_fragments": 500,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "user_id": {
+ "bcc": {
"number_of_fragments": 1,
"post_tags": ["</macro_em>"],
"pre_tags": ["<macro_em>"],
"type": "plain"
},
- "owner_id": {
+ "cc": {
"number_of_fragments": 1,
"post_tags": ["</macro_em>"],
"pre_tags": ["<macro_em>"],
"type": "plain"
},
- "sender": {
- "number_of_fragments": 1,
+ "content": {
+ "number_of_fragments": 500,
"post_tags": ["</macro_em>"],
"pre_tags": ["<macro_em>"],
"type": "plain"
@@ -487,13 +480,7 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
"pre_tags": ["<macro_em>"],
"type": "plain"
},
- "cc": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "bcc": {
+ "sender": {
"number_of_fragments": 1,
"post_tags": ["</macro_em>"],
"pre_tags": ["<macro_em>"],
@@ -508,24 +495,19 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "owner_id": {
- "case_insensitive": true,
- "value": "macro|test*",
- "boost": 5000.0
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "owner_id": "user"
}
}
]
@@ -537,23 +519,6 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
- {
- "term": {
- "owner_id": "user"
- }
- }
- ]
- }
- },
- {
- "bool": {
- "minimum_should_match": 1,
"must": [
{
"bool": {
@@ -561,43 +526,39 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
"should": [
{
"wildcard": {
- "sender": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
- }
- },
- {
- "wildcard": {
- "cc": {
+ "owner_id": {
+ "boost": 5000.0,
"case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
+ "value": "macro|test*"
}
}
},
{
- "wildcard": {
- "bcc": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
+ "match_phrase": {
+ "content": "test"
}
- },
+ }
+ ]
+ }
+ }
+ ]
+ }
+ },
+ {
+ "bool": {
+ "filter": [
+ {
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
{
- "wildcard": {
- "recipients": {
- "case_insensitive": true,
- "value": "test*",
- "boost": 5000.0
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user"
}
}
]
@@ -706,30 +667,6 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
}
],
- "must_not": [
- {
- "term": {
- "labels": "TRASH",
- }
- },
- ],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
- {
- "term": {
- "user_id": "user"
- }
- }
- ]
- }
- },
- {
- "bool": {
- "minimum_should_match": 1,
"must": [
{
"bool": {
@@ -737,10 +674,37 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
"should": [
{
"wildcard": {
- "sender_id": {
+ "sender": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "cc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "bcc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "recipients": {
+ "boost": 5000.0,
"case_insensitive": true,
- "value": "macro|test*",
- "boost": 5000.0
+ "value": "test*"
}
}
},
@@ -751,6 +715,24 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
]
}
+ }
+ ],
+ "must_not": [
+ {
+ "term": {
+ "labels": "TRASH"
+ }
+ }
+ ]
+ }
+ },
+ {
+ "bool": {
+ "filter": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
},
{
"term": {
@@ -773,10 +755,26 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
}
],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": ["id1", "id2"]
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "sender_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -784,24 +782,19 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "user_id": {
- "case_insensitive": true,
- "value": "macro|test*",
- "boost": 5000.0
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user"
}
}
]
@@ -836,15 +829,26 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "user_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -921,7 +925,7 @@ 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"
},
@@ -930,22 +934,14 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
"fields": {
"name": {
"number_of_fragments": 1,
- "post_tags": [
- "</macro_em>"
- ],
- "pre_tags": [
- "<macro_em>"
- ],
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
"type": "plain"
},
"project_name": {
"number_of_fragments": 1,
- "post_tags": [
- "</macro_em>"
- ],
- "pre_tags": [
- "<macro_em>"
- ],
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
"type": "plain"
}
},
@@ -957,11 +953,22 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -975,18 +982,10 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": [
- "id1",
- "id2"
- ]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -994,11 +993,22 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1012,18 +1022,10 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": [
- "id1",
- "id2"
- ]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1031,11 +1033,22 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1049,18 +1062,10 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": [
- "id1",
- "id2"
- ]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1068,11 +1073,22 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "project_name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1081,18 +1097,10 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": [
- "id1",
- "id2"
- ]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "project_name": "test"
}
}
]
@@ -1110,7 +1118,8 @@ fn test_build_unified_search_request_name() -> anyhow::Result<()> {
"entity_id": "desc"
}
]
- });
+ }
+ );
assert_eq!(result.to_json(), expected);
@@ -1199,12 +1208,6 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"pre_tags": ["<macro_em>"],
"type": "plain"
},
- "owner_id": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
"project_name": {
"number_of_fragments": 1,
"post_tags": ["</macro_em>"],
@@ -1222,12 +1225,6 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"post_tags": ["</macro_em>"],
"pre_tags": ["<macro_em>"],
"type": "plain"
- },
- "user_id": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
}
},
"require_field_match": false
@@ -1242,24 +1239,19 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "owner_id": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "macro|test*"
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "owner_id": "user"
}
}
]
@@ -1271,15 +1263,26 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "owner_id": "user"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "owner_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -1287,11 +1290,22 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1305,15 +1319,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1328,51 +1337,19 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "sender": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "test*"
- }
- }
- },
- {
- "wildcard": {
- "cc": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "test*"
- }
- }
- },
- {
- "wildcard": {
- "bcc": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "test*"
- }
- }
- },
- {
- "wildcard": {
- "recipients": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "test*"
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user"
}
}
]
@@ -1481,22 +1458,60 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "must_not": [
- {
- "term": {
- "labels": "TRASH",
- }
- }
- ],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": ["id1", "id2"]
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "sender": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "cc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "bcc": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "wildcard": {
+ "recipients": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
- },
+ }
+ ],
+ "must_not": [
{
"term": {
- "user_id": "user"
+ "labels": "TRASH"
}
}
]
@@ -1504,11 +1519,22 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1522,15 +1548,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
- {
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1545,27 +1566,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "bool": {
- "minimum_should_match": 1,
- "should": [
- {
- "wildcard": {
- "sender_id": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "macro|test*"
- }
- }
- },
- {
- "match_phrase": {
- "content": "test"
- }
- }
- ]
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
@@ -1589,10 +1593,26 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": ["id1", "id2"]
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "sender_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -1600,11 +1620,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
@@ -1618,10 +1637,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": ["id1", "id2"]
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1636,24 +1655,19 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "user_id": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "macro|test*"
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user"
}
}
]
@@ -1688,15 +1702,26 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "user_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -1704,11 +1729,22 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1722,15 +1758,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "name": "test"
}
}
]
@@ -1745,24 +1776,19 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
"should": [
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "user_id": {
- "boost": 5000.0,
- "case_insensitive": true,
- "value": "macro|test*"
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "user_id": "user"
}
}
]
@@ -1774,15 +1800,26 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "user_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
+ ]
}
}
]
@@ -1790,11 +1827,22 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
},
{
"bool": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
- "match_phrase": {
- "project_name": "test"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "terms": {
+ "entity_id": ["id1", "id2"]
+ }
+ },
+ {
+ "term": {
+ "user_id": "user"
+ }
+ }
+ ]
}
},
{
@@ -1803,15 +1851,10 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
- "should": [
- {
- "terms": {
- "entity_id": ["id1", "id2"]
- }
- },
+ "must": [
{
- "term": {
- "user_id": "user"
+ "match_phrase": {
+ "project_name": "test"
}
}
]
@@ -1860,31 +1903,65 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {
let result = build_unified_search_request(&unified_search_args)?;
let expected = serde_json::json!(
- {
+ {
+ "collapse": {
+ "field": "entity_id"
+ },
+ "from": 20,
+ "highlight": {
+ "fields": {
+ "bcc": {
+ "number_of_fragments": 1,
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
+ "type": "plain"
+ },
+ "cc": {
+ "number_of_fragments": 1,
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
+ "type": "plain"
+ },
+ "content": {
+ "number_of_fragments": 500,
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
+ "type": "plain"
+ },
+ "recipients": {
+ "number_of_fragments": 1,
+ "post_tags": ["</macro_em>"],
+ "pre_tags": ["<macro_em>"],
+ "type": "plain"
+ },
+ "sender": {
+ "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": {
- "minimum_should_match": 1,
- "must": [
+ "filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
- "wildcard": {
- "owner_id": {
- "case_insensitive": true,
- "value": "macro|test*",
- "boost": 5000.0
- }
+ "terms": {
+ "entity_id": ["id1", "id2"]
}
},
{
- "match_phrase": {
- "content": "test"
+ "term": {
+ "owner_id": "user"
}
}
]
@@ -1896,19 +1973,27 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {
}
}
],
- "should": [
+ "must": [
{
- "terms": {
- "entity_id": [
- "id1",
- "id2"
+ "bool": {
+ "minimum_should_match": 1,
+ "should": [
+ {
+ "wildcard": {
+ "owner_id": {
+ "boost": 5000.0,
+ "case_insensitive": true,
+ "value": "macro|test*"
+ }
+ }
+ },
+ {
+ "match_phrase": {
+ "content": "test"
+ }
+ }
]
}
- },
- {
- "term": {
- "owner_id": "user"
- }
}
]
}
@@ -1916,7 +2001,6 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {
]
}
},
- "from": 20,
"size": 20,
"sort": [
{
@@ -1925,63 +2009,9 @@ fn test_build_unified_search_request_single_index() -> anyhow::Result<()> {
{
"entity_id": "desc"
}
- ],
- "highlight": {
- "require_field_match": true,
- "fields": {
- "content": {
- "type": "plain",
- "number_of_fragments": 500,
- "pre_tags": [
- "<macro_em>"
- ],
- "post_tags": [
- "</macro_em>"
- ]
- },
- "user_id": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "owner_id": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "sender": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "recipients": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "cc": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- },
- "bcc": {
- "number_of_fragments": 1,
- "post_tags": ["</macro_em>"],
- "pre_tags": ["<macro_em>"],
- "type": "plain"
- }
- }
- },
- "collapse": {
- "field": "entity_id"
- }
+ ]
}
- );
+ );
assert_eq!(result.to_json(), expected);