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.

perf(search): remove wildcard matching and use match phrase prefix only (#2273)

gbirman54004495f76e
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 71486590fb..cd443e0fde 100644
--- a/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/builder/test.rs
@@ -187,6 +187,7 @@ fn test_build_bool_query() -> anyhow::Result<()> {
"match_phrase_prefix": {
"content": {
"query": "test",
+ "max_expansions": 256
}
}
},
@@ -231,6 +232,7 @@ fn test_build_must_term_query() -> anyhow::Result<()> {
"match_phrase_prefix": {
"content": {
"query": "test",
+ "max_expansions": 256
}
}
});
@@ -274,31 +276,18 @@ fn test_build_must_term_query_multiple_terms() -> anyhow::Result<()> {
}
#[test]
-fn test_build_must_term_query_term_with_short_last_word() -> anyhow::Result<()> {
+fn test_build_must_term_query_short_last_word_no_wildcard() -> anyhow::Result<()> {
let terms = vec!["test Ab".to_string()];
let builder = SearchQueryBuilder::<TestSearchConfig>::new(terms).match_type("partial");
let terms_must_vec = builder.build_must_term_query()?;
let expected = serde_json::json!({
- "bool": {
- "must": [
- {
- "match_phrase_prefix": {
- "content": {
- "query": "test",
- }
- }
- },
- {
- "wildcard": {
- "content": {
- "case_insensitive": true,
- "value": "*ab*",
- }
- }
- }
- ]
+ "match_phrase_prefix": {
+ "content": {
+ "query": "test Ab",
+ "max_expansions": 256
+ }
}
});
diff --git a/rust/cloud-storage/opensearch_client/src/search/query.rs b/rust/cloud-storage/opensearch_client/src/search/query.rs
index 6db431acf8..dc26fd085c 100644
--- a/rust/cloud-storage/opensearch_client/src/search/query.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/query.rs
@@ -5,7 +5,6 @@ use std::borrow::Cow;
use crate::{Result, error::OpensearchClientError};
use opensearch_query_builder::*;
-use unicode_segmentation::UnicodeSegmentation;
/// Containing keys for the title and content fields
pub struct Keys<'a> {
@@ -26,10 +25,7 @@ pub enum QueryKey {
Regexp,
}
-/// Validates that the last term lenght is >= 3 graphemes
-fn validate_last_term_length(term: &str) -> bool {
- UnicodeSegmentation::graphemes(term, true).count() >= 3
-}
+const MATCH_PHRASE_PREFIX_MAX_EXPANSIONS: u32 = 256;
pub(crate) struct CreateQueryParams<'a> {
/// The query key to use
@@ -41,7 +37,6 @@ pub(crate) struct CreateQueryParams<'a> {
}
/// Creates a query for a given term
-/// Fixes https://linear.app/macro-eng/issue/M-5094/unified-search-match-prefix-on-phrase-should-not-constrain-terms
pub(crate) fn create_query<'a>(params: CreateQueryParams<'a>) -> QueryType<'a> {
let CreateQueryParams {
query_key,
@@ -49,53 +44,14 @@ pub(crate) fn create_query<'a>(params: CreateQueryParams<'a>) -> QueryType<'a> {
term,
} = params;
- // We need to create a more complex combo query if the last word in a term is less than 3 characters.
- let mut terms = term.split(' ').collect::<Vec<_>>();
- let last_term = terms.last().unwrap_or(&"");
- let is_last_term_long_enough = validate_last_term_length(last_term);
-
- if !last_term.is_empty() && !is_last_term_long_enough && terms.len() > 1 {
- tracing::trace!("last value in term has length less than 3, building combo query");
-
- let last_part_of_term = terms.pop().unwrap();
- let first_part_of_term = terms.join(" ");
- let wildcard_pattern = format!("*{}*", last_part_of_term.to_lowercase());
-
- let first_term_query = match query_key {
- QueryKey::MatchPhrase => {
- QueryType::MatchPhrase(MatchPhraseQuery::new(field.to_string(), first_part_of_term))
- }
- QueryKey::MatchPhrasePrefix => QueryType::MatchPhrasePrefix(
- MatchPhrasePrefixQuery::new(field.to_string(), first_part_of_term),
- ),
- QueryKey::Regexp => {
- QueryType::Regexp(RegexpQuery::new(field.to_string(), first_part_of_term))
- }
- };
-
- let second_term_query = QueryType::WildCard(WildcardQuery::new(
- field.to_string(),
- wildcard_pattern,
- true,
- None,
- ));
-
- let mut bool_query = QueryType::bool_query();
-
- bool_query.must(first_term_query);
- bool_query.must(second_term_query);
-
- return bool_query.build().into();
- }
-
match query_key {
QueryKey::MatchPhrase => {
QueryType::MatchPhrase(MatchPhraseQuery::new(field.to_string(), term.to_string()))
}
- QueryKey::MatchPhrasePrefix => QueryType::MatchPhrasePrefix(MatchPhrasePrefixQuery::new(
- field.to_string(),
- term.to_string(),
- )),
+ QueryKey::MatchPhrasePrefix => QueryType::MatchPhrasePrefix(
+ MatchPhrasePrefixQuery::new(field.to_string(), term.to_string())
+ .max_expansions(MATCH_PHRASE_PREFIX_MAX_EXPANSIONS),
+ ),
QueryKey::Regexp => {
QueryType::Regexp(RegexpQuery::new(field.to_string(), term.to_string()))
}
diff --git a/rust/cloud-storage/opensearch_client/src/search/query/test.rs b/rust/cloud-storage/opensearch_client/src/search/query/test.rs
index 3c58daace9..24b8f61efb 100644
--- a/rust/cloud-storage/opensearch_client/src/search/query/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/query/test.rs
@@ -48,29 +48,16 @@ fn test_query_key_create_query() -> anyhow::Result<()> {
}
#[test]
-fn test_query_key_short_last_word() -> anyhow::Result<()> {
+fn test_query_key_create_query_partial() -> anyhow::Result<()> {
let field = "test";
let term = "test Ab";
let expected = serde_json::json!({
- "bool": {
- "must": [
- {
- "match_phrase_prefix": {
- "test": {
- "query": "test",
- }
- }
- },
- {
- "wildcard": {
- "test": {
- "case_insensitive": true,
- "value": "*ab*",
- }
- }
- }
- ]
+ "match_phrase_prefix": {
+ "test": {
+ "query": "test Ab",
+ "max_expansions": 256
+ }
}
});
@@ -111,6 +98,7 @@ fn test_generate_terms_must_query() -> anyhow::Result<()> {
"match_phrase_prefix": {
"test": {
"query": "test",
+ "max_expansions": 256
}
}
},
@@ -118,6 +106,7 @@ fn test_generate_terms_must_query() -> anyhow::Result<()> {
"match_phrase_prefix": {
"test": {
"query": "test2",
+ "max_expansions": 256
}
}
}