Skip to content

Commit cae992f

Browse files
committedJan 4, 2020
Merge branch 'feature/query-interval' of git://github.com/larrycinnabar/elastic into larrycinnabar-feature/query-interval
2 parents 6f3588e + fba31d1 commit cae992f

7 files changed

+453
-0
lines changed
 

‎search_queries_interval.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// IntervalQueryRule represents the generic matching interval rule interface. Interval Rule is actually
8+
// just a Query, but may be used only inside IntervalQuery. An extra method is added
9+
// just to separate all its implementations (*Rule objects) from other query objects
10+
type IntervalQueryRule interface {
11+
Query
12+
13+
// isIntervalQueryRule is never actually called, and is used just for Rule to differ from standard Query
14+
isIntervalQueryRule() bool
15+
}
16+
17+
// IntervalQuery returns documents based on the order and proximity of matching terms
18+
//
19+
// For more details, see
20+
// https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-intervals-query.html
21+
type IntervalQuery struct {
22+
field string
23+
rule IntervalQueryRule
24+
}
25+
26+
// NewIntervalQuery creates and initializes a new IntervalQuery.
27+
func NewIntervalQuery(field string, rule IntervalQueryRule) *IntervalQuery {
28+
return &IntervalQuery{field: field, rule: rule}
29+
}
30+
31+
// Source returns JSON for the function score query.
32+
func (q *IntervalQuery) Source() (interface{}, error) {
33+
source := make(map[string]interface{})
34+
35+
ruleSrc, err := q.rule.Source()
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
source[q.field] = ruleSrc
41+
42+
return source, nil
43+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package elastic
2+
3+
type IntervalQueryRuleAllOf struct {
4+
intervals []IntervalQueryRule
5+
maxGaps *int
6+
ordered *bool
7+
filter *IntervalQueryRuleFilter
8+
}
9+
10+
var _ IntervalQueryRule = &IntervalQueryRuleAllOf{}
11+
12+
func NewIntervalQueryRuleAllOf(intervals ...IntervalQueryRule) *IntervalQueryRuleAllOf {
13+
return &IntervalQueryRuleAllOf{intervals: intervals}
14+
}
15+
16+
func (r *IntervalQueryRuleAllOf) MaxGaps(maxGaps int) *IntervalQueryRuleAllOf {
17+
r.maxGaps = &maxGaps
18+
return r
19+
}
20+
21+
func (r *IntervalQueryRuleAllOf) Ordered(ordered bool) *IntervalQueryRuleAllOf {
22+
r.ordered = &ordered
23+
return r
24+
}
25+
26+
func (r *IntervalQueryRuleAllOf) Filter(filter *IntervalQueryRuleFilter) *IntervalQueryRuleAllOf {
27+
r.filter = filter
28+
return r
29+
}
30+
31+
// Source returns JSON for the function score query.
32+
func (r *IntervalQueryRuleAllOf) Source() (interface{}, error) {
33+
source := make(map[string]interface{})
34+
35+
intervalSources := make([]interface{}, 0)
36+
for _, interval := range r.intervals {
37+
src, err := interval.Source()
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
intervalSources = append(intervalSources, src)
43+
}
44+
source["intervals"] = intervalSources
45+
46+
if r.ordered != nil {
47+
source["ordered"] = *r.ordered
48+
}
49+
if r.maxGaps != nil {
50+
source["max_gaps"] = *r.maxGaps
51+
}
52+
if r.filter != nil {
53+
src, err := r.filter.Source()
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
source["filter"] = src
59+
}
60+
61+
return map[string]interface{}{"all_of": source}, nil
62+
}
63+
64+
func (r *IntervalQueryRuleAllOf) isIntervalQueryRule() bool {
65+
return true
66+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package elastic
2+
3+
type IntervalQueryRuleAnyOf struct {
4+
intervals []IntervalQueryRule
5+
filter *IntervalQueryRuleFilter
6+
}
7+
8+
var _ IntervalQueryRule = &IntervalQueryRuleAnyOf{}
9+
10+
func NewIntervalQueryRuleAnyOf(intervals ...IntervalQueryRule) *IntervalQueryRuleAnyOf {
11+
return &IntervalQueryRuleAnyOf{intervals: intervals}
12+
}
13+
func (r *IntervalQueryRuleAnyOf) Filter(filter *IntervalQueryRuleFilter) *IntervalQueryRuleAnyOf {
14+
r.filter = filter
15+
return r
16+
}
17+
18+
// Source returns JSON for the function score query.
19+
func (r *IntervalQueryRuleAnyOf) Source() (interface{}, error) {
20+
source := make(map[string]interface{})
21+
22+
intervalSources := make([]interface{}, 0)
23+
for _, interval := range r.intervals {
24+
src, err := interval.Source()
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
intervalSources = append(intervalSources, src)
30+
}
31+
source["intervals"] = intervalSources
32+
33+
if r.filter != nil {
34+
src, err := r.filter.Source()
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
source["filter"] = src
40+
}
41+
42+
return map[string]interface{}{"any_of": source}, nil
43+
}
44+
45+
func (r *IntervalQueryRuleAnyOf) isIntervalQueryRule() bool {
46+
return true
47+
}
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package elastic
2+
3+
type IntervalQueryRuleFilter struct {
4+
after Query
5+
before Query
6+
containedBy Query
7+
containing Query
8+
overlapping Query
9+
notContainedBy Query
10+
notContaining Query
11+
notOverlapping Query
12+
script *Script
13+
}
14+
15+
var _ IntervalQueryRule = &IntervalQueryRuleFilter{}
16+
17+
func NewIntervalQueryRuleFilter() *IntervalQueryRuleFilter {
18+
return &IntervalQueryRuleFilter{}
19+
}
20+
21+
func (r *IntervalQueryRuleFilter) After(after Query) *IntervalQueryRuleFilter {
22+
r.after = after
23+
return r
24+
}
25+
func (r *IntervalQueryRuleFilter) Before(before Query) *IntervalQueryRuleFilter {
26+
r.before = before
27+
return r
28+
}
29+
func (r *IntervalQueryRuleFilter) ContainedBy(containedBy Query) *IntervalQueryRuleFilter {
30+
r.containedBy = containedBy
31+
return r
32+
}
33+
func (r *IntervalQueryRuleFilter) Containing(containing Query) *IntervalQueryRuleFilter {
34+
r.containing = containing
35+
return r
36+
}
37+
func (r *IntervalQueryRuleFilter) Overlapping(overlapping Query) *IntervalQueryRuleFilter {
38+
r.overlapping = overlapping
39+
return r
40+
}
41+
func (r *IntervalQueryRuleFilter) NotContainedBy(notContainedBy Query) *IntervalQueryRuleFilter {
42+
r.notContainedBy = notContainedBy
43+
return r
44+
}
45+
func (r *IntervalQueryRuleFilter) NotContaining(notContaining Query) *IntervalQueryRuleFilter {
46+
r.notContaining = notContaining
47+
return r
48+
}
49+
func (r *IntervalQueryRuleFilter) NotOverlapping(notOverlapping Query) *IntervalQueryRuleFilter {
50+
r.notOverlapping = notOverlapping
51+
return r
52+
}
53+
func (r *IntervalQueryRuleFilter) Script(script *Script) *IntervalQueryRuleFilter {
54+
r.script = script
55+
return r
56+
}
57+
58+
// Source returns JSON for the function score query.
59+
func (r *IntervalQueryRuleFilter) Source() (interface{}, error) {
60+
source := make(map[string]interface{})
61+
62+
if r.before != nil {
63+
src, err := r.before.Source()
64+
if err != nil {
65+
return nil, err
66+
}
67+
source["before"] = src
68+
}
69+
70+
if r.after != nil {
71+
src, err := r.after.Source()
72+
if err != nil {
73+
return nil, err
74+
}
75+
source["after"] = src
76+
}
77+
78+
if r.containedBy != nil {
79+
src, err := r.containedBy.Source()
80+
if err != nil {
81+
return nil, err
82+
}
83+
source["contained_by"] = src
84+
}
85+
86+
if r.containing != nil {
87+
src, err := r.containing.Source()
88+
if err != nil {
89+
return nil, err
90+
}
91+
source["containing"] = src
92+
}
93+
94+
if r.overlapping != nil {
95+
src, err := r.overlapping.Source()
96+
if err != nil {
97+
return nil, err
98+
}
99+
source["overlapping"] = src
100+
}
101+
102+
if r.notContainedBy != nil {
103+
src, err := r.notContainedBy.Source()
104+
if err != nil {
105+
return nil, err
106+
}
107+
source["not_contained_by"] = src
108+
}
109+
110+
if r.notContaining != nil {
111+
src, err := r.notContaining.Source()
112+
if err != nil {
113+
return nil, err
114+
}
115+
source["not_containing"] = src
116+
}
117+
118+
if r.notOverlapping != nil {
119+
src, err := r.notOverlapping.Source()
120+
if err != nil {
121+
return nil, err
122+
}
123+
source["not_overlapping"] = src
124+
}
125+
126+
if r.script != nil {
127+
src, err := r.script.Source()
128+
if err != nil {
129+
return nil, err
130+
}
131+
source["script"] = src
132+
}
133+
134+
// todo: not so clear from docs, if filter can be a top-level rule, and so
135+
// if it does need a wrapper map[string]interface{}{"filter": ..} like other rules do
136+
137+
return source, nil
138+
}
139+
140+
func (r *IntervalQueryRuleFilter) isIntervalQueryRule() bool {
141+
return true
142+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package elastic
2+
3+
type IntervalQueryRuleMatch struct {
4+
query string
5+
maxGaps *int
6+
ordered *bool
7+
analyzer string
8+
useField string
9+
filter *IntervalQueryRuleFilter
10+
}
11+
12+
var _ IntervalQueryRule = &IntervalQueryRuleMatch{}
13+
14+
func NewIntervalQueryRuleMatch(query string) *IntervalQueryRuleMatch {
15+
return &IntervalQueryRuleMatch{query: query}
16+
}
17+
18+
func (r *IntervalQueryRuleMatch) MaxGaps(maxGaps int) *IntervalQueryRuleMatch {
19+
r.maxGaps = &maxGaps
20+
return r
21+
}
22+
func (r *IntervalQueryRuleMatch) Ordered(ordered bool) *IntervalQueryRuleMatch {
23+
r.ordered = &ordered
24+
return r
25+
}
26+
func (r *IntervalQueryRuleMatch) Analyzer(analyzer string) *IntervalQueryRuleMatch {
27+
r.analyzer = analyzer
28+
return r
29+
}
30+
func (r *IntervalQueryRuleMatch) UseField(useField string) *IntervalQueryRuleMatch {
31+
r.useField = useField
32+
return r
33+
}
34+
func (r *IntervalQueryRuleMatch) Filter(filter *IntervalQueryRuleFilter) *IntervalQueryRuleMatch {
35+
r.filter = filter
36+
return r
37+
}
38+
39+
// Source returns JSON for the function score query.
40+
func (r *IntervalQueryRuleMatch) Source() (interface{}, error) {
41+
source := make(map[string]interface{})
42+
43+
source["query"] = r.query
44+
45+
if r.ordered != nil {
46+
source["ordered"] = *r.ordered
47+
}
48+
if r.maxGaps != nil {
49+
source["max_gaps"] = *r.maxGaps
50+
}
51+
if r.analyzer != "" {
52+
source["analyzer"] = r.analyzer
53+
}
54+
if r.useField != "" {
55+
source["use_field"] = r.useField
56+
}
57+
if r.filter != nil {
58+
filterRuleSource, err := r.filter.Source()
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
source["filter"] = filterRuleSource
64+
}
65+
66+
return map[string]interface{}{"match": source}, nil
67+
}
68+
69+
func (r *IntervalQueryRuleMatch) isIntervalQueryRule() bool {
70+
return true
71+
}

0 commit comments

Comments
 (0)