use serde::Serialize;
use crate::{search::*, util::*};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct RankFeatureQuery {
field: String,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct RankFeatureSaturationQuery {
field: String,
saturation: Saturation,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct RankFeatureLogarithmQuery {
field: String,
log: Logarithm,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct RankFeatureSigmoidQuery {
field: String,
sigmoid: Sigmoid,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Self")]
pub struct RankFeatureLinearQuery {
field: String,
linear: Linear,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Saturation {
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
pivot: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Logarithm {
scaling_factor: f64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Sigmoid {
pivot: f64,
exponent: f64,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Linear {}
impl Query {
pub fn rank_feature<T>(field: T) -> RankFeatureQuery
where
T: ToString,
{
RankFeatureQuery {
field: field.to_string(),
boost: None,
_name: None,
}
}
}
impl RankFeatureQuery {
add_boost_and_name!();
pub fn saturation(self) -> RankFeatureSaturationQuery {
RankFeatureSaturationQuery {
field: self.field,
boost: self.boost,
_name: self._name,
saturation: Saturation { pivot: None },
}
}
pub fn logarithm(self, scaling_factor: f64) -> RankFeatureLogarithmQuery {
RankFeatureLogarithmQuery {
field: self.field,
boost: self.boost,
_name: self._name,
log: Logarithm { scaling_factor },
}
}
pub fn sigmoid(self, pivot: f64, exponent: f64) -> RankFeatureSigmoidQuery {
RankFeatureSigmoidQuery {
field: self.field,
boost: self.boost,
_name: self._name,
sigmoid: Sigmoid { pivot, exponent },
}
}
pub fn linear(self) -> RankFeatureLinearQuery {
RankFeatureLinearQuery {
field: self.field,
boost: self.boost,
_name: self._name,
linear: Linear {},
}
}
}
impl RankFeatureSaturationQuery {
add_boost_and_name!();
pub fn pivot<T>(mut self, pivot: T) -> Self
where
T: Into<f64>,
{
self.saturation.pivot = Some(pivot.into());
self
}
}
impl RankFeatureLogarithmQuery {
add_boost_and_name!();
}
impl RankFeatureSigmoidQuery {
add_boost_and_name!();
}
impl RankFeatureLinearQuery {
add_boost_and_name!();
}
impl ShouldSkip for RankFeatureQuery {}
impl ShouldSkip for RankFeatureSaturationQuery {}
impl ShouldSkip for RankFeatureLogarithmQuery {}
impl ShouldSkip for RankFeatureSigmoidQuery {}
impl ShouldSkip for RankFeatureLinearQuery {}
serialize_with_root!("rank_feature": RankFeatureQuery);
serialize_with_root!("rank_feature": RankFeatureSaturationQuery);
serialize_with_root!("rank_feature": RankFeatureLogarithmQuery);
serialize_with_root!("rank_feature": RankFeatureSigmoidQuery);
serialize_with_root!("rank_feature": RankFeatureLinearQuery);
deserialize_with_root!("rank_feature": RankFeatureQuery);
deserialize_with_root!("rank_feature": RankFeatureSaturationQuery);
deserialize_with_root!("rank_feature": RankFeatureLogarithmQuery);
deserialize_with_root!("rank_feature": RankFeatureSigmoidQuery);
deserialize_with_root!("rank_feature": RankFeatureLinearQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::rank_feature("test"),
json!({
"rank_feature": {
"field": "test",
}
}),
);
assert_serialize_query(
Query::rank_feature("test").boost(2).name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
}
}),
);
assert_serialize_query(
Query::rank_feature("test")
.saturation()
.boost(2)
.name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
"saturation": {},
}
}),
);
assert_serialize_query(
Query::rank_feature("test")
.saturation()
.pivot(2.2)
.boost(2)
.name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
"saturation": {
"pivot": 2.2,
},
}
}),
);
assert_serialize_query(
Query::rank_feature("test")
.logarithm(2.2)
.boost(2)
.name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
"log": {
"scaling_factor": 2.2
},
}
}),
);
assert_serialize_query(
Query::rank_feature("test")
.sigmoid(2.2, 3.3)
.boost(2)
.name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
"sigmoid": {
"pivot": 2.2,
"exponent": 3.3,
},
}
}),
);
assert_serialize_query(
Query::rank_feature("test").linear().boost(2).name("query"),
json!({
"rank_feature": {
"field": "test",
"boost": 2.0,
"_name": "query",
"linear": {},
}
}),
);
}
}