forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_queries_has_child.go
131 lines (119 loc) · 3.61 KB
/
search_queries_has_child.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// HasChildQuery accepts a query and the child type to run against, and results
// in parent documents that have child docs matching the query.
//
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-has-child-query.html
type HasChildQuery struct {
query Query
childType string
boost *float64
scoreMode string
minChildren *int
maxChildren *int
shortCircuitCutoff *int
queryName string
innerHit *InnerHit
}
// NewHasChildQuery creates and initializes a new has_child query.
func NewHasChildQuery(childType string, query Query) *HasChildQuery {
return &HasChildQuery{
query: query,
childType: childType,
}
}
// Boost sets the boost for this query.
func (q *HasChildQuery) Boost(boost float64) *HasChildQuery {
q.boost = &boost
return q
}
// ScoreMode defines how the scores from the matching child documents
// are mapped into the parent document. Allowed values are: min, max,
// avg, or none.
func (q *HasChildQuery) ScoreMode(scoreMode string) *HasChildQuery {
q.scoreMode = scoreMode
return q
}
// MinChildren defines the minimum number of children that are required
// to match for the parent to be considered a match.
func (q *HasChildQuery) MinChildren(minChildren int) *HasChildQuery {
q.minChildren = &minChildren
return q
}
// MaxChildren defines the maximum number of children that are required
// to match for the parent to be considered a match.
func (q *HasChildQuery) MaxChildren(maxChildren int) *HasChildQuery {
q.maxChildren = &maxChildren
return q
}
// ShortCircuitCutoff configures what cut off point only to evaluate
// parent documents that contain the matching parent id terms instead
// of evaluating all parent docs.
func (q *HasChildQuery) ShortCircuitCutoff(shortCircuitCutoff int) *HasChildQuery {
q.shortCircuitCutoff = &shortCircuitCutoff
return q
}
// QueryName specifies the query name for the filter that can be used when
// searching for matched filters per hit.
func (q *HasChildQuery) QueryName(queryName string) *HasChildQuery {
q.queryName = queryName
return q
}
// InnerHit sets the inner hit definition in the scope of this query and
// reusing the defined type and query.
func (q *HasChildQuery) InnerHit(innerHit *InnerHit) *HasChildQuery {
q.innerHit = innerHit
return q
}
// Source returns JSON for the function score query.
func (q *HasChildQuery) Source() (interface{}, error) {
// {
// "has_child" : {
// "type" : "blog_tag",
// "score_mode" : "min",
// "query" : {
// "term" : {
// "tag" : "something"
// }
// }
// }
// }
source := make(map[string]interface{})
query := make(map[string]interface{})
source["has_child"] = query
src, err := q.query.Source()
if err != nil {
return nil, err
}
query["query"] = src
query["type"] = q.childType
if q.boost != nil {
query["boost"] = *q.boost
}
if q.scoreMode != "" {
query["score_mode"] = q.scoreMode
}
if q.minChildren != nil {
query["min_children"] = *q.minChildren
}
if q.maxChildren != nil {
query["max_children"] = *q.maxChildren
}
if q.shortCircuitCutoff != nil {
query["short_circuit_cutoff"] = *q.shortCircuitCutoff
}
if q.queryName != "" {
query["_name"] = q.queryName
}
if q.innerHit != nil {
src, err := q.innerHit.Source()
if err != nil {
return nil, err
}
query["inner_hits"] = src
}
return source, nil
}