elasticsearch_dsl/search/queries/joining/
parent_id_query.rs

1use crate::search::*;
2use crate::util::*;
3
4/// Returns child documents joined to a specific parent document. You can use a join field mapping
5/// to create parent-child relationships between documents in the same index.
6///
7/// To create parent_id query:
8/// ```
9/// # use elasticsearch_dsl::queries::*;
10/// # use elasticsearch_dsl::queries::params::*;
11/// # let query =
12/// Query::parent_id("test", 1);
13/// ```
14/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ParentId-query.html>
15#[derive(Debug, Clone, PartialEq, Serialize)]
16#[serde(remote = "Self")]
17pub struct ParentIdQuery {
18    r#type: String,
19
20    id: String,
21
22    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
23    ignore_unmapped: Option<bool>,
24
25    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
26    boost: Option<f32>,
27
28    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
29    _name: Option<String>,
30}
31
32impl Query {
33    /// Creates an instance of [`ParentIdQuery`]
34    ///
35    /// - `type` - Name of the child relationship mapped for the join field
36    /// - `id` - ID of the parent document. The query will return child documents of this
37    ///   parent document.
38    pub fn parent_id<T, U>(r#type: T, id: U) -> ParentIdQuery
39    where
40        T: ToString,
41        U: ToString,
42    {
43        ParentIdQuery {
44            r#type: r#type.to_string(),
45            id: id.to_string(),
46            ignore_unmapped: None,
47            boost: None,
48            _name: None,
49        }
50    }
51}
52
53impl ParentIdQuery {
54    /// Indicates whether to ignore an unmapped `type` and not return any documents instead of an
55    /// error. Defaults to `false`.
56    ///
57    /// If `false`, Elasticsearch returns an error if the `type` is unmapped.
58    ///
59    /// You can use this parameter to query multiple indices that may not contain the `type`.
60    pub fn ignore_unmapped(mut self, ignore_unmapped: bool) -> Self {
61        self.ignore_unmapped = Some(ignore_unmapped);
62        self
63    }
64
65    add_boost_and_name!();
66}
67
68impl ShouldSkip for ParentIdQuery {
69    fn should_skip(&self) -> bool {
70        self.r#type.should_skip() || self.id.should_skip()
71    }
72}
73
74serialize_with_root!("parent_id": ParentIdQuery);
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn serialization() {
82        assert_serialize_query(
83            Query::parent_id("my-child", 1),
84            json!({
85                "parent_id": {
86                    "type": "my-child",
87                    "id": "1"
88                }
89            }),
90        );
91
92        assert_serialize_query(
93            Query::parent_id("my-child", 1)
94                .boost(2)
95                .name("test")
96                .ignore_unmapped(true),
97            json!({
98                "parent_id": {
99                    "type": "my-child",
100                    "id": "1",
101                    "ignore_unmapped": true,
102                    "boost": 2.0,
103                    "_name": "test"
104                }
105            }),
106        );
107    }
108}