Debian Codegraph @

search_queries_terms.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 // TermsQuery filters documents that have fields that match any
8 // of the provided terms (not analyzed).
9 //
10 // For more details, see
11 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-terms-query.html
12 type TermsQuery struct {
13 name string
14 values []interface{}
15 termsLookup *TermsLookup
16 queryName string
17 boost *float64
18 }
19
20 // NewTermsQuery creates and initializes a new TermsQuery.
21 func NewTermsQuery(name string, values ...interface{}) *TermsQuery {
22 q := &TermsQuery{
23 name: name,
24 values: make([]interface{}, 0),
25 }
26 if len(values) > 0 {
27 q.values = append(q.values, values...)
28 }
29 return q
30 }
31
32 // TermsLookup adds terms lookup details to the query.
33 func (q *TermsQuery) TermsLookup(lookup *TermsLookup) *TermsQuery {
34 q.termsLookup = lookup
35 return q
36 }
37
38 // Boost sets the boost for this query.
39 func (q *TermsQuery) Boost(boost float64) *TermsQuery {
40 q.boost = &boost
41 return q
42 }
43
44 // QueryName sets the query name for the filter that can be used
45 // when searching for matched_filters per hit
46 func (q *TermsQuery) QueryName(queryName string) *TermsQuery {
47 q.queryName = queryName
48 return q
49 }
50
51 // Creates the query source for the term query.
52 func (q *TermsQuery) Source() (interface{}, error) {
53 // {"terms":{"name":["value1","value2"]}}
54 source := make(map[string]interface{})
55 params := make(map[string]interface{})
56 source["terms"] = params
57
58 if q.termsLookup != nil {
59 src, err := q.termsLookup.Source()
60 if err != nil {
61 return nil, err
62 }
63 params[q.name] = src
64 } else {
65 params[q.name] = q.values
66 if q.boost != nil {
67 params["boost"] = *q.boost
68 }
69 if q.queryName != "" {
70 params["_name"] = q.queryName
71 }
72 }
73
74 return source, nil
75 }