Skip to main content

opensearch_dsl/search/response/
suggest_option.rs

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