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
use crate::search::*;
use crate::util::*;

/// In order to percolate a newly indexed document, the [percolate](PercolateLookupQuery) query can
/// be used. Based on the response from an index request, the `_id` and other meta information can
/// be used to immediately percolate the newly added document.
///
/// To percolate indexed document:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # use serde_json::json;
/// # let query =
/// Query::percolate_lookup("field", "index_name", "document_id");
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html#_percolating_an_existing_document>
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(remote = "Self")]
pub struct PercolateLookupQuery {
    field: String,

    index: String,

    id: String,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    routing: Option<String>,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    preference: Option<String>,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    version: Option<u64>,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    name: Option<String>,
}

impl Query {
    /// Creates an instance of [`PercolateLookupQuery`]
    ///
    /// - `field` - The field of type `percolator` that holds the indexed queries
    /// - `index` - The index the document resides in
    /// - `id` - The id of the document to fetch
    pub fn percolate_lookup<S, T, U>(field: S, index: T, id: U) -> PercolateLookupQuery
    where
        S: ToString,
        T: ToString,
        U: ToString,
    {
        PercolateLookupQuery {
            field: field.to_string(),
            index: index.to_string(),
            id: id.to_string(),
            routing: None,
            preference: None,
            version: None,
            name: None,
        }
    }
}

impl PercolateLookupQuery {
    /// Routing to be used to fetch document to percolate
    pub fn routing<S>(mut self, routing: S) -> Self
    where
        S: ToString,
    {
        self.routing = Some(routing.to_string());
        self
    }

    /// Preference to be used to fetch document to percolate
    pub fn preference<S>(mut self, preference: S) -> Self
    where
        S: ToString,
    {
        self.preference = Some(preference.to_string());
        self
    }

    /// The expected version of the document to be fetched
    pub fn version(mut self, version: u64) -> Self {
        self.version = Some(version);
        self
    }

    /// The suffix to be used for the `_percolator_document_slot` field in case multiple `percolate`
    /// queries have been specified. This is an optional parameter
    pub fn name<S>(mut self, name: S) -> Self
    where
        S: ToString,
    {
        self.name = Some(name.to_string());
        self
    }
}

impl ShouldSkip for PercolateLookupQuery {}

serialize_with_root!("percolate": PercolateLookupQuery);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serialization() {
        assert_serialize_query(
            Query::percolate_lookup("field_name", "index_name", "document_id"),
            json!({
                "percolate": {
                    "field": "field_name",
                    "index": "index_name",
                    "id": "document_id"
                }
            }),
        );

        assert_serialize_query(
            Query::percolate_lookup("field_name", "index_name", "document_id")
                .name("toast")
                .routing("routing_value")
                .preference("preference_value")
                .version(123),
            json!({
                "percolate": {
                    "field": "field_name",
                    "name": "toast",
                    "index": "index_name",
                    "id": "document_id",
                    "routing": "routing_value",
                    "preference": "preference_value",
                    "version": 123,
                }
            }),
        );
    }
}