elasticsearch_dsl/search/queries/full_text/match_phrase_query.rs
1use crate::search::*;
2use crate::util::*;
3
4/// The `match_phrase` query analyzes the text and creates a phrase query out
5/// of the analyzed text.
6///
7/// To create a MatchPhrase query:
8/// ```
9/// # use elasticsearch_dsl::queries::*;
10/// # use elasticsearch_dsl::queries::params::*;
11/// # let query =
12/// Query::match_phrase("test", "search text")
13/// .boost(2)
14/// .name("test");
15/// ```
16/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html>
17#[derive(Debug, Clone, PartialEq, Serialize)]
18#[serde(remote = "Self")]
19pub struct MatchPhraseQuery {
20 #[serde(skip)]
21 field: String,
22
23 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
24 query: Text,
25
26 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
27 analyzer: Option<String>,
28
29 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
30 slop: Option<u8>,
31
32 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
33 boost: Option<f32>,
34
35 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
36 _name: Option<String>,
37}
38
39impl Query {
40 /// Creates an instance of [`MatchPhraseQuery`]
41 ///
42 /// - `field` - Field you wish to search.
43 /// - `query` - Text, number, boolean value or date you wish to find in the provided
44 /// `<field>`.<br/>
45 /// The `match_phrase` query
46 /// [analyzes](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)
47 /// any provided text before performing a search. This means the
48 /// `match_phrase` query can search
49 /// [`text`](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html)
50 /// fields for analyzed tokens rather than an exact term.
51 pub fn match_phrase<T, U>(field: T, query: U) -> MatchPhraseQuery
52 where
53 T: ToString,
54 U: Into<Text>,
55 {
56 MatchPhraseQuery {
57 field: field.to_string(),
58 query: query.into(),
59 analyzer: None,
60 slop: None,
61 boost: None,
62 _name: None,
63 }
64 }
65}
66
67impl MatchPhraseQuery {
68 /// [Analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)
69 /// used to convert the text in the `query` value into tokens. Defaults to the
70 /// [index-time analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/specify-analyzer.html#specify-index-time-analyzer)
71 /// mapped for the `<field>`. If no analyzer is mapped, the index’s default analyzer is used.
72 pub fn analyzer<T>(mut self, analyzer: T) -> Self
73 where
74 T: ToString,
75 {
76 self.analyzer = Some(analyzer.to_string());
77 self
78 }
79
80 /// The maximum number of intervening unmatched positions, as well as
81 /// whether matches are required to be in-order.
82 pub fn slop(mut self, slop: u8) -> Self {
83 self.slop = Some(slop);
84 self
85 }
86
87 add_boost_and_name!();
88}
89
90impl ShouldSkip for MatchPhraseQuery {
91 fn should_skip(&self) -> bool {
92 self.query.should_skip()
93 }
94}
95
96serialize_with_root_keyed!("match_phrase": MatchPhraseQuery);
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn serialization() {
104 assert_serialize_query(
105 Query::match_phrase("test", "search text"),
106 json!({
107 "match_phrase": {
108 "test": {
109 "query": "search text"
110 }
111 }
112 }),
113 );
114
115 assert_serialize_query(
116 Query::match_phrase("test", "search text")
117 .analyzer("search_time_analyzer")
118 .slop(1u8)
119 .boost(2)
120 .name("test"),
121 json!({
122 "match_phrase": {
123 "test": {
124 "query": "search text",
125 "analyzer": "search_time_analyzer",
126 "slop": 1,
127 "boost": 2.0,
128 "_name": "test"
129 }
130 }
131 }),
132 );
133 }
134}