Debian Codegraph @

search_queries_nested.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 // NestedQuery allows to query nested objects / docs.
8 // The query is executed against the nested objects / docs as if they were
9 // indexed as separate docs (they are, internally) and resulting in the
10 // root parent doc (or parent nested mapping).
11 //
12 // For more details, see
13 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-nested-query.html
14 type NestedQuery struct {
15 query Query
16 path string
17 scoreMode string
18 boost *float64
19 queryName string
20 innerHit *InnerHit
21 ignoreUnmapped *bool
22 }
23
24 // NewNestedQuery creates and initializes a new NestedQuery.
25 func NewNestedQuery(path string, query Query) *NestedQuery {
26 return &NestedQuery{path: path, query: query}
27 }
28
29 // ScoreMode specifies the score mode.
30 func (q *NestedQuery) ScoreMode(scoreMode string) *NestedQuery {
31 q.scoreMode = scoreMode
32 return q
33 }
34
35 // Boost sets the boost for this query.
36 func (q *NestedQuery) Boost(boost float64) *NestedQuery {
37 q.boost = &boost
38 return q
39 }
40
41 // QueryName sets the query name for the filter that can be used
42 // when searching for matched_filters per hit
43 func (q *NestedQuery) QueryName(queryName string) *NestedQuery {
44 q.queryName = queryName
45 return q
46 }
47
48 // InnerHit sets the inner hit definition in the scope of this nested query
49 // and reusing the defined path and query.
50 func (q *NestedQuery) InnerHit(innerHit *InnerHit) *NestedQuery {
51 q.innerHit = innerHit
52 return q
53 }
54
55 // IgnoreUnmapped sets the ignore_unmapped option for the filter that ignores
56 // unmapped nested fields
57 func (q *NestedQuery) IgnoreUnmapped(value bool) *NestedQuery {
58 q.ignoreUnmapped = &value
59 return q
60 }
61
62 // Source returns JSON for the query.
63 func (q *NestedQuery) Source() (interface{}, error) {
64 query := make(map[string]interface{})
65 nq := make(map[string]interface{})
66 query["nested"] = nq
67
68 src, err := q.query.Source()
69 if err != nil {
70 return nil, err
71 }
72 nq["query"] = src
73
74 nq["path"] = q.path
75
76 if q.scoreMode != "" {
77 nq["score_mode"] = q.scoreMode
78 }
79 if q.boost != nil {
80 nq["boost"] = *q.boost
81 }
82 if q.queryName != "" {
83 nq["_name"] = q.queryName
84 }
85 if q.ignoreUnmapped != nil {
86 nq["ignore_unmapped"] = *q.ignoreUnmapped
87 }
88 if q.innerHit != nil {
89 src, err := q.innerHit.Source()
90 if err != nil {
91 return nil, err
92 }
93 nq["inner_hits"] = src
94 }
95 return query, nil
96 }