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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::aggregation::AggregationTrait;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use serde_json::{json, Value};
use crate::merge;
#[derive(Default)]
pub struct TermsAggregation {
name: String,
value: TermsValue,
aggregation: Value,
}
#[derive(Default, Clone)]
struct TermsOrder {
order_field: String,
order_value: String,
}
#[derive(Default)]
struct TermsValue {
script: String,
field: String,
size: i64,
order: Option<TermsOrder>,
include: Vec<String>,
exclude: Vec<String>,
}
impl TermsAggregation {
pub fn new(name: &str) -> Self {
let mut term = TermsAggregation {
name: name.to_string(),
..Default::default()
};
term.value.size = 10;
term
}
pub fn set_field(mut self, field: &str) -> Self {
self.value.field = field.to_string();
self
}
pub fn set_size(mut self, size: i64) -> Self {
self.value.size = size;
self
}
pub fn set_script(mut self, script: &str) -> Self {
self.value.script = script.to_string();
self
}
pub fn set_order(mut self, order_field: &str, order_value: &str) -> Self {
self.value.order = Some(TermsOrder {
order_field: order_field.to_string(),
order_value: order_value.to_string(),
});
self
}
pub fn set_include(mut self, include: &[&str]) -> Self {
self.value.include = include.iter().map(|q| q.to_string()).collect();
self
}
pub fn set_exclude(mut self, exclude: &[&str]) -> Self {
self.value.exclude = exclude.iter().map(|q| q.to_string()).collect();
self
}
pub fn set_aggregation<T>(mut self, aggregation: T) -> Self
where
T: AggregationTrait,
{
self.aggregation = aggregation.build();
self
}
pub fn append_aggregation<T>(mut self, query: T) -> Self
where
T: AggregationTrait,
{
let mut values = self.aggregation.clone();
merge(&mut values, &query.build());
self.aggregation = json!(values);
return self;
}
}
impl Serialize for TermsValue {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("TermsBuilder", 0)?;
if self.script.is_empty() {
state.serialize_field("field", &self.field)?;
state.serialize_field("size", &self.size)?;
} else {
}
if self.order.is_some() {
state.serialize_field("order", &self.order.as_ref().unwrap())?;
}
if !self.include.is_empty() {
state.serialize_field("include", &self.include)?;
}
if !self.exclude.is_empty() {
state.serialize_field("exclude", &self.exclude)?;
}
state.end()
}
}
impl Serialize for TermsOrder {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("TermsOrder", 0)?;
state.serialize_field("order_field", &self.order_field)?;
state.serialize_field("order_value", &self.order_value)?;
state.end()
}
}
impl Serialize for TermsAggregation {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("BoolQuery", 0)?;
state.serialize_field("terms", &self.value)?;
if !(self.aggregation.is_null() || self.aggregation.to_string().is_empty()) {
state.serialize_field("aggs", &self.aggregation)?;
}
state.end()
}
}
impl AggregationTrait for TermsAggregation {
fn name(&self) -> &str {
self.name.as_str()
}
fn build(&self) -> Value {
let name = self.name.to_string();
json!({ name: self })
}
fn query_name(&self) -> String {
"terms".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregation::AggregationTrait;
#[test]
fn test_terms_aggregation() {
let agg = TermsAggregation::new("hoge")
.set_field("field_name")
.set_include(&[
"field_a",
"field_b",
])
.set_aggregation(TermsAggregation::new("agg"));
let json = agg.build();
println!("{}", json);
}
}