| 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 |
// TermQuery finds documents that contain the exact term specified |
| 8 |
// in the inverted index. |
| 9 |
// |
| 10 |
// For details, see |
| 11 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-term-query.html |
| 12 |
type TermQuery struct { |
| 13 |
name string |
| 14 |
value interface{} |
| 15 |
boost *float64 |
| 16 |
queryName string |
| 17 |
} |
| 18 |
|
| 19 |
// NewTermQuery creates and initializes a new TermQuery. |
| 20 |
func NewTermQuery(name string, value interface{}) *TermQuery { |
| 21 |
return &TermQuery{name: name, value: value} |
| 22 |
} |
| 23 |
|
| 24 |
// Boost sets the boost for this query. |
| 25 |
func (q *TermQuery) Boost(boost float64) *TermQuery { |
| 26 |
q.boost = &boost |
| 27 |
return q |
| 28 |
} |
| 29 |
|
| 30 |
// QueryName sets the query name for the filter that can be used |
| 31 |
// when searching for matched_filters per hit |
| 32 |
func (q *TermQuery) QueryName(queryName string) *TermQuery { |
| 33 |
q.queryName = queryName |
| 34 |
return q |
| 35 |
} |
| 36 |
|
| 37 |
// Source returns JSON for the query. |
| 38 |
func (q *TermQuery) Source() (interface{}, error) { |
| 39 |
// {"term":{"name":"value"}} |
| 40 |
source := make(map[string]interface{}) |
| 41 |
tq := make(map[string]interface{}) |
| 42 |
source["term"] = tq |
| 43 |
|
| 44 |
if q.boost == nil && q.queryName == "" { |
| 45 |
tq[q.name] = q.value |
| 46 |
} else { |
| 47 |
subQ := make(map[string]interface{}) |
| 48 |
subQ["value"] = q.value |
| 49 |
if q.boost != nil { |
| 50 |
subQ["boost"] = *q.boost |
| 51 |
} |
| 52 |
if q.queryName != "" { |
| 53 |
subQ["_name"] = q.queryName |
| 54 |
} |
| 55 |
tq[q.name] = subQ |
| 56 |
} |
| 57 |
return source, nil |
| 58 |
} |