daedal-sl 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module DaedalSL
2
+ class Builder
3
+ include DaedalSL::QueryHelpers
4
+
5
+ attr_reader :data
6
+
7
+ def initialize(data=nil)
8
+ @data = data
9
+ @query = match_all
10
+ @filter = nil
11
+ @options = {}
12
+ end
13
+
14
+ def query
15
+ if block_given?
16
+ @query = yield
17
+ end
18
+ end
19
+
20
+ def filter
21
+ if block_given?
22
+ @filter = yield
23
+ end
24
+ end
25
+
26
+ def from(num)
27
+ @options[:from] = num
28
+ end
29
+
30
+ def size(num)
31
+ @options[:size] = num
32
+ end
33
+
34
+ def fields(*f)
35
+ @options[:fields] = f
36
+ end
37
+
38
+ def paginate(options={})
39
+ page = options[:page] || 1
40
+ per_page = options[:per_page] || 10
41
+
42
+ from ((page - 1) * per_page)
43
+ size per_page
44
+ end
45
+
46
+ def to_hash
47
+ result = {}
48
+ unless @query.nil?
49
+ result[:query] = @query.to_hash
50
+ end
51
+ unless @filter.nil?
52
+ result[:filter] = @filter.to_hash
53
+ end
54
+ result.merge(@options)
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,16 @@
1
+ module DaedalSL
2
+ class DisMaxQuery < BlockQuery
3
+
4
+ def initialize(parent, options)
5
+ @query_type = Daedal::Queries::DisMaxQuery
6
+ super
7
+ end
8
+
9
+ def query
10
+ if (result = yield)
11
+ @base.queries << result
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ module DaedalSL
2
+ class NestedBoolFilter < BlockQuery
3
+
4
+ def initialize(parent, options)
5
+ @parent = parent
6
+ @base = Daedal::Filters::NestedFilter.new(options.merge(filter: Daedal::Filters::BoolFilter.new))
7
+ end
8
+
9
+ def must
10
+ if (result = yield)
11
+ @base.filter.must << result
12
+ end
13
+ end
14
+
15
+ def should
16
+ if (result = yield)
17
+ @base.filter.should << result
18
+ end
19
+ end
20
+
21
+ def must_not
22
+ if (result = yield)
23
+ @base.filter.must_not << result
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module DaedalSL
2
+ class NestedBoolQuery < BlockQuery
3
+
4
+ def initialize(parent, options)
5
+ @parent = parent
6
+ @base = Daedal::Queries::NestedQuery.new(options.merge(query: Daedal::Queries::BoolQuery.new))
7
+ end
8
+
9
+ def must
10
+ if (result = yield)
11
+ @base.query.must << result
12
+ end
13
+ end
14
+
15
+ def should
16
+ if (result = yield)
17
+ @base.query.should << result
18
+ end
19
+ end
20
+
21
+ def must_not
22
+ if (result = yield)
23
+ @base.query.must_not << result
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module DaedalSL
2
+ class NestedDisMaxQuery < BlockQuery
3
+
4
+ def initialize(parent, options)
5
+ @parent = parent
6
+ @base = Daedal::Queries::NestedQuery.new(options.merge(query: Daedal::Queries::DisMaxQuery.new))
7
+ end
8
+
9
+ def query
10
+ if (result = yield)
11
+ @base.query.queries << result
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module DaedalSL
2
+ class OrFilter < BlockQuery
3
+
4
+ def initialize(parent, options)
5
+ @query_type = Daedal::Filters::OrFilter
6
+ super
7
+ end
8
+
9
+ def filter
10
+ if (result = yield)
11
+ @base.filters << result
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,91 @@
1
+ module DaedalSL
2
+ module QueryHelpers
3
+ include Daedal::Queries
4
+ include Daedal::Filters
5
+
6
+ def and_filter(options={}, &block)
7
+ DaedalSL::AndFilter.build(self, options, &block)
8
+ end
9
+
10
+ def bool_filter(options={}, &block)
11
+ DaedalSL::BoolFilter.build(self, options, &block)
12
+ end
13
+
14
+ def bool_query(options={}, &block)
15
+ DaedalSL::BoolQuery.build(self, options, &block)
16
+ end
17
+
18
+ def constant_score(options)
19
+ ConstantScoreQuery.new(options)
20
+ end
21
+
22
+ def dis_max(options={}, &block)
23
+ DaedalSL::DisMaxQuery.build(self, options, &block)
24
+ end
25
+
26
+ def exists_filter(options)
27
+ ExistsFilter.new(options)
28
+ end
29
+
30
+ def filtered_query(options)
31
+ FilteredQuery.new(options)
32
+ end
33
+
34
+ def fuzzy(options)
35
+ FuzzyQuery.new(options)
36
+ end
37
+
38
+ def geo_distance_filter(options)
39
+ GeoDistanceFilter.new(options)
40
+ end
41
+
42
+ def match(options)
43
+ MatchQuery.new(options)
44
+ end
45
+
46
+ def match_all
47
+ MatchAllQuery.new
48
+ end
49
+
50
+ def multi_match(options)
51
+ MultiMatchQuery.new(options)
52
+ end
53
+
54
+ def nested_bool_query(options={}, &block)
55
+ DaedalSL::NestedBoolQuery.build(self, options, &block)
56
+ end
57
+
58
+ def nested_bool_filter(options={}, &block)
59
+ DaedalSL::NestedBoolFilter.build(self, options, &block)
60
+ end
61
+
62
+ def nested_dis_max_query(options={}, &block)
63
+ DaedalSL::NestedDisMaxQuery.build(self, options, &block)
64
+ end
65
+
66
+ def or_filter(options={}, &block)
67
+ DaedalSL::OrFilter.build(self, options, &block)
68
+ end
69
+
70
+ def prefix(options)
71
+ PrefixQuery.new(options)
72
+ end
73
+
74
+ def query_string(options)
75
+ QueryStringQuery.new(options)
76
+ end
77
+
78
+ def range_filter(options)
79
+ RangeFilter.new(options)
80
+ end
81
+
82
+ def term_filter(options)
83
+ TermFilter.new(options)
84
+ end
85
+
86
+ def terms_filter(options)
87
+ TermsFilter.new(options)
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module DaedalSL
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,201 @@
1
+ require 'spec_helper'
2
+ require 'hashie'
3
+
4
+ describe DaedalSL do
5
+
6
+ context 'creating a complex query' do
7
+ it 'works' do
8
+ expected_result = {
9
+ query: {
10
+ bool: {
11
+ should: [
12
+ {
13
+ fuzzy: {
14
+ foofoo: {
15
+ value: "barbar"
16
+ }
17
+ }
18
+ },
19
+ {
20
+ multi_match: {
21
+ query: "barbar",
22
+ fields: [:field1, :field2]
23
+ }
24
+ }
25
+ ],
26
+ must: [
27
+ {
28
+ nested: {
29
+ path: :path,
30
+ query: {
31
+ bool: {
32
+ should: [
33
+ {
34
+ match: {
35
+ foofoo: {
36
+ query: "barbar"
37
+ }
38
+ }
39
+ }
40
+ ],
41
+ must: [
42
+ {
43
+ match: {
44
+ foofoo: {
45
+ query: "barbar"
46
+ }
47
+ }
48
+ }
49
+ ],
50
+ must_not: []
51
+ }
52
+ }
53
+ }
54
+ },
55
+ {
56
+ nested: {
57
+ path: :path,
58
+ query: {
59
+ dis_max: {
60
+ queries: [
61
+ {
62
+ match: {
63
+ foofoo: {
64
+ query: "barbar"
65
+ }
66
+ }
67
+ },
68
+ {
69
+ match: {
70
+ foofoo: {
71
+ query: "barbar"
72
+ }
73
+ }
74
+ }
75
+ ]
76
+ }
77
+ }
78
+ }
79
+ }
80
+ ],
81
+ must_not: [
82
+ {
83
+ match: {
84
+ foofoo: {
85
+ query: "barbar"
86
+ }
87
+ }
88
+ }
89
+ ],
90
+ boost: 100.0
91
+ }
92
+ },
93
+ filter: {
94
+ bool: {
95
+ should: [
96
+ {
97
+ and: [
98
+ {
99
+ term: {
100
+ foofoo: "barbar"
101
+ }
102
+ },
103
+ {
104
+ terms: {
105
+ foofoo: ["term1", "term2"]
106
+ }
107
+ }
108
+ ]
109
+ }
110
+ ],
111
+ must: [
112
+ {
113
+ nested: {
114
+ path: :path,
115
+ filter: {
116
+ bool: {
117
+ should: [
118
+ {
119
+ term: {
120
+ foofoo: "barbar"
121
+ }
122
+ }
123
+ ],
124
+ must: [
125
+ {
126
+ term: {
127
+ foofoo: "barbar"
128
+ }
129
+ }
130
+ ],
131
+ must_not: []
132
+ }
133
+ }
134
+ }
135
+ }
136
+ ],
137
+ must_not: [
138
+ {
139
+ range: {
140
+ foofoo: {
141
+ lt: 2,
142
+ gt: 1
143
+ }
144
+ }
145
+ }
146
+ ]
147
+ }
148
+ },
149
+ fields: [:foo, :bar],
150
+ :from=>0,
151
+ :size=>10
152
+ }
153
+ data = Hashie::Mash.new({
154
+ foo: 'foofoo',
155
+ bar: 'barbar'
156
+ })
157
+ result = DaedalSL.build(data) do
158
+ query do
159
+ bool_query boost: 100 do
160
+ must do
161
+ nested_bool_query path: 'path' do
162
+ must { match field: data.foo, query: data.bar }
163
+ should { match field: data.foo, query: data.bar }
164
+ end
165
+ end
166
+ must do
167
+ nested_dis_max_query path: 'path' do
168
+ query { match field: data.foo, query: data.bar }
169
+ query { match field: data.foo, query: data.bar }
170
+ end
171
+ end
172
+ should { fuzzy field: data.foo, query: data.bar }
173
+ should { multi_match fields: ['field1', 'field2'], query: data.bar }
174
+ must_not { match field: data.foo, query: data.bar }
175
+ end
176
+ end
177
+ filter do
178
+ bool_filter do
179
+ must do
180
+ nested_bool_filter path: 'path' do
181
+ must { term_filter field: data.foo, term: data.bar }
182
+ should { term_filter field: data.foo, term: data.bar }
183
+ end
184
+ end
185
+ should do
186
+ and_filter do
187
+ filter { term_filter field: data.foo, term: data.bar }
188
+ filter { terms_filter field: data.foo, terms: ['term1', 'term2'] }
189
+ end
190
+ end
191
+ must_not { range_filter field: data.foo, gt: 1, lt: 2 }
192
+ end
193
+ end
194
+ fields :foo, :bar
195
+ paginate page: 1, per_page: 10
196
+ end
197
+ expect(result).to eq expected_result
198
+ end
199
+ end
200
+
201
+ end