os_query_builder_rs/full_text/
match_phrase_prefix.rs

1use serde::ser::{Serialize, SerializeMap, Serializer};
2use serde_json::Value;
3
4#[derive(Debug, Default, Clone)]
5pub struct MatchPhrasePrefix {
6    field: Option<String>,
7    value: MatchPhrasePrefixValues,
8}
9
10impl MatchPhrasePrefix {
11    pub fn new() -> Self {
12        Self::default()
13    }
14
15    pub fn field<T: Into<String>>(self, field: T) -> Self {
16        Self {
17            field: Some(field.into()),
18            ..self
19        }
20    }
21
22    pub fn value<T: Into<Value>>(self, val: T) -> Self {
23        let value = MatchPhrasePrefixValues {
24            query: Some(val.into()),
25            ..self.value
26        };
27        Self { value, ..self }
28    }
29
30    pub fn query<T: Into<Value>>(self, value: T) -> Self {
31        self.value(value)
32    }
33
34    pub fn slop<T: Into<u64>>(self, slop: T) -> Self {
35        let value = MatchPhrasePrefixValues {
36            slop: Some(slop.into()),
37            ..self.value
38        };
39        Self { value, ..self }
40    }
41
42    pub fn analyzer<T: Into<String>>(self, analyzer: T) -> Self {
43        let value = MatchPhrasePrefixValues {
44            analyzer: Some(analyzer.into()),
45            ..self.value
46        };
47        Self { value, ..self }
48    }
49
50    pub fn max_expansions<T: Into<u64>>(self, max_expansions: T) -> Self {
51        let value = MatchPhrasePrefixValues {
52            max_expansions: Some(max_expansions.into()),
53            ..self.value
54        };
55        Self { value, ..self }
56    }
57}
58
59#[derive(Debug, Default, Clone, serde::Serialize)]
60struct MatchPhrasePrefixValues {
61    query: Option<Value>,
62
63    #[serde(skip_serializing_if = "Option::is_none")]
64    slop: Option<u64>,
65
66    #[serde(skip_serializing_if = "Option::is_none")]
67    analyzer: Option<String>,
68
69    #[serde(skip_serializing_if = "Option::is_none")]
70    max_expansions: Option<u64>,
71}
72
73impl Serialize for MatchPhrasePrefix {
74    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
75        where
76            S: Serializer,
77    {
78        let mut state = serializer.serialize_map(Some(1))?;
79        state.serialize_entry(self.field.as_deref().unwrap_or_default(), &self.value)?;
80        state.end()
81    }
82}