elasticsearch_dsl/search/queries/specialized/
pinned_query.rs1use crate::search::*;
2use crate::util::*;
3
4#[derive(Debug, Clone, PartialEq, Serialize)]
18#[serde(remote = "Self")]
19pub struct PinnedQuery {
20 #[serde(flatten)]
21 values: PinnedQueryValues,
22
23 organic: Box<Query>,
26
27 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
28 boost: Option<f32>,
29
30 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
31 _name: Option<String>,
32}
33
34impl Query {
35 pub fn pinned<Q>(values: PinnedQueryValues, organic: Q) -> PinnedQuery
37 where
38 Q: Into<Query>,
39 {
40 PinnedQuery {
41 values,
42 organic: Box::new(organic.into()),
43 boost: None,
44 _name: None,
45 }
46 }
47}
48
49impl PinnedQuery {
50 add_boost_and_name!();
51}
52
53impl ShouldSkip for PinnedQuery {
54 fn should_skip(&self) -> bool {
55 self.organic.should_skip()
56 }
57}
58
59serialize_with_root!("pinned": PinnedQuery);
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn serialization() {
67 assert_serialize_query(
68 Query::pinned(PinnedQueryValues::ids([1]), Query::term("user_id", 2)),
69 json!({
70 "pinned": {
71 "ids": ["1"],
72 "organic": {
73 "term": {
74 "user_id": {
75 "value": 2
76 }
77 }
78 }
79 }
80 }),
81 );
82
83 assert_serialize_query(
84 Query::pinned(
85 PinnedQueryValues::docs([PinnedDocument::new("index", 1)]),
86 Query::term("user_id", 2),
87 ),
88 json!({
89 "pinned": {
90 "docs": [{ "_index": "index", "_id": "1" }],
91 "organic": {
92 "term": {
93 "user_id": {
94 "value": 2
95 }
96 }
97 }
98 }
99 }),
100 );
101
102 assert_serialize_query(
103 Query::pinned(PinnedQueryValues::ids([1]), Query::term("user_id", 2))
104 .boost(2)
105 .name("test"),
106 json!({
107 "pinned": {
108 "ids": ["1"],
109 "organic": {
110 "term": {
111 "user_id": {
112 "value": 2
113 }
114 }
115 },
116 "boost": 2.0,
117 "_name": "test"
118 }
119 }),
120 );
121
122 assert_serialize_query(
123 Query::pinned(
124 PinnedQueryValues::docs([PinnedDocument::new("index", 1)]),
125 Query::term("user_id", 2),
126 )
127 .boost(2)
128 .name("test"),
129 json!({
130 "pinned": {
131 "docs": [{ "_index": "index", "_id": "1" }],
132 "organic": {
133 "term": {
134 "user_id": {
135 "value": 2
136 }
137 }
138 },
139 "boost": 2.0,
140 "_name": "test"
141 }
142 }),
143 );
144 }
145}