elasticsearch_dsl/search/queries/full_text/
match_phrase_prefix_query.rs

1use crate::search::*;
2use crate::util::*;
3
4/// Returns documents that contain the words of a provided text, in the **same order** as provided.
5/// The last term of the provided text is treated as a [prefix](crate::PrefixQuery), matching any
6/// words that begin with that term.
7///
8/// To create a MatchPhrasePrefix query:
9/// ```
10/// # use elasticsearch_dsl::queries::*;
11/// # use elasticsearch_dsl::queries::params::*;
12/// # let query =
13/// Query::match_phrase_prefix("test", "search text")
14///     .boost(2)
15///     .name("test");
16/// ```
17/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html>
18#[derive(Debug, Clone, PartialEq, Serialize)]
19#[serde(remote = "Self")]
20pub struct MatchPhrasePrefixQuery {
21    #[serde(skip)]
22    field: String,
23
24    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
25    query: Text,
26
27    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
28    analyzer: Option<String>,
29
30    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
31    max_expansions: Option<u8>,
32
33    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
34    slop: Option<u8>,
35
36    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
37    zero_terms_query: Option<ZeroTermsQuery>,
38
39    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
40    boost: Option<f32>,
41
42    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
43    _name: Option<String>,
44}
45
46impl Query {
47    /// Creates an instance of [`MatchPhrasePrefixQuery`]
48    ///
49    /// - `field` - Field you wish to search.
50    /// - `query` - Text you wish to find in the provided `<field>`. <br/> The `match_phrase_prefix`
51    ///   query analyzes any provided text into tokens before performing a search. The last term of
52    ///   this text is treated as a [prefix](crate::PrefixQuery), matching any words that begin with
53    ///   that term.
54    pub fn match_phrase_prefix<T, U>(field: T, query: U) -> MatchPhrasePrefixQuery
55    where
56        T: ToString,
57        U: Into<Text>,
58    {
59        MatchPhrasePrefixQuery {
60            field: field.to_string(),
61            query: query.into(),
62            analyzer: None,
63            max_expansions: None,
64            slop: None,
65            zero_terms_query: None,
66            boost: None,
67            _name: None,
68        }
69    }
70}
71
72impl MatchPhrasePrefixQuery {
73    /// [Analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)
74    /// used to convert the text in the `query` value into tokens. Defaults to the
75    /// [index-time analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/specify-analyzer.html#specify-index-time-analyzer)
76    /// mapped for the `<field>`. If no analyzer is mapped, the index’s default analyzer is used.
77    pub fn analyzer<T>(mut self, analyzer: T) -> Self
78    where
79        T: ToString,
80    {
81        self.analyzer = Some(analyzer.to_string());
82        self
83    }
84
85    /// Maximum number of terms to which the query will expand.
86    /// Defaults to `50`.
87    pub fn max_expansions(mut self, max_expansions: u8) -> Self {
88        self.max_expansions = Some(max_expansions);
89        self
90    }
91
92    /// The maximum number of intervening unmatched positions, as well as
93    /// whether matches are required to be in-order.
94    pub fn slop(mut self, slop: u8) -> Self {
95        self.slop = Some(slop);
96        self
97    }
98
99    /// Indicates whether no documents are returned if the `analyzer` removes
100    /// all tokens, such as when using a `stop` filter.
101    pub fn zero_terms_query(mut self, zero_terms_query: ZeroTermsQuery) -> Self {
102        self.zero_terms_query = Some(zero_terms_query);
103        self
104    }
105
106    add_boost_and_name!();
107}
108
109impl ShouldSkip for MatchPhrasePrefixQuery {
110    fn should_skip(&self) -> bool {
111        self.query.should_skip()
112    }
113}
114
115serialize_with_root_keyed!("match_phrase_prefix": MatchPhrasePrefixQuery);
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn serialization() {
123        assert_serialize_query(
124            Query::match_phrase_prefix("test", "search text"),
125            json!({
126                "match_phrase_prefix": {
127                    "test": {
128                        "query": "search text"
129                    }
130                }
131            }),
132        );
133
134        assert_serialize_query(
135            Query::match_phrase_prefix("test", "search text")
136                .analyzer("search_time_analyzer")
137                .max_expansions(20)
138                .slop(5)
139                .zero_terms_query(ZeroTermsQuery::None)
140                .boost(2)
141                .name("test"),
142            json!({
143                "match_phrase_prefix": {
144                    "test": {
145                        "query": "search text",
146                        "analyzer": "search_time_analyzer",
147                        "max_expansions": 20,
148                        "slop": 5,
149                        "zero_terms_query": "none",
150                        "boost": 2.0,
151                        "_name": "test"
152                    }
153                }
154            }),
155        );
156    }
157}