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;
#[derive(Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SuggestOption {
Completion(CompletionSuggestOption),
Term(TermSuggestOption),
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),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompletionSuggestOption {
pub text: String,
#[serde(rename = "_index")]
pub index: String,
#[serde(rename = "_id")]
pub id: String,
#[serde(alias = "_score")]
pub score: f32,
#[serde(
skip_serializing_if = "ShouldSkip::should_skip",
rename = "_source",
default
)]
pub source: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
pub contexts: Map<String, Vec<String>>,
}
impl CompletionSuggestOption {
pub fn parse<T>(&self) -> Result<T, serde_json::Error>
where
T: DeserializeOwned,
{
serde_json::from_value(self.source.clone().unwrap_or_default())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TermSuggestOption {
pub text: String,
pub score: f32,
#[serde(rename = "freq")]
pub frequency: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PhraseSuggestOption {
pub text: String,
pub score: f32,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
pub collate_match: Option<bool>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
pub highlighted: Option<String>,
}