From ace227bfa81b8135937c8d89e8741baf230075c1 Mon Sep 17 00:00:00 2001 From: Paul Delafosse Date: Tue, 21 Nov 2023 08:35:03 +0100 Subject: [PATCH] Fix/function score (#260) * fix: function score with no query serialisation closes #257 * fix: fix field value factor serialization format --- .../queries/compound/function_score_query.rs | 43 ++++++++++++++++++- .../queries/params/function_score_query.rs | 5 ++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/search/queries/compound/function_score_query.rs b/src/search/queries/compound/function_score_query.rs index 7008f6c..956f215 100644 --- a/src/search/queries/compound/function_score_query.rs +++ b/src/search/queries/compound/function_score_query.rs @@ -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() } } @@ -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" + } + } + ] + } + }), + ) + } } diff --git a/src/search/queries/params/function_score_query.rs b/src/search/queries/params/function_score_query.rs index 14b75ad..63e849b 100644 --- a/src/search/queries/params/function_score_query.rs +++ b/src/search/queries/params/function_score_query.rs @@ -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, @@ -475,6 +475,7 @@ pub struct Decay { weight: Option, } + #[derive(Debug, Clone, PartialEq)] struct DecayFieldInner { field: String, @@ -689,6 +690,6 @@ mod tests { } } }), - ) + ); } }