Debian Codegraph @

search_queries_has_parent.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 // HasParentQuery accepts a query and a parent type. The query is executed
8 // in the parent document space which is specified by the parent type.
9 // This query returns child documents which associated parents have matched.
10 // For the rest has_parent query has the same options and works in the
11 // same manner as has_child query.
12 //
13 // For more details, see
14 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-has-parent-query.html
15 type HasParentQuery struct {
16 query Query
17 parentType string
18 boost *float64
19 score *bool
20 queryName string
21 innerHit *InnerHit
22 ignoreUnmapped *bool
23 }
24
25 // NewHasParentQuery creates and initializes a new has_parent query.
26 func NewHasParentQuery(parentType string, query Query) *HasParentQuery {
27 return &HasParentQuery{
28 query: query,
29 parentType: parentType,
30 }
31 }
32
33 // Boost sets the boost for this query.
34 func (q *HasParentQuery) Boost(boost float64) *HasParentQuery {
35 q.boost = &boost
36 return q
37 }
38
39 // Score defines if the parent score is mapped into the child documents.
40 func (q *HasParentQuery) Score(score bool) *HasParentQuery {
41 q.score = &score
42 return q
43 }
44
45 // QueryName specifies the query name for the filter that can be used when
46 // searching for matched filters per hit.
47 func (q *HasParentQuery) QueryName(queryName string) *HasParentQuery {
48 q.queryName = queryName
49 return q
50 }
51
52 // InnerHit sets the inner hit definition in the scope of this query and
53 // reusing the defined type and query.
54 func (q *HasParentQuery) InnerHit(innerHit *InnerHit) *HasParentQuery {
55 q.innerHit = innerHit
56 return q
57 }
58
59 // IgnoreUnmapped specifies whether unmapped types should be ignored.
60 // If set to false, the query failes when an unmapped type is found.
61 func (q *HasParentQuery) IgnoreUnmapped(ignore bool) *HasParentQuery {
62 q.ignoreUnmapped = &ignore
63 return q
64 }
65
66 // Source returns JSON for the function score query.
67 func (q *HasParentQuery) Source() (interface{}, error) {
68 // {
69 // "has_parent" : {
70 // "parent_type" : "blog",
71 // "query" : {
72 // "term" : {
73 // "tag" : "something"
74 // }
75 // }
76 // }
77 // }
78 source := make(map[string]interface{})
79 query := make(map[string]interface{})
80 source["has_parent"] = query
81
82 src, err := q.query.Source()
83 if err != nil {
84 return nil, err
85 }
86 query["query"] = src
87 query["parent_type"] = q.parentType
88 if q.boost != nil {
89 query["boost"] = *q.boost
90 }
91 if q.score != nil {
92 query["score"] = *q.score
93 }
94 if q.queryName != "" {
95 query["_name"] = q.queryName
96 }
97 if q.innerHit != nil {
98 src, err := q.innerHit.Source()
99 if err != nil {
100 return nil, err
101 }
102 query["inner_hits"] = src
103 }
104 if q.ignoreUnmapped != nil {
105 query["ignore_unmapped"] = *q.ignoreUnmapped
106 }
107 return source, nil
108 }