os_query_builder_rs/full_text/
match_boolean_prefix.rs

1use serde::{
2    Serialize,
3    Serializer,
4    ser::SerializeMap
5};
6use crate::misc::{
7    fuzziness::Fuzziness,
8    operator::Operator,
9    rewrite::Rewrite
10};
11
12#[derive(Debug, Default, Clone)]
13pub struct MatchBoolPrefix {
14    field: Option<String>,
15    value: MatchBoolPrefixValue
16}
17
18#[derive(Debug, Default, Clone, Serialize)]
19struct MatchBoolPrefixValue {
20    query: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    analyzer: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    fuzzy_rewrite: Option<Rewrite>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    fuzziness: Option<Fuzziness>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    fuzzy_transpositions: Option<bool>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    max_expansions: Option<u64>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    minimum_should_match: Option<String>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    operator: Option<Operator>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    prefix_length: Option<u64>
37}
38
39impl Serialize for MatchBoolPrefix {
40    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
41        where
42            S: Serializer,
43    {
44        let mut state = serializer.serialize_map(Some(1))?;
45        state.serialize_entry(self.field.as_deref().unwrap_or_default(), &self.value)?;
46        state.end()
47    }
48}
49
50
51impl MatchBoolPrefix {
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    pub fn field<T: Into<String>>(self, field: T) -> Self {
57        Self {
58            field: Some(field.into()),
59            ..self
60        }
61    }
62
63    pub fn query<T: Into<String>>(self, value: T) -> Self {
64        let value = MatchBoolPrefixValue {
65            query: value.into(),
66            ..self.value
67        };
68        Self { value, ..self }
69    }
70    pub fn fuzzy_rewrite<T: Into<Rewrite>>(self, fuzzy_rewrite: T) -> Self {
71        let value = MatchBoolPrefixValue {
72            fuzzy_rewrite: Some(fuzzy_rewrite.into()),
73            ..self.value
74        };
75        Self { value, ..self }
76    }
77
78
79    pub fn operator<T: Into<Operator>>(self, operator: T) -> Self {
80        let value = MatchBoolPrefixValue {
81            operator: Some(operator.into()),
82            ..self.value
83        };
84        Self { value, ..self }
85    }
86
87    pub fn analyzer<T: Into<String>>(self, analyzer: T) -> Self {
88        let value = MatchBoolPrefixValue {
89            analyzer: Some(analyzer.into()),
90            ..self.value
91        };
92        Self { value, ..self }
93    }
94
95
96    pub fn prefix_length<T: Into<u64>>(self, prefix_length: T) -> Self {
97        let value = MatchBoolPrefixValue {
98            prefix_length: Some(prefix_length.into()),
99            ..self.value
100        };
101        Self { value, ..self }
102    }
103
104    pub fn max_expansions<T: Into<u64>>(self, max_expansions: T) -> Self {
105        let value = MatchBoolPrefixValue {
106            max_expansions: Some(max_expansions.into()),
107            ..self.value
108        };
109        Self { value, ..self }
110    }
111
112    pub fn fuzziness<T: Into<Fuzziness>>(self, fuzziness: T) -> Self {
113        let value = MatchBoolPrefixValue {
114            fuzziness: Some(fuzziness.into()),
115            ..self.value
116        };
117        Self { value, ..self }
118    }
119
120    pub fn fuzzy_transpositions(self, fuzzy_transpositions: bool) -> Self {
121        let value = MatchBoolPrefixValue {
122            fuzzy_transpositions: Some(fuzzy_transpositions),
123            ..self.value
124        };
125        Self { value, ..self }
126    }
127
128    pub fn minimum_should_match<T: Into<String>>(self, minimum_should_match: T) -> Self {
129        let value = MatchBoolPrefixValue {
130            minimum_should_match: Some(minimum_should_match.into()),
131            ..self.value
132        };
133        Self { value, ..self }
134    }
135}