elastic_query_builder/query/
match_query.rs

1use crate::query::QueryTrait;
2use crate::util::UtilMap;
3use serde_json::Value;
4
5#[derive(Default)]
6pub struct MatchQuery {
7    field: String,
8    query: String,
9    boost: Option<f64>,
10}
11
12impl MatchQuery {
13    pub fn new(field: &str, query: &str) -> MatchQuery {
14        let mut value = MatchQuery::default();
15        value.field = field.to_string();
16        value.query = query.to_string();
17        return value;
18    }
19    pub fn set_boost(mut self, boost: f64) -> MatchQuery {
20        self.boost = Some(boost);
21        self
22    }
23}
24
25impl QueryTrait for MatchQuery {
26    fn build(&self) -> Value {
27        let mut query = UtilMap::new();
28        query.append_string("query", self.query.to_string());
29        query.append_boost(self.boost);
30
31        let mut root = UtilMap::new();
32        root.append_object(self.field.to_string(), query);
33        root.build_object(self.query_name())
34    }
35    fn query_name(&self) -> String {
36        return "match".to_string();
37    }
38}
39
40#[test]
41fn test() {
42    let build = MatchQuery::new("title", "elastic").set_boost(100.0);
43    assert_eq!(
44        "{\"match\":{\"title\":{\"boost\":100.0,\"query\":\"elastic\"}}}",
45        build.build().to_string()
46    );
47}