File: search_queries_has_child.go

package info (click to toggle)
golang-gopkg-olivere-elastic.v3 3.0.41-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,364 kB
  • sloc: makefile: 16
file content (129 lines) | stat: -rw-r--r-- 3,616 bytes parent folder | download | duplicates (2)
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
// Copyright 2012-2015 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/current/query-dsl-has-child-query.html
type HasChildQuery struct {
	query              Query
	childType          string
	boost              *float64
	scoreType          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
}

// ScoreType defines how the scores from the matching child documents
// are mapped into the parent document.
func (q *HasChildQuery) ScoreType(scoreType string) *HasChildQuery {
	q.scoreType = scoreType
	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",
	//       "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.scoreType != "" {
		query["score_type"] = q.scoreType
	}
	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
}