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

/// Returns child documents joined to a specific parent document. You can use a join field mapping
/// to create parent-child relationships between documents in the same index.
///
/// To create parent_id query:
/// ```
/// # use elasticsearch_dsl::queries::*;
/// # use elasticsearch_dsl::queries::params::*;
/// # let query =
/// Query::parent_id("test", 1);
/// ```
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ParentId-query.html>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct ParentIdQuery {
    r#type: String,

    id: String,

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

    #[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 [`ParentIdQuery`]
    ///
    /// - `type` - Name of the child relationship mapped for the join field
    /// - `id` - ID of the parent document. The query will return child documents of this
    /// parent document.
    pub fn parent_id<T, U>(r#type: T, id: U) -> ParentIdQuery
    where
        T: ToString,
        U: ToString,
    {
        ParentIdQuery {
            r#type: r#type.to_string(),
            id: id.to_string(),
            ignore_unmapped: None,
            boost: None,
            _name: None,
        }
    }
}

impl ParentIdQuery {
    /// Indicates whether to ignore an unmapped `type` and not return any documents instead of an
    /// error. Defaults to `false`.
    ///
    /// If `false`, Elasticsearch returns an error if the `type` is unmapped.
    ///
    /// You can use this parameter to query multiple indices that may not contain the `type`.
    pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
        self.ignore_unmapped = Some(ignore_unmapped);
        self
    }

    add_boost_and_name!();
}

impl ShouldSkip for ParentIdQuery {
    fn should_skip(&self) -> bool {
        self.r#type.should_skip() || self.id.should_skip()
    }
}

serialize_with_root!("parent_id": ParentIdQuery);

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

    #[test]
    fn serialization() {
        assert_serialize_query(
            Query::parent_id("my-child", 1),
            json!({
                "parent_id": {
                    "type": "my-child",
                    "id": "1"
                }
            }),
        );

        assert_serialize_query(
            Query::parent_id("my-child", 1)
                .boost(2)
                .name("test")
                .ignore_unmapped(true),
            json!({
                "parent_id": {
                    "type": "my-child",
                    "id": "1",
                    "ignore_unmapped": true,
                    "boost": 2.0,
                    "_name": "test"
                }
            }),
        );
    }
}