os_query_builder_rs/
lib.rs

1pub mod full_text;
2pub mod misc;
3pub mod term;
4pub mod compound_query;
5
6pub mod model {
7    use serde::Serialize;
8    use serde_json::Value;
9    use crate::misc::query_field::QueryField;
10
11
12    /// Examples
13    /// ```
14    /// use os_query_builder_rs::full_text::multi_match::MultiMatch;
15    /// use os_query_builder_rs::misc::operator::Operator;
16    /// use os_query_builder_rs::misc::query_field::QueryField;
17    /// use os_query_builder_rs::misc::r#type::Type;
18    /// use os_query_builder_rs::model::Query;
19    ///
20    /// let multi_match = MultiMatch::new()
21    ///             .fields(vec!["brands", "articles"])
22    ///             .value("oc47")
23    ///             .operator(Operator::And)
24    ///             .query_type(Type::BestFields)
25    ///             .boost(2)
26    ///             .minimum_should_match("90%");
27    ///
28    /// let query = Query::new()
29    ///             .source(vec!["test"])
30    ///             .query(multi_match);
31    /// ```
32    #[derive(Debug, Default, Clone, Serialize)]
33    pub struct Query {
34        #[serde(skip_serializing_if = "Option::is_none")]
35        from: Option<usize>,
36
37        #[serde(skip_serializing_if = "Option::is_none")]
38        size: Option<usize>,
39
40        #[serde(rename = "_source", skip_serializing_if = "Option::is_none")]
41        source: Option<Vec<String>>,
42
43        #[serde(skip_serializing_if = "Option::is_none")]
44        query: Option<QueryField>,
45
46        #[serde(skip_serializing_if = "Option::is_none")]
47        aggs: Option<Value>,
48    }
49
50    impl Query {
51        pub fn new() -> Self {
52            Self::default()
53        }
54
55        pub fn source<F, T>(self, source: F) -> Self
56            where
57                F: IntoIterator<Item=T>,
58                T: Into<String>
59        {
60            Self {
61                source: Some(source
62                    .into_iter()
63                    .map(|x| x.into())
64                    .collect()),
65                ..self
66            }
67        }
68
69        pub fn query<T: Into<QueryField> + Serialize>(self, query: T) -> Self {
70            Self {
71                query: Some(query.into()),
72                ..self
73            }
74        }
75
76        pub fn from<T: Into<usize> + Serialize>(self, from: T) -> Self {
77            Self {
78                from: Some(from.into()),
79                ..self
80            }
81        }
82
83        pub fn size<T: Into<usize> + Serialize>(self, size: T) -> Self {
84            Self {
85                size: Some(size.into()),
86                ..self
87            }
88        }
89
90        pub fn aggs<T: Into<Value> + Serialize>(self, aggs: T) -> Self {
91            Self {
92                aggs: Some(aggs.into()),
93                ..self
94            }
95        }
96    }
97}