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
pub mod full_text;
pub mod misc;
pub mod term;
pub mod compound_query;

pub mod model {
    use serde::Serialize;
    use serde_json::Value;
    use crate::misc::query_field::QueryField;


    /// Examples
    /// ```
    /// use os_query_builder_rs::full_text::multi_match::MultiMatch;
    /// use os_query_builder_rs::misc::operator::Operator;
    /// use os_query_builder_rs::misc::query_field::QueryField;
    /// use os_query_builder_rs::misc::r#type::Type;
    /// use os_query_builder_rs::model::Query;
    ///
    /// let multi_match = MultiMatch::new()
    ///             .fields(vec!["brands", "articles"])
    ///             .value("oc47")
    ///             .operator(Operator::And)
    ///             .query_type(Type::BestFields)
    ///             .boost(2)
    ///             .minimum_should_match("90%");
    ///
    /// let query = Query::new()
    ///             .source(vec!["test"])
    ///             .query(multi_match);
    /// ```
    #[derive(Debug, Default, Clone, Serialize)]
    pub struct Query {
        #[serde(skip_serializing_if = "Option::is_none")]
        from: Option<usize>,

        #[serde(skip_serializing_if = "Option::is_none")]
        size: Option<usize>,

        #[serde(rename = "_source", skip_serializing_if = "Option::is_none")]
        source: Option<Vec<String>>,

        #[serde(skip_serializing_if = "Option::is_none")]
        query: Option<QueryField>,

        #[serde(skip_serializing_if = "Option::is_none")]
        aggs: Option<Value>,
    }

    impl Query {
        pub fn new() -> Self {
            Self::default()
        }

        pub fn source<F, T>(self, source: F) -> Self
            where
                F: IntoIterator<Item=T>,
                T: Into<String>
        {
            Self {
                source: Some(source
                    .into_iter()
                    .map(|x| x.into())
                    .collect()),
                ..self
            }
        }

        pub fn query<T: Into<QueryField> + Serialize>(self, query: T) -> Self {
            Self {
                query: Some(query.into()),
                ..self
            }
        }

        pub fn from<T: Into<usize> + Serialize>(self, from: T) -> Self {
            Self {
                from: Some(from.into()),
                ..self
            }
        }

        pub fn size<T: Into<usize> + Serialize>(self, size: T) -> Self {
            Self {
                size: Some(size.into()),
                ..self
            }
        }

        pub fn aggs<T: Into<Value> + Serialize>(self, aggs: T) -> Self {
            Self {
                aggs: Some(aggs.into()),
                ..self
            }
        }
    }
}