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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::search::*;
use crate::util::*;
use std::convert::TryInto;
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct TopHitsAggregation {
#[serde(skip_serializing)]
pub(crate) name: String,
top_hits: TopHitsAggregationInner,
}
#[derive(Debug, Clone, Serialize, PartialEq)]
struct TopHitsAggregationInner {
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_source: Option<SourceFilter>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
from: Option<u64>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
size: Option<u64>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
sort: Vec<Sort>,
}
impl Aggregation {
pub fn top_hits(name: impl Into<String>) -> TopHitsAggregation {
TopHitsAggregation {
name: name.into(),
top_hits: TopHitsAggregationInner {
_source: None,
from: None,
size: None,
sort: vec![],
},
}
}
}
impl TopHitsAggregation {
pub fn source(mut self, source: impl Into<SourceFilter>) -> Self {
self.top_hits._source = Some(source.into());
self
}
pub fn from(mut self, from: impl TryInto<u64>) -> Self {
if let Ok(from) = from.try_into() {
self.top_hits.from = Some(from);
}
self
}
pub fn size(mut self, size: impl TryInto<u64>) -> Self {
if let Ok(size) = size.try_into() {
self.top_hits.size = Some(size);
}
self
}
pub fn sort(mut self, sort: impl Into<Vec<Sort>>) -> Self {
self.top_hits.sort.extend(sort.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
test_serialization! {
with_required_fields(
Aggregation::top_hits("test_agg"),
json!({ "top_hits": { } })
);
with_all_fields(
Aggregation::top_hits("test_agg")
.source(false)
.from(2u8)
.size(10u8)
.sort(Sort::new("sort_field").order(SortOrder::Desc)),
json!({
"top_hits": {
"_source": false,
"from": 2,
"size": 10,
"sort": [
{ "sort_field": { "order": "desc" } }
]
}
})
);
}
}