Skip to content

Commit 8e57512

Browse files
committed
cleanup: return 0 results when there are matching groups for a given set of tracking_id filters instead of erroring
1 parent 79d093d commit 8e57512

File tree

2 files changed

+74
-65
lines changed

2 files changed

+74
-65
lines changed

server/src/data/models.rs

+50-43
Original file line numberDiff line numberDiff line change
@@ -4411,56 +4411,63 @@ impl FieldCondition {
44114411
}
44124412

44134413
if let Some(match_any) = &self.match_any {
4414-
match match_any.first().ok_or(ServiceError::BadRequest(
4415-
"Should have at least one value for match".to_string(),
4416-
))? {
4417-
MatchCondition::Text(_) => Ok(Some(qdrant::Condition::matches(
4418-
self.field.as_str(),
4419-
match_any.iter().map(|x| x.to_string()).collect_vec(),
4420-
))),
4421-
MatchCondition::Integer(_) | MatchCondition::Float(_) => {
4422-
Ok(Some(qdrant::Condition::matches(
4414+
if let Some(first_match_any) = match_any.first() {
4415+
match first_match_any {
4416+
MatchCondition::Text(_) => Ok(Some(qdrant::Condition::matches(
44234417
self.field.as_str(),
4424-
match_any
4425-
.iter()
4426-
.map(|x: &MatchCondition| x.to_i64())
4427-
.collect_vec(),
4428-
)))
4418+
match_any.iter().map(|x| x.to_string()).collect_vec(),
4419+
))),
4420+
MatchCondition::Integer(_) | MatchCondition::Float(_) => {
4421+
Ok(Some(qdrant::Condition::matches(
4422+
self.field.as_str(),
4423+
match_any
4424+
.iter()
4425+
.map(|x: &MatchCondition| x.to_i64())
4426+
.collect_vec(),
4427+
)))
4428+
}
44294429
}
4430+
} else {
4431+
Ok(None)
44304432
}
44314433
} else if let Some(match_all) = &self.match_all {
4432-
match match_all.first().ok_or(ServiceError::BadRequest(
4433-
"Should have at least one value for match".to_string(),
4434-
))? {
4435-
MatchCondition::Text(_) => Ok(Some(
4436-
qdrant::Filter::must(
4437-
match_all
4438-
.iter()
4439-
.map(|cond| {
4440-
qdrant::Condition::matches(
4441-
self.field.as_str(),
4442-
vec![cond.to_string()],
4443-
)
4444-
})
4445-
.collect_vec(),
4446-
)
4447-
.into(),
4448-
)),
4449-
MatchCondition::Integer(_) | MatchCondition::Float(_) => Ok(Some(
4450-
qdrant::Filter::must(
4451-
match_all
4452-
.iter()
4453-
.map(|cond| {
4454-
qdrant::Condition::matches(self.field.as_str(), vec![cond.to_i64()])
4455-
})
4456-
.collect_vec(),
4457-
)
4458-
.into(),
4459-
)),
4434+
if let Some(first_match_all) = match_all.first() {
4435+
match first_match_all {
4436+
MatchCondition::Text(_) => Ok(Some(
4437+
qdrant::Filter::must(
4438+
match_all
4439+
.iter()
4440+
.map(|cond| {
4441+
qdrant::Condition::matches(
4442+
self.field.as_str(),
4443+
vec![cond.to_string()],
4444+
)
4445+
})
4446+
.collect_vec(),
4447+
)
4448+
.into(),
4449+
)),
4450+
MatchCondition::Integer(_) | MatchCondition::Float(_) => Ok(Some(
4451+
qdrant::Filter::must(
4452+
match_all
4453+
.iter()
4454+
.map(|cond| {
4455+
qdrant::Condition::matches(
4456+
self.field.as_str(),
4457+
vec![cond.to_i64()],
4458+
)
4459+
})
4460+
.collect_vec(),
4461+
)
4462+
.into(),
4463+
)),
4464+
}
4465+
} else {
4466+
Ok(None)
44604467
}
44614468
} else {
44624469
Err(ServiceError::BadRequest(
4463-
"No filter condition provided".to_string(),
4470+
"No filter condition provided. Field must not be null and date_range, range, geo_bounding_box, geo_radius, geo_polygon, match_any, or match_all must be populated.".to_string(),
44644471
))
44654472
}
44664473
}

server/src/operators/search_operator.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ async fn convert_group_tracking_ids_to_group_ids(
106106
.map(|item| item.to_string())
107107
.collect::<Vec<String>>();
108108

109-
let correct_matches: Vec<MatchCondition> =
109+
let mut correct_matches: Vec<MatchCondition> =
110110
get_group_ids_from_tracking_ids_query(matches, dataset_id, pool.clone())
111111
.await?
112112
.iter()
113113
.map(|(id, _)| MatchCondition::Text(id.to_string()))
114114
.collect();
115+
if correct_matches.is_empty() {
116+
correct_matches.push(MatchCondition::Text(uuid::Uuid::default().to_string()));
117+
}
115118

116119
Ok(FieldCondition {
117120
field: "group_ids".to_string(),
@@ -128,13 +131,17 @@ async fn convert_group_tracking_ids_to_group_ids(
128131
.iter()
129132
.map(|item| item.to_string())
130133
.collect::<Vec<String>>();
134+
let matches_len = matches.len();
131135

132-
let correct_matches: Vec<MatchCondition> =
136+
let mut correct_matches: Vec<MatchCondition> =
133137
get_group_ids_from_tracking_ids_query(matches, dataset_id, pool.clone())
134138
.await?
135139
.iter()
136140
.map(|(id, _)| MatchCondition::Text(id.to_string()))
137141
.collect();
142+
if matches_len != correct_matches.len() {
143+
correct_matches.push(MatchCondition::Text(uuid::Uuid::default().to_string()));
144+
}
138145

139146
Ok(FieldCondition {
140147
field: "group_ids".to_string(),
@@ -148,7 +155,8 @@ async fn convert_group_tracking_ids_to_group_ids(
148155
})
149156
} else {
150157
Err(ServiceError::BadRequest(
151-
"match_any key not found for group_tracking_ids".to_string(),
158+
"group_tracking_ids filter can only be used with match_all or match_any clauses"
159+
.to_string(),
152160
))?
153161
}
154162
} else {
@@ -2819,31 +2827,25 @@ async fn cross_encoder_for_groups(
28192827
) -> Result<Vec<GroupScoreChunk>, actix_web::Error> {
28202828
let score_chunks = groups_chunks
28212829
.iter()
2822-
.map(|group| {
2823-
group
2824-
.metadata
2825-
.clone()
2826-
.get(0)
2827-
.expect("Group should have at least one chunk")
2828-
.clone()
2829-
})
2830+
.filter_map(|group| group.metadata.clone().get(0).cloned().clone())
28302831
.collect_vec();
28312832

28322833
let cross_encoder_results = cross_encoder(query, page_size, score_chunks, config).await?;
28332834
let mut group_results = cross_encoder_results
28342835
.into_iter()
2835-
.map(|score_chunk| {
2836-
let mut group = groups_chunks
2837-
.iter()
2838-
.find(|group| {
2839-
group.metadata.iter().any(|chunk| {
2840-
chunk.metadata[0].metadata().id == score_chunk.metadata[0].metadata().id
2841-
})
2836+
.filter_map(|score_chunk| {
2837+
match groups_chunks.iter().find(|group| {
2838+
group.metadata.iter().any(|chunk| {
2839+
chunk.metadata[0].metadata().id == score_chunk.metadata[0].metadata().id
28422840
})
2843-
.expect("Group not found")
2844-
.clone();
2845-
group.metadata[0].score = score_chunk.score;
2846-
group
2841+
}) {
2842+
Some(group) => {
2843+
let mut group = group.clone();
2844+
group.metadata[0].score = score_chunk.score;
2845+
Some(group)
2846+
}
2847+
None => None,
2848+
}
28472849
})
28482850
.collect_vec();
28492851

0 commit comments

Comments
 (0)