|
| 1 | +package elastic |
| 2 | + |
| 3 | +var ( |
| 4 | + _ IntervalQueryRule = (*IntervalQueryFilter)(nil) |
| 5 | +) |
| 6 | + |
| 7 | +// IntervalQueryFilter specifies filters used in some |
| 8 | +// IntervalQueryRule implementations, e.g. IntervalQueryRuleAllOf. |
| 9 | +// |
| 10 | +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-intervals-query.html#interval_filter |
| 11 | +// for details. |
| 12 | +type IntervalQueryFilter struct { |
| 13 | + after IntervalQueryRule |
| 14 | + before IntervalQueryRule |
| 15 | + containedBy IntervalQueryRule |
| 16 | + containing IntervalQueryRule |
| 17 | + overlapping IntervalQueryRule |
| 18 | + notContainedBy IntervalQueryRule |
| 19 | + notContaining IntervalQueryRule |
| 20 | + notOverlapping IntervalQueryRule |
| 21 | + script *Script |
| 22 | +} |
| 23 | + |
| 24 | +// NewIntervalQueryFilter initializes and creates a new |
| 25 | +// IntervalQueryFilter. |
| 26 | +func NewIntervalQueryFilter() *IntervalQueryFilter { |
| 27 | + return &IntervalQueryFilter{} |
| 28 | +} |
| 29 | + |
| 30 | +// After specifies the query to be used to return intervals that follow |
| 31 | +// an interval from the filter rule. |
| 32 | +func (r *IntervalQueryFilter) After(after IntervalQueryRule) *IntervalQueryFilter { |
| 33 | + r.after = after |
| 34 | + return r |
| 35 | +} |
| 36 | + |
| 37 | +// Before specifies the query to be used to return intervals that occur |
| 38 | +// before an interval from the filter rule. |
| 39 | +func (r *IntervalQueryFilter) Before(before IntervalQueryRule) *IntervalQueryFilter { |
| 40 | + r.before = before |
| 41 | + return r |
| 42 | +} |
| 43 | + |
| 44 | +// ContainedBy specifies the query to be used to return intervals contained |
| 45 | +// by an interval from the filter rule. |
| 46 | +func (r *IntervalQueryFilter) ContainedBy(containedBy IntervalQueryRule) *IntervalQueryFilter { |
| 47 | + r.containedBy = containedBy |
| 48 | + return r |
| 49 | +} |
| 50 | + |
| 51 | +// Containing specifies the query to be used to return intervals that contain an |
| 52 | +// interval from the filter rule. |
| 53 | +func (r *IntervalQueryFilter) Containing(containing IntervalQueryRule) *IntervalQueryFilter { |
| 54 | + r.containing = containing |
| 55 | + return r |
| 56 | +} |
| 57 | + |
| 58 | +// Overlapping specifies the query to be used to return intervals that overlap |
| 59 | +// with an interval from the filter rule. |
| 60 | +func (r *IntervalQueryFilter) Overlapping(overlapping IntervalQueryRule) *IntervalQueryFilter { |
| 61 | + r.overlapping = overlapping |
| 62 | + return r |
| 63 | +} |
| 64 | + |
| 65 | +// NotContainedBy specifies the query to be used to return intervals that are NOT |
| 66 | +// contained by an interval from the filter rule. |
| 67 | +func (r *IntervalQueryFilter) NotContainedBy(notContainedBy IntervalQueryRule) *IntervalQueryFilter { |
| 68 | + r.notContainedBy = notContainedBy |
| 69 | + return r |
| 70 | +} |
| 71 | + |
| 72 | +// NotContaining specifies the query to be used to return intervals that do NOT |
| 73 | +// contain an interval from the filter rule. |
| 74 | +func (r *IntervalQueryFilter) NotContaining(notContaining IntervalQueryRule) *IntervalQueryFilter { |
| 75 | + r.notContaining = notContaining |
| 76 | + return r |
| 77 | +} |
| 78 | + |
| 79 | +// NotOverlapping specifies the query to be used to return intervals that do NOT |
| 80 | +// overlap with an interval from the filter rule. |
| 81 | +func (r *IntervalQueryFilter) NotOverlapping(notOverlapping IntervalQueryRule) *IntervalQueryFilter { |
| 82 | + r.notOverlapping = notOverlapping |
| 83 | + return r |
| 84 | +} |
| 85 | + |
| 86 | +// Script allows a script to be used to return matching documents. The script |
| 87 | +// must return a boolean value, true or false. |
| 88 | +func (r *IntervalQueryFilter) Script(script *Script) *IntervalQueryFilter { |
| 89 | + r.script = script |
| 90 | + return r |
| 91 | +} |
| 92 | + |
| 93 | +// Source returns JSON for the function score query. |
| 94 | +func (r *IntervalQueryFilter) Source() (interface{}, error) { |
| 95 | + source := make(map[string]interface{}) |
| 96 | + |
| 97 | + if r.before != nil { |
| 98 | + src, err := r.before.Source() |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + source["before"] = src |
| 103 | + } |
| 104 | + |
| 105 | + if r.after != nil { |
| 106 | + src, err := r.after.Source() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + source["after"] = src |
| 111 | + } |
| 112 | + |
| 113 | + if r.containedBy != nil { |
| 114 | + src, err := r.containedBy.Source() |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + source["contained_by"] = src |
| 119 | + } |
| 120 | + |
| 121 | + if r.containing != nil { |
| 122 | + src, err := r.containing.Source() |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + source["containing"] = src |
| 127 | + } |
| 128 | + |
| 129 | + if r.overlapping != nil { |
| 130 | + src, err := r.overlapping.Source() |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + source["overlapping"] = src |
| 135 | + } |
| 136 | + |
| 137 | + if r.notContainedBy != nil { |
| 138 | + src, err := r.notContainedBy.Source() |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + source["not_contained_by"] = src |
| 143 | + } |
| 144 | + |
| 145 | + if r.notContaining != nil { |
| 146 | + src, err := r.notContaining.Source() |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + source["not_containing"] = src |
| 151 | + } |
| 152 | + |
| 153 | + if r.notOverlapping != nil { |
| 154 | + src, err := r.notOverlapping.Source() |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + source["not_overlapping"] = src |
| 159 | + } |
| 160 | + |
| 161 | + if r.script != nil { |
| 162 | + src, err := r.script.Source() |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + source["script"] = src |
| 167 | + } |
| 168 | + |
| 169 | + return source, nil |
| 170 | +} |
| 171 | + |
| 172 | +// isIntervalQueryRule implements the marker interface. |
| 173 | +func (r *IntervalQueryFilter) isIntervalQueryRule() bool { |
| 174 | + return true |
| 175 | +} |
0 commit comments