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
|
// 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
// Nested query allows to query nested objects / docs (see nested mapping).
// The query is executed against the nested objects / docs as if they were
// indexed as separate docs (they are, internally) and resulting in the
// root parent doc (or parent nested mapping).
//
// For more details, see:
// http://www.elasticsearch.org/guide/reference/query-dsl/nested-query/
type NestedQuery struct {
query Query
filter Filter
path string
scoreMode string
boost *float32
queryName string
innerHit *InnerHit
}
// Creates a new nested_query query.
func NewNestedQuery(path string) NestedQuery {
return NestedQuery{path: path}
}
func (q NestedQuery) Query(query Query) NestedQuery {
q.query = query
return q
}
func (q NestedQuery) Filter(filter Filter) NestedQuery {
q.filter = filter
return q
}
func (q NestedQuery) Path(path string) NestedQuery {
q.path = path
return q
}
func (q NestedQuery) ScoreMode(scoreMode string) NestedQuery {
q.scoreMode = scoreMode
return q
}
func (q NestedQuery) Boost(boost float32) NestedQuery {
q.boost = &boost
return q
}
func (q NestedQuery) QueryName(queryName string) NestedQuery {
q.queryName = queryName
return q
}
func (q NestedQuery) InnerHit(innerHit *InnerHit) NestedQuery {
q.innerHit = innerHit
return q
}
// Creates the query source for the nested_query query.
func (q NestedQuery) Source() interface{} {
// {
// "nested" : {
// "query" : {
// "bool" : {
// "must" : [
// {
// "match" : {"obj1.name" : "blue"}
// },
// {
// "range" : {"obj1.count" : {"gt" : 5}}
// }
// ]
// }
// },
// "filter" : {
// ...
// },
// "path" : "obj1",
// "score_mode" : "avg",
// "boost" : 1.0
// }
// }
query := make(map[string]interface{})
nq := make(map[string]interface{})
query["nested"] = nq
if q.query != nil {
nq["query"] = q.query.Source()
}
if q.filter != nil {
nq["filter"] = q.filter.Source()
}
nq["path"] = q.path
if q.scoreMode != "" {
nq["score_mode"] = q.scoreMode
}
if q.boost != nil {
nq["boost"] = *q.boost
}
if q.queryName != "" {
nq["_name"] = q.queryName
}
if q.innerHit != nil {
nq["inner_hits"] = q.innerHit.Source()
}
return query
}
|