elastic_query_builder/query/
wildcard_query.rs

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