From d6904ca1212f31d6f84117e3eeb38ce34c62dac1 Mon Sep 17 00:00:00 2001 From: Evaldas Buinauskas Date: Fri, 8 Sep 2023 12:10:27 +0300 Subject: [PATCH] Implement inner hits for has child query --- src/search/queries/joining/has_child_query.rs | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/search/queries/joining/has_child_query.rs b/src/search/queries/joining/has_child_query.rs index 239e11b..228dd6e 100644 --- a/src/search/queries/joining/has_child_query.rs +++ b/src/search/queries/joining/has_child_query.rs @@ -31,6 +31,9 @@ pub struct HasChildQuery { #[serde(skip_serializing_if = "ShouldSkip::should_skip")] score_mode: Option, + #[serde(skip_serializing_if = "ShouldSkip::should_skip")] + inner_hits: Option>, + #[serde(skip_serializing_if = "ShouldSkip::should_skip")] boost: Option, @@ -56,6 +59,7 @@ impl Query { max_children: None, min_children: None, score_mode: None, + inner_hits: None, boost: None, _name: None, } @@ -96,6 +100,29 @@ impl HasChildQuery { self } + /// The [parent-join](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html) + /// and [nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) + /// features allow the return of documents that have matches in a different scope. In the + /// parent/child case, parent documents are returned based on matches in child documents or + /// child documents are returned based on matches in parent documents. In the nested case, + /// documents are returned based on matches in nested inner objects. + /// + /// In both cases, the actual matches in the different scopes that caused a document to be + /// returned are hidden. In many cases, it’s very useful to know which inner nested objects + /// (in the case of nested) or children/parent documents (in the case of parent/child) caused + /// certain information to be returned. The inner hits feature can be used for this. This + /// feature returns per search hit in the search response additional nested hits that caused a + /// search hit to match in a different scope. + /// + /// Inner hits can be used by defining an `inner_hits` definition on a `nested`, `has_child` + /// or `has_parent` query and filter. + /// + /// + pub fn inner_hits(mut self, inner_hits: InnerHits) -> Self { + self.inner_hits = Some(Box::new(inner_hits)); + self + } + add_boost_and_name!(); } @@ -134,8 +161,9 @@ mod tests { .boost(2) .name("test") .ignore_unmapped(true) - .max_children(3u32) - .min_children(2u32) + .max_children(3) + .min_children(2) + .inner_hits(InnerHits::new()) .score_mode(HasChildScoreMode::Max), json!({ "has_child": { @@ -151,6 +179,7 @@ mod tests { } } }, + "inner_hits": {}, "boost": 2.0, "_name": "test" }