elasticsearch_dsl/search/response/
suggest_option.rs

1use crate::{util::ShouldSkip, Map};
2use serde::de::DeserializeOwned;
3
4/// Suggester response option variants
5#[derive(Clone, PartialEq, Serialize, Deserialize)]
6#[serde(untagged)]
7pub enum SuggestOption {
8    /// Completion suggester response option
9    Completion(CompletionSuggestOption),
10
11    /// Term suggester response option
12    Term(TermSuggestOption),
13
14    /// Phrase suggester response option
15    Phrase(PhraseSuggestOption),
16}
17
18impl std::fmt::Debug for SuggestOption {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::Completion(suggest_option) => suggest_option.fmt(f),
22            Self::Term(suggest_option) => suggest_option.fmt(f),
23            Self::Phrase(suggest_option) => suggest_option.fmt(f),
24        }
25    }
26}
27
28/// Suggester response item option
29#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30pub struct CompletionSuggestOption {
31    /// Suggested text
32    pub text: String,
33
34    /// Document index
35    #[serde(rename = "_index")]
36    pub index: String,
37
38    /// Document id
39    #[serde(rename = "_id")]
40    pub id: String,
41
42    /// Document score for completion suggester, suggest score for term, phrase
43    #[serde(alias = "_score")]
44    pub score: f32,
45
46    /// Document source
47    ///
48    /// Not using [crate::Source] due to a bug in enums and RawValue
49    /// <https://github.com/serde-rs/json/issues/779>
50    #[serde(
51        skip_serializing_if = "ShouldSkip::should_skip",
52        rename = "_source",
53        default
54    )]
55    pub source: Option<serde_json::Value>,
56
57    /// The contexts associated with the completed document
58    ///
59    /// Contexts always return either as a category or as geohash
60    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
61    pub contexts: Map<String, Vec<String>>,
62}
63
64impl CompletionSuggestOption {
65    /// Parses document source into a concrete type
66    pub fn parse<T>(&self) -> Result<T, serde_json::Error>
67    where
68        T: DeserializeOwned,
69    {
70        serde_json::from_value(self.source.clone().unwrap_or_default())
71    }
72}
73
74/// Term suggester response item option
75#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
76pub struct TermSuggestOption {
77    /// Suggested text
78    pub text: String,
79
80    /// Suggest score
81    pub score: f32,
82
83    /// Term frequency
84    #[serde(rename = "freq")]
85    pub frequency: u64,
86}
87
88/// Phrase suggester response item option
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct PhraseSuggestOption {
91    /// Suggested text
92    pub text: String,
93
94    /// Suggest score
95    pub score: f32,
96
97    /// Phrase suggestions only, true if matching documents for the collate query were found
98    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
99    pub collate_match: Option<bool>,
100
101    /// Highlighted version of text
102    #[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
103    pub highlighted: Option<String>,
104}