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
use std::collections::HashMap;
use serde::{Serialize, Serializer};
use serde::ser::SerializeStruct;
use crate::query::QueryTrait;
use serde_json::{json, Map, Value};
use crate::util::UtilMap;

#[derive(Default)]
pub struct MatchQuery {
    field: String,
    query: String,
    boost: Option<f64>,
}

impl MatchQuery {
    pub fn new(field: &str, query: &str) -> MatchQuery {
        let mut value = MatchQuery::default();
        value.field = field.to_string();
        value.query = query.to_string();
        return value;
    }
    pub fn set_boost(mut self, boost: f64) -> MatchQuery {
        self.boost = Some(boost);
        self
    }
}


impl QueryTrait for MatchQuery {
    fn build(&self) -> Value {
        let mut query = UtilMap::new();
        query.append_string("query",self.query.to_string());
        query.append_boost(self.boost);

        let mut root = UtilMap::new();
        root.append_object(self.field.to_string(),query);
        root.build_object(self.query_name())
    }
    fn query_name(&self) -> String {
        return "match".to_string();
    }
}

#[test]
fn test() {
    let build = MatchQuery::new("title", "elastic").set_boost(100.0);
    assert_eq!("{\"match\":{\"title\":{\"boost\":100.0,\"query\":\"elastic\"}}}", build.build().to_string());
}