Skip to content

Commit

Permalink
Fix/function score (#260)
Browse files Browse the repository at this point in the history
* fix: function score with no query serialisation

closes #257

* fix: fix field value factor serialization format
  • Loading branch information
oknozor authored Nov 21, 2023
1 parent cb62837 commit ace227b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/search/queries/compound/function_score_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl FunctionScoreQuery {

impl ShouldSkip for FunctionScoreQuery {
fn should_skip(&self) -> bool {
self.query.should_skip() || self.functions.should_skip()
self.functions.should_skip()
}
}

Expand Down Expand Up @@ -292,4 +292,45 @@ mod tests {
.filter(Query::term("type", "street")),
);
}

#[test]
fn should_not_skip_serializing_function_score_with_empty_query_gh_257() {
assert_serialize(
Query::bool().should(
Query::function_score()
.function(
Function::field_value_factor("weight")
.factor(10.0)
.missing(0.0)
.modifier(FieldValueFactorModifier::Log1P)
.weight(0.3),
)
.score_mode(FunctionScoreMode::Max)
.boost_mode(FunctionBoostMode::Replace),
),
json!( {
"bool": {
"should": [
{
"function_score": {
"boost_mode": "replace",
"functions": [
{
"field_value_factor": {
"factor": 10.0,
"field": "weight",
"missing": 0.0,
"modifier": "log1p"
},
"weight": 0.3
}
],
"score_mode": "max"
}
}
]
}
}),
)
}
}
5 changes: 3 additions & 2 deletions src/search/queries/params/function_score_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ impl FieldValueFactor {
///
/// Defaults to [none](FieldValueFactorModifier::None)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
#[serde(rename_all = "lowercase")]
pub enum FieldValueFactorModifier {
/// Do not apply any multiplier to the field value
None,
Expand Down Expand Up @@ -475,6 +475,7 @@ pub struct Decay<T: Origin> {

weight: Option<f32>,
}

#[derive(Debug, Clone, PartialEq)]
struct DecayFieldInner<T: Origin> {
field: String,
Expand Down Expand Up @@ -689,6 +690,6 @@ mod tests {
}
}
}),
)
);
}
}

0 comments on commit ace227b

Please sign in to comment.