Skip to content

Instantly share code, notes, and snippets.

@skeptrunedev
Created December 1, 2023 17:23
Show Gist options
  • Save skeptrunedev/3ede217aa78d6462c5c52c63d0318764 to your computer and use it in GitHub Desktop.
Save skeptrunedev/3ede217aa78d6462c5c52c63d0318764 to your computer and use it in GitHub Desktop.
Arguflow Qdrant Filter Function Rust
pub fn assemble_qdrant_filter(
current_user_id: Option<uuid::Uuid>,
tag_set: Option<Vec<String>>,
link: Option<Vec<String>>,
time_range: Option<(String, String)>,
filters: Option<serde_json::Value>,
quote_words: Option<Vec<String>>,
negated_words: Option<Vec<String>>,
) -> Filter {
let mut filter = Filter::default();
filter.should.push(Condition::is_empty("private"));
filter.should.push(Condition::is_null("private"));
filter.should.push(Condition::matches("private", false));
filter.should.push(Condition::matches(
"authors",
current_user_id.unwrap_or_default().to_string(),
));
let tag_set_inner = tag_set.unwrap_or_default();
let link_inner = link.unwrap_or_default();
if !tag_set_inner.is_empty() {
filter
.must
.push(Condition::matches("tag_set", tag_set_inner));
}
if !link_inner.is_empty() {
filter.must.push(Condition::matches("link", link_inner));
}
if let Some(time_range) = time_range {
if time_range.0 != "null" && time_range.1 != "null" {
filter.must.push(Condition::range(
"time_stamp",
Range {
gt: None,
lt: None,
gte: Some(
NaiveDateTime::parse_from_str(&time_range.0, "%Y-%m-%d %H:%M:%S")
.map_err(|_| DefaultError {
message: "Failed to parse time range",
})?
.timestamp() as f64,
),
lte: Some(
NaiveDateTime::parse_from_str(&time_range.1, "%Y-%m-%d %H:%M:%S")
.map_err(|_| DefaultError {
message: "Failed to parse time range",
})?
.timestamp() as f64,
),
},
));
} else if time_range.1 == "null" {
filter.must.push(Condition::range(
"time_stamp",
Range {
gt: None,
lt: None,
gte: Some(
NaiveDateTime::parse_from_str(&time_range.0, "%Y-%m-%d %H:%M:%S")
.map_err(|_| DefaultError {
message: "Failed to parse time range",
})?
.timestamp() as f64,
),
lte: None,
},
));
} else if time_range.0 == "null" {
filter.must.push(Condition::range(
"time_stamp",
Range {
gt: None,
lt: None,
gte: None,
lte: Some(
NaiveDateTime::parse_from_str(&time_range.1, "%Y-%m-%d %H:%M:%S")
.map_err(|_| DefaultError {
message: "Failed to parse time range",
})?
.timestamp() as f64,
),
},
));
}
}
if let Some(serde_json::Value::Object(obj)) = &filters {
for key in obj.keys() {
let value = obj.get(key).expect("Value should exist");
match value {
serde_json::Value::Array(arr) => {
filter.must.push(Condition::matches(
&format!("metadata.{}", key),
arr.iter()
.map(|item| item.to_string())
.collect::<Vec<String>>(),
));
}
_ => {
filter.must.push(Condition::matches(
&format!("metadata.{}", key),
value.to_string().replace('\"', ""),
));
}
}
}
}
if let Some(quote_words) = quote_words {
for word in quote_words.iter() {
filter
.must
.push(Condition::matches("card_html", word.clone() + " "));
}
}
if let Some(negated_words) = negated_words {
for word in negated_words.iter() {
filter
.must_not
.push(Condition::matches("card_html", word.clone() + " "));
}
}
filter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment