Skip to content

Commit e415ddf

Browse files
authored
fix: the FTS deserialization is broken (#3708)
Signed-off-by: BubbleCal <bubble-cal@outlook.com>
1 parent 9dde5ea commit e415ddf

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

  • rust/lance-index/src/scalar/inverted

rust/lance-index/src/scalar/inverted/query.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ pub enum Operator {
4545
Or,
4646
}
4747

48+
impl Default for Operator {
49+
fn default() -> Self {
50+
Self::Or
51+
}
52+
}
53+
4854
impl TryFrom<&str> for Operator {
4955
type Error = Error;
5056
fn try_from(value: &str) -> Result<Self> {
@@ -187,6 +193,9 @@ pub struct MatchQuery {
187193
// If None, it will be determined at query time.
188194
pub column: Option<String>,
189195
pub terms: String,
196+
197+
// literal default is not supported so we set it by function
198+
#[serde(default = "MatchQuery::default_boost")]
190199
pub boost: f32,
191200

192201
// The max edit distance for fuzzy matching.
@@ -199,12 +208,14 @@ pub struct MatchQuery {
199208

200209
/// The maximum number of terms to expand for fuzzy matching.
201210
/// Default to 50.
211+
#[serde(default = "MatchQuery::default_max_expansions")]
202212
pub max_expansions: usize,
203213

204214
/// The operator to use for combining terms.
205215
/// This can be either `And` or `Or`, it's 'Or' by default.
206216
/// - `And`: All terms must match.
207217
/// - `Or`: At least one term must match.
218+
#[serde(default)]
208219
pub operator: Operator,
209220
}
210221

@@ -220,6 +231,14 @@ impl MatchQuery {
220231
}
221232
}
222233

234+
fn default_boost() -> f32 {
235+
1.0
236+
}
237+
238+
fn default_max_expansions() -> usize {
239+
50
240+
}
241+
223242
pub fn with_column(mut self, column: Option<String>) -> Self {
224243
self.column = column;
225244
self
@@ -300,6 +319,7 @@ impl FtsQueryNode for PhraseQuery {
300319
pub struct BoostQuery {
301320
pub positive: Box<FtsQuery>,
302321
pub negative: Box<FtsQuery>,
322+
#[serde(default = "BoostQuery::default_negative_boost")]
303323
pub negative_boost: f32,
304324
}
305325

@@ -311,6 +331,10 @@ impl BoostQuery {
311331
negative_boost: negative_boost.unwrap_or(0.5),
312332
}
313333
}
334+
335+
fn default_negative_boost() -> f32 {
336+
0.5
337+
}
314338
}
315339

316340
impl FtsQueryNode for BoostQuery {
@@ -521,3 +545,43 @@ pub fn fill_fts_query_column(
521545
}
522546
}
523547
}
548+
549+
#[cfg(test)]
550+
mod tests {
551+
#[test]
552+
fn test_match_query_serde() {
553+
use super::*;
554+
use serde_json::json;
555+
556+
let query = MatchQuery::new("hello world".to_string())
557+
.with_column(Some("text".to_string()))
558+
.with_boost(2.0)
559+
.with_fuzziness(Some(1))
560+
.with_max_expansions(10)
561+
.with_operator(Operator::And);
562+
563+
let serialized = serde_json::to_value(&query).unwrap();
564+
let expected = json!({
565+
"column": "text",
566+
"terms": "hello world",
567+
"boost": 2.0,
568+
"fuzziness": 1,
569+
"max_expansions": 10,
570+
"operator": "And"
571+
});
572+
assert_eq!(serialized, expected);
573+
574+
let expected = json!({
575+
"column": "text",
576+
"terms": "hello world",
577+
"fuzziness": 0,
578+
});
579+
let query = serde_json::from_str::<MatchQuery>(&expected.to_string()).unwrap();
580+
assert_eq!(query.column, Some("text".to_owned()));
581+
assert_eq!(query.terms, "hello world");
582+
assert_eq!(query.boost, 1.0);
583+
assert_eq!(query.fuzziness, Some(0));
584+
assert_eq!(query.max_expansions, 50);
585+
assert_eq!(query.operator, Operator::Or);
586+
}
587+
}

0 commit comments

Comments
 (0)