elasticsearch_dsl/search/queries/params/
inner_hits.rs

1use crate::search::*;
2use crate::util::*;
3use crate::Set;
4
5/// The [parent-join](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html)
6/// and [nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)
7/// features allow the return of documents that have matches in a different scope. In the
8/// parent/child case, parent documents are returned based on matches in child documents or
9/// child documents are returned based on matches in parent documents. In the nested case,
10/// documents are returned based on matches in nested inner objects.
11///
12/// In both cases, the actual matches in the different scopes that caused a document to be
13/// returned are hidden. In many cases, it’s very useful to know which inner nested objects
14/// (in the case of nested) or children/parent documents (in the case of parent/child) caused
15/// certain information to be returned. The inner hits feature can be used for this. This
16/// feature returns per search hit in the search response additional nested hits that caused a
17/// search hit to match in a different scope.
18///
19/// Inner hits can be used by defining an `inner_hits` definition on a `nested`, `has_child`
20/// or `has_parent` query and filter.
21///
22/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html>
23#[derive(Debug, Default, Clone, PartialEq, Serialize)]
24pub struct InnerHits {
25    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
26    _source: Option<SourceFilter>,
27
28    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
29    name: Option<String>,
30
31    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
32    from: Option<u64>,
33
34    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
35    size: Option<u64>,
36
37    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
38    sort: SortCollection,
39
40    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
41    highlight: Option<Highlight>,
42
43    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
44    docvalue_fields: Set<String>,
45
46    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
47    collapse: Option<InnerHitsCollapse>,
48}
49
50#[derive(Debug, Default, Clone, PartialEq, Serialize)]
51struct InnerHitsCollapse {
52    field: String,
53}
54
55impl InnerHits {
56    /// Creates a new instance of [InnerHits](InnerHits)
57    pub fn new() -> Self {
58        Default::default()
59    }
60
61    /// Indicates which source fields are returned for matching documents
62    pub fn source<T>(mut self, source: T) -> Self
63    where
64        T: Into<SourceFilter>,
65    {
66        self._source = Some(source.into());
67        self
68    }
69
70    /// Inner hit name, useful when multiple `inner_hits` exist in a single search request
71    pub fn name<T>(mut self, name: T) -> Self
72    where
73        T: ToString,
74    {
75        self.name = Some(name.to_string());
76        self
77    }
78
79    /// Starting document offset.
80    ///
81    /// Defaults to `0`.
82    pub fn from(mut self, from: u64) -> Self {
83        self.from = Some(from);
84        self
85    }
86
87    /// The number of hits to return.
88    ///
89    /// Defaults to `10`.
90    pub fn size(mut self, size: u64) -> Self {
91        self.size = Some(size);
92        self
93    }
94
95    /// A collection of sorting fields
96    pub fn sort<T>(mut self, sort: T) -> Self
97    where
98        T: IntoIterator,
99        T::Item: Into<Sort>,
100    {
101        self.sort.extend(sort);
102        self
103    }
104
105    /// Highlight
106    pub fn highlight<T>(mut self, highlight: T) -> Self
107    where
108        T: Into<Highlight>,
109    {
110        self.highlight = Some(highlight.into());
111        self
112    }
113
114    /// A collection of docvalue fields
115    pub fn docvalue_fields<T>(mut self, docvalue_fields: T) -> Self
116    where
117        T: IntoIterator,
118        T::Item: ToString,
119    {
120        self.docvalue_fields
121            .extend(docvalue_fields.into_iter().map(|x| x.to_string()));
122        self
123    }
124
125    /// A field to collapse by
126    pub fn collapse<T>(mut self, collapse: T) -> Self
127    where
128        T: ToString,
129    {
130        self.collapse = Some(InnerHitsCollapse {
131            field: collapse.to_string(),
132        });
133        self
134    }
135}