|
42 | 42 |
|
43 | 43 | context 'when a block is provided' do
|
44 | 44 |
|
45 |
| - context 'when the containing scope is accessed in the block' do |
| 45 | + context 'when the top-level scope is accessed in the block' do |
46 | 46 |
|
47 |
| - let(:s) do |
48 |
| - def query_value |
49 |
| - { title: 'test' } |
| 47 | + context 'when the method is accessed from within a \'query\' block' do |
| 48 | + |
| 49 | + let(:s) do |
| 50 | + def query_value |
| 51 | + { title: 'test' } |
| 52 | + end |
| 53 | + |
| 54 | + search do |
| 55 | + query do |
| 56 | + match query_value |
| 57 | + end |
| 58 | + end |
50 | 59 | end
|
51 | 60 |
|
52 |
| - search do |
53 |
| - query do |
54 |
| - match query_value |
| 61 | + let(:expected_hash) do |
| 62 | + { query: { match: { title: 'test' } } } |
| 63 | + end |
| 64 | + |
| 65 | + it 'allows access to the containing scope' do |
| 66 | + expect(s.to_hash).to eq(expected_hash) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'when the method is accessed from within a \'base_component\' block' do |
| 71 | + |
| 72 | + let(:s) do |
| 73 | + def sort_field |
| 74 | + :category |
| 75 | + end |
| 76 | + |
| 77 | + search do |
| 78 | + query do |
| 79 | + match title: 'test' |
| 80 | + end |
| 81 | + sort do |
| 82 | + by sort_field, order: 'desc' |
| 83 | + end |
55 | 84 | end
|
56 | 85 | end
|
| 86 | + |
| 87 | + let(:expected_hash) do |
| 88 | + { query: { match: { title: 'test' } }, sort: [{ category: { order: 'desc'} }] } |
| 89 | + end |
| 90 | + |
| 91 | + it 'allows access to the containing scope' do |
| 92 | + expect(s.to_hash).to eq(expected_hash) |
| 93 | + end |
57 | 94 | end
|
58 | 95 |
|
59 |
| - let(:expected_hash) do |
60 |
| - { query: { match: { title: 'test' } } } |
| 96 | + context 'when the method is accessed from within a \'_not\' block' do |
| 97 | + |
| 98 | + let(:s) do |
| 99 | + def term_field |
| 100 | + :color |
| 101 | + end |
| 102 | + |
| 103 | + search do |
| 104 | + query do |
| 105 | + filtered do |
| 106 | + filter do |
| 107 | + _not do |
| 108 | + term term_field => 'red' |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + let(:expected_hash) do |
| 117 | + { query: { filtered: { filter: { not: { term: { color: 'red' } } } } } } |
| 118 | + end |
| 119 | + |
| 120 | + it 'allows access to the containing scope' do |
| 121 | + expect(s.to_hash).to eq(expected_hash) |
| 122 | + end |
61 | 123 | end
|
62 | 124 |
|
63 |
| - it 'allows access to the containing scope' do |
64 |
| - expect(s.to_hash).to eq(expected_hash) |
| 125 | + context 'when the method is accessed from within a \'aggregation\' block' do |
| 126 | + |
| 127 | + let(:s) do |
| 128 | + def sum_field |
| 129 | + 'clicks' |
| 130 | + end |
| 131 | + |
| 132 | + def aggregation_name |
| 133 | + :sum_clicks |
| 134 | + end |
| 135 | + |
| 136 | + search do |
| 137 | + aggregation :clicks_for_tag_one do |
| 138 | + filter terms: { tags: ['one'] } do |
| 139 | + aggregation aggregation_name do |
| 140 | + sum field: sum_field |
| 141 | + end |
| 142 | + end |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + let(:expected_hash) do |
| 148 | + { aggregations: { clicks_for_tag_one: { filter: { terms: { tags: ['one'] } }, |
| 149 | + aggregations: { sum_clicks: { sum: { field: 'clicks' } } } } } } |
| 150 | + end |
| 151 | + |
| 152 | + it 'allows access to the containing scope' do |
| 153 | + expect(s.to_hash).to eq(expected_hash) |
| 154 | + end |
65 | 155 | end
|
66 |
| - end |
67 | 156 |
|
68 |
| - context 'when other methods are used to construct the query' do |
| 157 | + context 'when the method is accessed from within a \'filter\' block' do |
| 158 | + |
| 159 | + let(:s) do |
69 | 160 |
|
70 |
| - def bool_query(obj) |
71 |
| - obj.instance_eval do |
72 |
| - bool do |
73 |
| - must do |
74 |
| - match foo: 'bar' |
| 161 | + def term_field |
| 162 | + :color |
| 163 | + end |
| 164 | + |
| 165 | + def not_clause(obj) |
| 166 | + obj.instance_eval do |
| 167 | + _not do |
| 168 | + term term_field => 'red' |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + search do |
| 174 | + query do |
| 175 | + filtered do |
| 176 | + filter do |
| 177 | + not_clause(self) |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + let(:expected_hash) do |
| 185 | + { query: { filtered: { filter: { not: { term: { color: 'red' } } } } } } |
| 186 | + end |
| 187 | + |
| 188 | + it 'allows access to the containing scope' do |
| 189 | + expect(s.to_hash).to eq(expected_hash) |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + context 'when the method is accessed from within a \'base_aggregation_component\' block' do |
| 194 | + |
| 195 | + let(:s) do |
| 196 | + |
| 197 | + def term_field |
| 198 | + :body |
| 199 | + end |
| 200 | + |
| 201 | + search do |
| 202 | + aggregation :interesting_terms do |
| 203 | + significant_terms do |
| 204 | + field :body |
| 205 | + end |
75 | 206 | end
|
76 |
| - filter do |
77 |
| - term foo: 'bar' |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + let(:expected_hash) do |
| 211 | + { aggregations: { interesting_terms: { significant_terms: { field: :body } } } } |
| 212 | + end |
| 213 | + |
| 214 | + it 'allows access to the containing scope' do |
| 215 | + expect(s.to_hash).to eq(expected_hash) |
| 216 | + end |
| 217 | + end |
| 218 | + |
| 219 | + context 'when the method is accessed from within a \'base_compound_filter_component\' block' do |
| 220 | + |
| 221 | + let(:s) do |
| 222 | + |
| 223 | + def term_field |
| 224 | + 'red' |
| 225 | + end |
| 226 | + |
| 227 | + search do |
| 228 | + query do |
| 229 | + filtered do |
| 230 | + filter do |
| 231 | + _and do |
| 232 | + term color: term_field |
| 233 | + term size: 'xxl' |
| 234 | + end |
| 235 | + end |
| 236 | + end |
78 | 237 | end
|
79 | 238 | end
|
80 | 239 | end
|
| 240 | + |
| 241 | + let(:expected_hash) do |
| 242 | + { query: { filtered: { filter: { and: [{ term: { color: 'red' } }, |
| 243 | + { term: { size: 'xxl' } }] } } } } |
| 244 | + end |
| 245 | + |
| 246 | + it 'allows access to the containing scope' do |
| 247 | + expect(s.to_hash).to eq(expected_hash) |
| 248 | + end |
81 | 249 | end
|
| 250 | + end |
| 251 | + |
| 252 | + context 'when other methods are used to construct the query' do |
82 | 253 |
|
83 |
| - let(:my_search) do |
84 |
| - search do |
85 |
| - query do |
86 |
| - bool_query(self) |
| 254 | + def bool_query(obj) |
| 255 | + obj.instance_eval do |
| 256 | + bool do |
| 257 | + must do |
| 258 | + match foo: 'bar' |
| 259 | + end |
| 260 | + filter do |
| 261 | + term foo: 'bar' |
87 | 262 | end
|
88 | 263 | end
|
89 | 264 | end
|
| 265 | + end |
90 | 266 |
|
91 |
| - it 'finds the correct bindings' do |
92 |
| - expect(my_search.to_hash).to eq(query: { bool: { filter: [{ term: { foo: 'bar' } }], |
93 |
| - must: [{ match: { foo: 'bar' } }] } }) |
| 267 | + let(:my_search) do |
| 268 | + search do |
| 269 | + query do |
| 270 | + bool_query(self) |
| 271 | + end |
94 | 272 | end
|
95 | 273 | end
|
| 274 | + |
| 275 | + it 'finds the correct bindings' do |
| 276 | + expect(my_search.to_hash).to eq(query: { bool: { filter: [{ term: { foo: 'bar' } }], |
| 277 | + must: [{ match: { foo: 'bar' } }] } }) |
| 278 | + end |
| 279 | + end |
96 | 280 | end
|
97 | 281 |
|
98 | 282 | describe '#collapse' do
|
|
0 commit comments