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
use crate::{util::ShouldSkip, Map};
use serde::de::DeserializeOwned;

/// Suggester response option variants
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SuggestOption {
    /// Completion suggester response option
    Completion(CompletionSuggestOption),

    /// Term suggester response option
    Term(TermSuggestOption),

    /// Phrase suggester response option
    Phrase(PhraseSuggestOption),
}

impl std::fmt::Debug for SuggestOption {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Completion(suggest_option) => suggest_option.fmt(f),
            Self::Term(suggest_option) => suggest_option.fmt(f),
            Self::Phrase(suggest_option) => suggest_option.fmt(f),
        }
    }
}

/// Suggester response item option
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompletionSuggestOption {
    /// Suggested text
    pub text: String,

    /// Document index
    #[serde(rename = "_index")]
    pub index: String,

    /// Document id
    #[serde(rename = "_id")]
    pub id: String,

    /// Document score for completion suggester, suggest score for term, phrase
    #[serde(alias = "_score")]
    pub score: f32,

    /// Document source
    ///
    /// Not using [crate::Source] due to a bug in enums and RawValue
    /// <https://github.com/serde-rs/json/issues/779>
    #[serde(
        skip_serializing_if = "ShouldSkip::should_skip",
        rename = "_source",
        default
    )]
    pub source: Option<serde_json::Value>,

    /// The contexts associated with the completed document
    ///
    /// Contexts always return either as a category or as geohash
    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
    pub contexts: Map<String, Vec<String>>,
}

impl CompletionSuggestOption {
    /// Parses document source into a concrete type
    pub fn parse<T>(&self) -> Result<T, serde_json::Error>
    where
        T: DeserializeOwned,
    {
        serde_json::from_value(self.source.clone().unwrap_or_default())
    }
}

/// Term suggester response item option
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TermSuggestOption {
    /// Suggested text
    pub text: String,

    /// Suggest score
    pub score: f32,

    /// Term frequency
    #[serde(rename = "freq")]
    pub frequency: u64,
}

/// Phrase suggester response item option
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PhraseSuggestOption {
    /// Suggested text
    pub text: String,

    /// Suggest score
    pub score: f32,

    /// Phrase suggestions only, true if matching documents for the collate query were found
    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
    pub collate_match: Option<bool>,

    /// Highlighted version of text
    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
    pub highlighted: Option<String>,
}