elasticsearch_dsl/search/queries/full_text/
match_bool_prefix_query.rs

1use crate::search::*;
2use crate::util::*;
3
4/// A `match_bool_prefix` query analyzes its input and constructs a
5/// [`bool` query](crate::BoolQuery) from the terms. Each term except the last is used in a
6/// [`term` query](crate::TermQuery). The last term is used in a
7/// [`prefix` query](crate::PrefixQuery).
8///
9/// To create a MatchBoolPrefix query:
10/// ```
11/// # use elasticsearch_dsl::queries::*;
12/// # use elasticsearch_dsl::queries::params::*;
13/// # let query =
14/// Query::match_bool_prefix("test", "search text")
15///     .boost(2)
16///     .name("test");
17/// ```
18/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-bool-prefix-query.html>
19#[derive(Debug, Clone, PartialEq, Serialize)]
20#[serde(remote = "Self")]
21pub struct MatchBoolPrefixQuery {
22    #[serde(skip)]
23    field: String,
24
25    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
26    query: Text,
27
28    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
29    analyzer: Option<String>,
30
31    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
32    minimum_should_match: Option<String>,
33
34    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
35    operator: Option<Operator>,
36
37    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
38    boost: Option<f32>,
39
40    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
41    _name: Option<String>,
42}
43
44impl Query {
45    /// Creates an instance of [`MatchBoolPrefixQuery`]
46    ///
47    /// - `field` - Field you wish to search.
48    /// - `query` - Text, number, boolean value or date you wish to find in the provided `<field>`
49    pub fn match_bool_prefix<T, U>(field: T, query: U) -> MatchBoolPrefixQuery
50    where
51        T: ToString,
52        U: Into<Text>,
53    {
54        MatchBoolPrefixQuery {
55            field: field.to_string(),
56            query: query.into(),
57            analyzer: None,
58            minimum_should_match: None,
59            operator: None,
60            boost: None,
61            _name: None,
62        }
63    }
64}
65
66impl MatchBoolPrefixQuery {
67    /// [Analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)
68    /// used to convert the text in the `query` value into tokens. Defaults to the
69    /// [index-time analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/specify-analyzer.html#specify-index-time-analyzer)
70    /// mapped for the `<field>`. If no analyzer is mapped, the index’s default analyzer is used.
71    pub fn analyzer<T>(mut self, analyzer: T) -> Self
72    where
73        T: ToString,
74    {
75        self.analyzer = Some(analyzer.to_string());
76        self
77    }
78
79    /// Minimum number of clauses that must match for a document to be returned. See the
80    /// `minimum_should_match` parameter for valid values and more information.
81    pub fn minimum_should_match<T>(mut self, minimum_should_match: T) -> Self
82    where
83        T: ToString,
84    {
85        self.minimum_should_match = Some(minimum_should_match.to_string());
86        self
87    }
88
89    /// Boolean logic used to interpret text in the `query` value
90    pub fn operator(mut self, operator: Operator) -> Self {
91        self.operator = Some(operator);
92        self
93    }
94
95    add_boost_and_name!();
96}
97
98impl ShouldSkip for MatchBoolPrefixQuery {
99    fn should_skip(&self) -> bool {
100        self.query.should_skip()
101    }
102}
103
104serialize_with_root_keyed!("match_bool_prefix": MatchBoolPrefixQuery);
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn serialization() {
112        assert_serialize_query(
113            Query::match_bool_prefix("test", "search text"),
114            json!({
115                "match_bool_prefix": {
116                    "test": {
117                        "query": "search text"
118                    }
119                }
120            }),
121        );
122
123        assert_serialize_query(
124            Query::match_bool_prefix("test", "search text")
125                .analyzer("search_time_analyzer")
126                .minimum_should_match("12")
127                .operator(Operator::Or)
128                .boost(2)
129                .name("test"),
130            json!({
131                "match_bool_prefix": {
132                    "test": {
133                        "query": "search text",
134                        "analyzer": "search_time_analyzer",
135                        "minimum_should_match": "12",
136                        "operator": "OR",
137                        "boost": 2.0,
138                        "_name": "test"
139                    }
140                }
141            }),
142        );
143    }
144}