rs_es/query/
joining.rs

1/*
2 * Copyright 2016-2018 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Joining queries
18
19use serde::Serialize;
20
21use crate::json::ShouldSkip;
22
23use super::{Query, ScoreMode};
24
25/// Nested query
26#[derive(Debug, Default, Serialize)]
27pub struct NestedQuery {
28    path: String,
29    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
30    score_mode: Option<ScoreMode>,
31    query: Query,
32}
33
34impl Query {
35    pub fn build_nested<A, B>(path: A, query: B) -> NestedQuery
36    where
37        A: Into<String>,
38        B: Into<Query>,
39    {
40        NestedQuery {
41            path: path.into(),
42            query: query.into(),
43            ..Default::default()
44        }
45    }
46}
47
48impl NestedQuery {
49    add_field!(with_score_mode, score_mode, ScoreMode);
50
51    build!(Nested);
52}
53
54/// Has Child query
55#[derive(Debug, Default, Serialize)]
56pub struct HasChildQuery {
57    doc_type: String,
58    query: Query,
59    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
60    score_mode: Option<ScoreMode>,
61    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
62    min_children: Option<u64>,
63    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
64    max_children: Option<u64>,
65}
66
67/// Has Parent query
68#[derive(Debug, Default, Serialize)]
69pub struct HasParentQuery {
70    parent_type: String,
71    query: Query,
72    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
73    score_mode: Option<ScoreMode>,
74}
75
76impl Query {
77    pub fn build_has_child<A, B>(doc_type: A, query: B) -> HasChildQuery
78    where
79        A: Into<String>,
80        B: Into<Query>,
81    {
82        HasChildQuery {
83            doc_type: doc_type.into(),
84            query: query.into(),
85            ..Default::default()
86        }
87    }
88
89    pub fn build_has_parent<A, B>(parent_type: A, query: B) -> HasParentQuery
90    where
91        A: Into<String>,
92        B: Into<Query>,
93    {
94        HasParentQuery {
95            parent_type: parent_type.into(),
96            query: query.into(),
97            ..Default::default()
98        }
99    }
100}
101
102impl HasChildQuery {
103    add_field!(with_score_mode, score_mode, ScoreMode);
104    add_field!(with_min_children, min_children, u64);
105    add_field!(with_max_children, max_children, u64);
106
107    build!(HasChild);
108}
109
110impl HasParentQuery {
111    add_field!(with_score_mode, score_mode, ScoreMode);
112
113    build!(HasParent);
114}