| 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 |
// RangeQuery matches documents with fields that have terms within a certain range. |
| 8 |
// |
| 9 |
// For details, see |
| 10 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-range-query.html |
| 11 |
type RangeQuery struct { |
| 12 |
name string |
| 13 |
from interface{} |
| 14 |
to interface{} |
| 15 |
timeZone string |
| 16 |
includeLower bool |
| 17 |
includeUpper bool |
| 18 |
boost *float64 |
| 19 |
queryName string |
| 20 |
format string |
| 21 |
relation string |
| 22 |
} |
| 23 |
|
| 24 |
// NewRangeQuery creates and initializes a new RangeQuery. |
| 25 |
func NewRangeQuery(name string) *RangeQuery { |
| 26 |
return &RangeQuery{name: name, includeLower: true, includeUpper: true} |
| 27 |
} |
| 28 |
|
| 29 |
// From indicates the from part of the RangeQuery. |
| 30 |
// Use nil to indicate an unbounded from part. |
| 31 |
func (q *RangeQuery) From(from interface{}) *RangeQuery { |
| 32 |
q.from = from |
| 33 |
return q |
| 34 |
} |
| 35 |
|
| 36 |
// Gt indicates a greater-than value for the from part. |
| 37 |
// Use nil to indicate an unbounded from part. |
| 38 |
func (q *RangeQuery) Gt(from interface{}) *RangeQuery { |
| 39 |
q.from = from |
| 40 |
q.includeLower = false |
| 41 |
return q |
| 42 |
} |
| 43 |
|
| 44 |
// Gte indicates a greater-than-or-equal value for the from part. |
| 45 |
// Use nil to indicate an unbounded from part. |
| 46 |
func (q *RangeQuery) Gte(from interface{}) *RangeQuery { |
| 47 |
q.from = from |
| 48 |
q.includeLower = true |
| 49 |
return q |
| 50 |
} |
| 51 |
|
| 52 |
// To indicates the to part of the RangeQuery. |
| 53 |
// Use nil to indicate an unbounded to part. |
| 54 |
func (q *RangeQuery) To(to interface{}) *RangeQuery { |
| 55 |
q.to = to |
| 56 |
return q |
| 57 |
} |
| 58 |
|
| 59 |
// Lt indicates a less-than value for the to part. |
| 60 |
// Use nil to indicate an unbounded to part. |
| 61 |
func (q *RangeQuery) Lt(to interface{}) *RangeQuery { |
| 62 |
q.to = to |
| 63 |
q.includeUpper = false |
| 64 |
return q |
| 65 |
} |
| 66 |
|
| 67 |
// Lte indicates a less-than-or-equal value for the to part. |
| 68 |
// Use nil to indicate an unbounded to part. |
| 69 |
func (q *RangeQuery) Lte(to interface{}) *RangeQuery { |
| 70 |
q.to = to |
| 71 |
q.includeUpper = true |
| 72 |
return q |
| 73 |
} |
| 74 |
|
| 75 |
// IncludeLower indicates whether the lower bound should be included or not. |
| 76 |
// Defaults to true. |
| 77 |
func (q *RangeQuery) IncludeLower(includeLower bool) *RangeQuery { |
| 78 |
q.includeLower = includeLower |
| 79 |
return q |
| 80 |
} |
| 81 |
|
| 82 |
// IncludeUpper indicates whether the upper bound should be included or not. |
| 83 |
// Defaults to true. |
| 84 |
func (q *RangeQuery) IncludeUpper(includeUpper bool) *RangeQuery { |
| 85 |
q.includeUpper = includeUpper |
| 86 |
return q |
| 87 |
} |
| 88 |
|
| 89 |
// Boost sets the boost for this query. |
| 90 |
func (q *RangeQuery) Boost(boost float64) *RangeQuery { |
| 91 |
q.boost = &boost |
| 92 |
return q |
| 93 |
} |
| 94 |
|
| 95 |
// QueryName sets the query name for the filter that can be used when |
| 96 |
// searching for matched_filters per hit. |
| 97 |
func (q *RangeQuery) QueryName(queryName string) *RangeQuery { |
| 98 |
q.queryName = queryName |
| 99 |
return q |
| 100 |
} |
| 101 |
|
| 102 |
// TimeZone is used for date fields. In that case, we can adjust the |
| 103 |
// from/to fields using a timezone. |
| 104 |
func (q *RangeQuery) TimeZone(timeZone string) *RangeQuery { |
| 105 |
q.timeZone = timeZone |
| 106 |
return q |
| 107 |
} |
| 108 |
|
| 109 |
// Format is used for date fields. In that case, we can set the format |
| 110 |
// to be used instead of the mapper format. |
| 111 |
func (q *RangeQuery) Format(format string) *RangeQuery { |
| 112 |
q.format = format |
| 113 |
return q |
| 114 |
} |
| 115 |
|
| 116 |
// Relation is used for range fields. which can be one of |
| 117 |
// "within", "contains", "intersects" (default) and "disjoint". |
| 118 |
func (q *RangeQuery) Relation(relation string) *RangeQuery { |
| 119 |
q.relation = relation |
| 120 |
return q |
| 121 |
} |
| 122 |
|
| 123 |
// Source returns JSON for the query. |
| 124 |
func (q *RangeQuery) Source() (interface{}, error) { |
| 125 |
source := make(map[string]interface{}) |
| 126 |
|
| 127 |
rangeQ := make(map[string]interface{}) |
| 128 |
source["range"] = rangeQ |
| 129 |
|
| 130 |
params := make(map[string]interface{}) |
| 131 |
rangeQ[q.name] = params |
| 132 |
|
| 133 |
params["from"] = q.from |
| 134 |
params["to"] = q.to |
| 135 |
if q.timeZone != "" { |
| 136 |
params["time_zone"] = q.timeZone |
| 137 |
} |
| 138 |
if q.format != "" { |
| 139 |
params["format"] = q.format |
| 140 |
} |
| 141 |
if q.relation != "" { |
| 142 |
params["relation"] = q.relation |
| 143 |
} |
| 144 |
if q.boost != nil { |
| 145 |
params["boost"] = *q.boost |
| 146 |
} |
| 147 |
params["include_lower"] = q.includeLower |
| 148 |
params["include_upper"] = q.includeUpper |
| 149 |
|
| 150 |
if q.queryName != "" { |
| 151 |
rangeQ["_name"] = q.queryName |
| 152 |
} |
| 153 |
|
| 154 |
return source, nil |
| 155 |
} |