Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(globalsearch): Implement tag-based filters in global search #5151

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/analytics/src/opensearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub struct OpenSearchQueryBuilder {
pub query: String,
pub offset: Option<i64>,
pub count: Option<i64>,
pub filters: Vec<(String, String)>,
pub filters: Vec<(String, Vec<String>)>,
}

impl OpenSearchQueryBuilder {
Expand All @@ -391,7 +391,7 @@ impl OpenSearchQueryBuilder {
Ok(())
}

pub fn add_filter_clause(&mut self, lhs: String, rhs: String) -> QueryResult<()> {
pub fn add_filter_clause(&mut self, lhs: String, rhs: Vec<String>) -> QueryResult<()> {
self.filters.push((lhs, rhs));
Ok(())
}
Expand All @@ -403,7 +403,7 @@ impl OpenSearchQueryBuilder {
let mut filters = self
.filters
.iter()
.map(|(k, v)| json!({"match_phrase" : {k : v}}))
.map(|(k, v)| json!({"terms" : {k : v}}))
.collect::<Vec<Value>>();

query.append(&mut filters);
Expand Down
55 changes: 53 additions & 2 deletions crates/analytics/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,35 @@ pub async fn msearch_results(
OpenSearchQueryBuilder::new(OpenSearchQuery::Msearch(indexes.clone()), req.query);

query_builder
.add_filter_clause("merchant_id".to_string(), merchant_id.to_string())
.add_filter_clause(
"merchant_id.keyword".to_string(),
vec![merchant_id.to_string()],
)
.switch()?;

if let Some(filters) = req.filters {
if let Some(currency) = filters.currency {
query_builder
.add_filter_clause("currency.keyword".to_string(), currency)
.switch()?;
};
if let Some(status) = filters.status {
query_builder
.add_filter_clause("status.keyword".to_string(), status)
.switch()?;
};
if let Some(payment_method) = filters.payment_method {
query_builder
.add_filter_clause("payment_method.keyword".to_string(), payment_method)
.switch()?;
};
if let Some(customer_email) = filters.customer_email {
query_builder
.add_filter_clause("customer_email.keyword".to_string(), customer_email)
.switch()?;
};
};

let response_text: OpenMsearchOutput = client
.execute(query_builder)
.await
Expand Down Expand Up @@ -82,9 +108,34 @@ pub async fn search_results(
OpenSearchQueryBuilder::new(OpenSearchQuery::Search(req.index), search_req.query);

query_builder
.add_filter_clause("merchant_id".to_string(), merchant_id.to_string())
.add_filter_clause(
"merchant_id.keyword".to_string(),
vec![merchant_id.to_string()],
)
.switch()?;

if let Some(filters) = search_req.filters {
if let Some(currency) = filters.currency {
query_builder
.add_filter_clause("currency.keyword".to_string(), currency)
.switch()?;
};
if let Some(status) = filters.status {
query_builder
.add_filter_clause("status.keyword".to_string(), status)
.switch()?;
};
if let Some(payment_method) = filters.payment_method {
query_builder
.add_filter_clause("payment_method.keyword".to_string(), payment_method)
.switch()?;
};
if let Some(customer_email) = filters.customer_email {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Some(customer_email) = filters.customer_email {
if let Some(customer_email) = filters.customer_email && !customer_email.is_empty() {

can we add this sort of validation to make sure that empty arrays are ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, added the validations to ignore empty arrays

query_builder
.add_filter_clause("customer_email.keyword".to_string(), customer_email)
.switch()?;
};
};
query_builder
.set_offset_n_count(search_req.offset, search_req.count)
.switch()?;
Expand Down
3 changes: 3 additions & 0 deletions crates/api_models/src/analytics/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use serde_json::Value;
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct SearchFilters {
pub payment_method: Option<Vec<String>>,
pub currency: Option<Vec<String>>,
pub status: Option<Vec<String>>,
pub customer_email: Option<Vec<String>>,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
Expand Down
Loading