elasticsearch_dsl/search/queries/params/
regexp_query.rs

1use serde::ser::{Serialize, Serializer};
2
3/// You can use the flags parameter to enable more optional operators for Lucene’s regular
4/// expression engine.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
6pub enum RegexpFlag {
7    /// Enables all optional operators.
8    All,
9
10    /// Enables the `~` operator. You can use `~` to negate the shortest following pattern.
11    /// For example:
12    ///
13    /// `a~bc   # matches 'adc' and 'aec' but not 'abc'`
14    Complement,
15
16    /// Enables the `<>` operators. You can use `<>` to match a numeric range. For example:
17    ///
18    /// `foo<1-100>      # matches 'foo1', 'foo2' ... 'foo99', 'foo100'`
19    /// `foo<01-100>     # matches 'foo01', 'foo02' ... 'foo99', 'foo100'`
20    Interval,
21
22    /// Enables the `&` operator, which acts as an AND operator. The match will succeed if patterns
23    /// on both the left side AND the right side matches. For example:
24    ///
25    /// `aaa.+&.+bbb  # matches 'aaabbb'`
26    Intersection,
27
28    /// Enables the `@` operator. You can use @ to match any entire string.
29    ///
30    /// You can combine the `@` operator with `&` and `~` operators to create an
31    /// "everything except" logic. For example:
32    ///
33    /// `@&~(abc.+)  # matches everything except terms beginning with 'abc'`
34    Anystring,
35}
36
37impl From<RegexpFlag> for &'static str {
38    fn from(value: RegexpFlag) -> Self {
39        match value {
40            RegexpFlag::All => "ALL",
41            RegexpFlag::Complement => "COMPLEMENT",
42            RegexpFlag::Interval => "INTERVAL",
43            RegexpFlag::Intersection => "INTERSECTION",
44            RegexpFlag::Anystring => "ANYSTRING",
45        }
46    }
47}
48
49impl From<RegexpFlag> for String {
50    fn from(value: RegexpFlag) -> Self {
51        <&'static str>::from(value).to_string()
52    }
53}
54
55impl std::fmt::Display for RegexpFlag {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        <&'static str>::from(*self).fmt(f)
58    }
59}
60
61impl Serialize for RegexpFlag {
62    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63    where
64        S: Serializer,
65    {
66        <&'static str>::from(*self).serialize(serializer)
67    }
68}