-
Notifications
You must be signed in to change notification settings - Fork 0
/
compound_query_constant_score.rs
133 lines (118 loc) · 3.85 KB
/
compound_query_constant_score.rs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use serde_json::json;
use os_query_builder_rs::compound_query::bool::Bool;
use os_query_builder_rs::compound_query::boosting::Boosting;
use os_query_builder_rs::compound_query::constant_score::ConstantScore;
use os_query_builder_rs::full_text::r#match::Match;
use os_query_builder_rs::misc::query_field::QueryField;
use os_query_builder_rs::model::Query;
#[test]
fn example_from_docs_test() {
let match_value = Match::new().field("text_entry").value("Hamlet");
let constant_score = ConstantScore::new(match_value).boost(1.2);
let query = Query::new().query(constant_score);
let actual_json = json!(query);
let excepted_json = json!({
"query": {
"constant_score": {
"filter": {
"match": {
"text_entry": {
"query":"Hamlet"
}
}
},
"boost": 1.2
}
}
});
assert_eq!(actual_json, excepted_json);
}
#[test]
fn simple_match_test() {
let match_value = Match::new().field("brand").value("Инструменты СТО");
let constant_score = ConstantScore::new(match_value);
let query = Query::new().query(constant_score);
let actual_json = json!(query);
let excepted_json = json!({
"query": {
"constant_score": {
"filter": {
"match": {
"brand": {
"query":"Инструменты СТО"
}
}
}
}
}
});
assert_eq!(actual_json, excepted_json);
}
#[test]
fn with_nested_bool_test() {
let match_value = Match::new().field("brand").value("FIAT");
let boolean = Bool::new().must(vec![QueryField::Match(match_value)]);
let constant_score = ConstantScore::new(boolean);
let query = Query::new().query(constant_score);
let actual_json = json!(query);
let excepted_json = json!({
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"match": {
"brand": {
"query": "FIAT"
}
}
}
]
}
}
}
}
});
assert_eq!(actual_json, excepted_json);
}
#[test]
fn with_nested_boosting_with_boost_test() {
let match_positive = Match::new()
.field("product_id")
.value("46118216");
let match_negative = Match::new()
.field("name")
.value("Деталь");
let boosting = Boosting::new(match_positive, match_negative)
.negative_boost(0.1f64);
let constant_score = ConstantScore::new(boosting);
let query = Query::new().query(constant_score);
let actual_json = json!(query);
let excepted_json = json!({
"query": {
"constant_score": {
"filter": {
"boosting": {
"positive": {
"match": {
"product_id": {
"query": "46118216"
}
}
},
"negative": {
"match": {
"name": {
"query": "Деталь"
}
}
},
"negative_boost": 0.1
}
}
}
}
});
assert_eq!(actual_json, excepted_json);
}