os_query_builder_rs/full_text/
match_phrase.rs

1use serde::ser::{Serialize, SerializeMap, Serializer};
2use serde_json::Value;
3
4#[derive(Debug, Default, Clone)]
5pub struct MatchPhrase {
6    field: Option<String>,
7    value: MatchPhraseValues,
8}
9
10impl MatchPhrase {
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 = MatchPhraseValues {
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 = MatchPhraseValues {
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 = MatchPhraseValues {
44            analyzer: Some(analyzer.into()),
45            ..self.value
46        };
47        Self { value, ..self }
48    }
49
50    pub fn zero_terms_query<T: Into<String>>(self, zero_terms_query: T) -> Self {
51        let value = MatchPhraseValues {
52            zero_terms_query: Some(zero_terms_query.into()),
53            ..self.value
54        };
55        Self { value, ..self }
56    }
57}
58
59#[derive(Debug, Default, Clone, serde::Serialize)]
60struct MatchPhraseValues {
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    zero_terms_query: Option<String>,
71}
72
73impl Serialize for MatchPhrase {
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}