| 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 |
// ParentIdQuery can be used to find child documents which belong to a |
| 8 |
// particular parent. Given the following mapping definition. |
| 9 |
// |
| 10 |
// For more details, see |
| 11 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-parent-id-query.html |
| 12 |
type ParentIdQuery struct { |
| 13 |
typ string |
| 14 |
id string |
| 15 |
ignoreUnmapped *bool |
| 16 |
boost *float64 |
| 17 |
queryName string |
| 18 |
innerHit *InnerHit |
| 19 |
} |
| 20 |
|
| 21 |
// NewParentIdQuery creates and initializes a new parent_id query. |
| 22 |
func NewParentIdQuery(typ, id string) *ParentIdQuery { |
| 23 |
return &ParentIdQuery{ |
| 24 |
typ: typ, |
| 25 |
id: id, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// Type sets the parent type. |
| 30 |
func (q *ParentIdQuery) Type(typ string) *ParentIdQuery { |
| 31 |
q.typ = typ |
| 32 |
return q |
| 33 |
} |
| 34 |
|
| 35 |
// Id sets the id. |
| 36 |
func (q *ParentIdQuery) Id(id string) *ParentIdQuery { |
| 37 |
q.id = id |
| 38 |
return q |
| 39 |
} |
| 40 |
|
| 41 |
// IgnoreUnmapped specifies whether unmapped types should be ignored. |
| 42 |
// If set to false, the query failes when an unmapped type is found. |
| 43 |
func (q *ParentIdQuery) IgnoreUnmapped(ignore bool) *ParentIdQuery { |
| 44 |
q.ignoreUnmapped = &ignore |
| 45 |
return q |
| 46 |
} |
| 47 |
|
| 48 |
// Boost sets the boost for this query. |
| 49 |
func (q *ParentIdQuery) Boost(boost float64) *ParentIdQuery { |
| 50 |
q.boost = &boost |
| 51 |
return q |
| 52 |
} |
| 53 |
|
| 54 |
// QueryName specifies the query name for the filter that can be used when |
| 55 |
// searching for matched filters per hit. |
| 56 |
func (q *ParentIdQuery) QueryName(queryName string) *ParentIdQuery { |
| 57 |
q.queryName = queryName |
| 58 |
return q |
| 59 |
} |
| 60 |
|
| 61 |
// InnerHit sets the inner hit definition in the scope of this query and |
| 62 |
// reusing the defined type and query. |
| 63 |
func (q *ParentIdQuery) InnerHit(innerHit *InnerHit) *ParentIdQuery { |
| 64 |
q.innerHit = innerHit |
| 65 |
return q |
| 66 |
} |
| 67 |
|
| 68 |
// Source returns JSON for the parent_id query. |
| 69 |
func (q *ParentIdQuery) Source() (interface{}, error) { |
| 70 |
// { |
| 71 |
// "parent_id" : { |
| 72 |
// "type" : "blog", |
| 73 |
// "id" : "1" |
| 74 |
// } |
| 75 |
// } |
| 76 |
source := make(map[string]interface{}) |
| 77 |
query := make(map[string]interface{}) |
| 78 |
source["parent_id"] = query |
| 79 |
|
| 80 |
query["type"] = q.typ |
| 81 |
query["id"] = q.id |
| 82 |
if q.boost != nil { |
| 83 |
query["boost"] = *q.boost |
| 84 |
} |
| 85 |
if q.ignoreUnmapped != nil { |
| 86 |
query["ignore_unmapped"] = *q.ignoreUnmapped |
| 87 |
} |
| 88 |
if q.queryName != "" { |
| 89 |
query["_name"] = q.queryName |
| 90 |
} |
| 91 |
if q.innerHit != nil { |
| 92 |
src, err := q.innerHit.Source() |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
query["inner_hits"] = src |
| 97 |
} |
| 98 |
return source, nil |
| 99 |
} |