elastic_query_builder/query/
exists_query.rs

1use crate::query::QueryTrait;
2use serde_json::{json, Value};
3
4#[derive(Default)]
5pub struct ExistsQuery {
6    field: String,
7}
8
9impl ExistsQuery {
10    pub fn new(field: &str) -> ExistsQuery {
11        let mut value = ExistsQuery::default();
12        value.field = field.to_string();
13        return value;
14    }
15}
16
17impl QueryTrait for ExistsQuery {
18    fn build(&self) -> Value {
19        let name = self.query_name();
20        let field = self.field.to_string();
21        json!({
22            name: {
23                "field":field
24            }
25        })
26    }
27
28    fn query_name(&self) -> String {
29        return "exists".to_string();
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::query::exists_query::ExistsQuery;
36    use crate::query::QueryTrait;
37
38    #[test]
39    fn build() {
40        assert_eq!(
41            ExistsQuery::new("id").build().to_string(),
42            "{\"exists\":{\"field\":\"id\"}}"
43        );
44    }
45}