os_query_builder_rs/term/
terms.rs1use std::collections::HashMap;
2use serde::Serialize;
3use super::term_type::TermType;
4
5#[derive(Debug, Default, Clone, Serialize)]
6pub struct Terms {
7
8 #[serde(flatten)]
9 #[serde(skip_serializing_if = "Option::is_none")]
10 terms_query: Option<HashMap<String, Vec<TermType>>>,
11
12 #[serde(flatten)]
13 #[serde(skip_serializing_if = "Option::is_none")]
14 terms_lookup: Option<HashMap<String, TermsLookup>>,
15
16 #[serde(skip_serializing_if = "Option::is_none")]
17 boost: Option<f64>
18}
19
20#[derive(Debug, Default, Clone, Serialize)]
21pub struct TermsLookup {
22 index: String,
23 id: String,
24 path: String,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 routing: Option<String>,
27}
28
29impl Terms {
30 pub fn new_with_terms_query<F,T,K>(field_name: K, values: F) -> Self
31 where
32 F: IntoIterator<Item=T>,
33 T: Into<TermType>,
34 K: Into<String>
35 {
36 let mut terms = HashMap::with_capacity(1);
37 terms.insert(field_name.into(), values.into_iter()
38 .map(|x| x.into())
39 .collect()
40 );
41
42 Self {
43 terms_query: Some(terms),
44 terms_lookup: None,
45 boost: None
46 }
47 }
48
49 pub fn new_with_terms_lookup<T:Into<String>>(field_name: T, values: TermsLookup) -> Self
50 {
51
52 let mut terms = HashMap::with_capacity(1);
53 terms.insert(field_name.into(), values.into());
54
55 Self {
56 terms_query: None,
57 terms_lookup: Some(terms),
58 boost: None
59 }
60 }
61
62 pub fn boost(self, boost: f64) -> Self {
63 Self {
64 boost: Some(boost),
65 ..self
66 }
67 }
68}
69
70
71impl TermsLookup {
72
73 pub fn new<T,F,X>(index: T, id: F, path: X) -> Self
74 where T: Into<String>,
75 F: Into<String>,
76 X: Into<String>
77 {
78 Self {
79 index: index.into(),
80 id: id.into(),
81 path: path.into(),
82 routing: None
83 }
84 }
85
86 pub fn routing<T:Into<String>>(self, routing: T) -> Self {
87 Self {
88 routing: Some(routing.into()),
89 ..self
90 }
91 }
92}