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 134 135 136 137 138 139 140 141 142 143 144 145
use crate::search::*;
use crate::util::*;
/// The most simple query, which matches all documents, giving them all a
/// `_score` of `1.0`.
///
/// To create match_all query:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::pinned(PinnedQueryValues::ids([1]), Query::term("user_id", 2))
/// .boost(2)
/// .name("matches_everything");
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct PinnedQuery {
#[serde(flatten)]
values: PinnedQueryValues,
/// Any choice of query used to rank documents which will be ranked below
/// the "pinned" documents.
organic: Box<Query>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
boost: Option<f32>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
_name: Option<String>,
}
impl Query {
/// Creates an instance of [`PinnedQuery`]
pub fn pinned<Q>(values: PinnedQueryValues, organic: Q) -> PinnedQuery
where
Q: Into<Query>,
{
PinnedQuery {
values,
organic: Box::new(organic.into()),
boost: None,
_name: None,
}
}
}
impl PinnedQuery {
add_boost_and_name!();
}
impl ShouldSkip for PinnedQuery {
fn should_skip(&self) -> bool {
self.organic.should_skip()
}
}
serialize_with_root!("pinned": PinnedQuery);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialization() {
assert_serialize_query(
Query::pinned(PinnedQueryValues::ids([1]), Query::term("user_id", 2)),
json!({
"pinned": {
"ids": ["1"],
"organic": {
"term": {
"user_id": {
"value": 2
}
}
}
}
}),
);
assert_serialize_query(
Query::pinned(
PinnedQueryValues::docs([PinnedDocument::new("index", 1)]),
Query::term("user_id", 2),
),
json!({
"pinned": {
"docs": [{ "_index": "index", "_id": "1" }],
"organic": {
"term": {
"user_id": {
"value": 2
}
}
}
}
}),
);
assert_serialize_query(
Query::pinned(PinnedQueryValues::ids([1]), Query::term("user_id", 2))
.boost(2)
.name("test"),
json!({
"pinned": {
"ids": ["1"],
"organic": {
"term": {
"user_id": {
"value": 2
}
}
},
"boost": 2.0,
"_name": "test"
}
}),
);
assert_serialize_query(
Query::pinned(
PinnedQueryValues::docs([PinnedDocument::new("index", 1)]),
Query::term("user_id", 2),
)
.boost(2)
.name("test"),
json!({
"pinned": {
"docs": [{ "_index": "index", "_id": "1" }],
"organic": {
"term": {
"user_id": {
"value": 2
}
}
},
"boost": 2.0,
"_name": "test"
}
}),
);
}
}