1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
use crate::search::*;
use crate::util::*;
use serde::Serialize;
/// Returns documents that contain terms similar to the search term, as measured by a
/// [Levenshtein edit distance](https://en.wikipedia.org/wiki/Levenshtein_distance).
///
/// An edit distance is the number of one-character changes needed to turn one term into another.
/// These changes can include:
///
/// - Changing a character (**b**ox → **f**ox)
/// - Removing a character (**b**lack → lack)
/// - Inserting a character (sic → sic**k**)
/// - Transposing two adjacent characters (**ac**t → **ca**t)
/// To find similar terms, the fuzzy query creates a set of all possible variations, or expansions, of the search term within a specified edit distance. The query then returns exact matches for each expansion.
///
/// To create a fuzzy query with numeric values:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::fuzzy("test", 123);
/// ```
/// To create a fuzzy query with string values and optional fields:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::fuzzy("test", "username")
/// .boost(2)
/// .name("test");
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct FuzzyQuery {
#[serde(skip)]
field: String,
value: Option<Term>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
fuzziness: Option<Fuzziness>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
max_expansions: Option<u8>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
prefix_length: Option<u8>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
transpositions: Option<bool>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
rewrite: Option<Rewrite>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
/// Creates an instance of [`FuzzyQuery`]
///
/// - `field` - Field you wish to search.
/// - `value` - Fuzzy you wish to find in the provided field.
pub fn fuzzy<T, U>(field: T, value: U) -> FuzzyQuery
where
T: ToString,
U: Serialize,
{
FuzzyQuery {
field: field.to_string(),
value: Term::new(value),
fuzziness: None,
max_expansions: None,
prefix_length: None,
transpositions: None,
rewrite: None,
boost: None,
_name: None,
}
}
}
impl FuzzyQuery {
/// Maximum edit distance allowed for matching.
/// See [Fuzziness](Fuzziness)
/// for valid values and more information. See
/// [Fuzziness in the match query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-fuzziness)
/// for an example.
pub fn fuzziness<T>(mut self, fuzziness: T) -> Self
where
T: Into<Fuzziness>,
{
self.fuzziness = Some(fuzziness.into());
self
}
/// Maximum number of terms to which the query will expand.
/// Defaults to `50`.
pub fn max_expansions(mut self, max_expansions: u8) -> Self {
self.max_expansions = Some(max_expansions);
self
}
/// Number of beginning characters left unchanged for fuzzy matching.
/// Defaults to `0`.
pub fn prefix_length(mut self, prefix_length: u8) -> Self {
self.prefix_length = Some(prefix_length);
self
}
/// Indicates whether edits include transpositions of two adjacent characters (ab → ba).
/// Defaults to `true`
pub fn transpositions(mut self, transpositions: bool) -> Self {
self.transpositions = Some(transpositions);
self
}
/// Method used to rewrite the query. For valid values and more information, see the
/// [rewrite](Rewrite) parameter.
pub fn rewrite(mut self, rewrite: Rewrite) -> Self {
self.rewrite = Some(rewrite);
self
}
add_boost_and_name!();
}
impl ShouldSkip for FuzzyQuery {
fn should_skip(&self) -> bool {
self.value.should_skip()
}
}
serialize_with_root_keyed!("fuzzy": FuzzyQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::fuzzy("test", 123),
json!({
"fuzzy": {
"test": {
"value": 123
}
}
}),
);
assert_serialize_query(
Query::fuzzy("test", 123)
.fuzziness(Fuzziness::Auto)
.max_expansions(3)
.prefix_length(4)
.transpositions(false)
.rewrite(Rewrite::ScoringBoolean)
.boost(2)
.name("test"),
json!({
"fuzzy": {
"test": {
"value": 123,
"fuzziness": "AUTO",
"max_expansions": 3,
"prefix_length": 4,
"transpositions": false,
"rewrite": "scoring_boolean",
"boost": 2.0,
"_name": "test"
}
}
}),
);
assert_serialize_query(
Query::bool().filter(Query::fuzzy("test", None::<String>)),
json!({ "bool": {} }),
)
}
}