|
40 | 40 | end
|
41 | 41 | end
|
42 | 42 |
|
| 43 | + context 'when a block is provided' do |
| 44 | + |
| 45 | + context 'when the containing scope is accessed in the block' do |
| 46 | + |
| 47 | + let(:s) do |
| 48 | + def query_value |
| 49 | + { title: 'test' } |
| 50 | + end |
| 51 | + |
| 52 | + search do |
| 53 | + query do |
| 54 | + match query_value |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + let(:expected_hash) do |
| 60 | + { query: { match: { title: 'test' } } } |
| 61 | + end |
| 62 | + |
| 63 | + it 'allows access to the containing scope' do |
| 64 | + expect(s.to_hash).to eq(expected_hash) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context 'when other methods are used to construct the query' do |
| 69 | + |
| 70 | + def bool_query(obj) |
| 71 | + obj.instance_eval do |
| 72 | + bool do |
| 73 | + must do |
| 74 | + match foo: 'bar' |
| 75 | + end |
| 76 | + filter do |
| 77 | + term foo: 'bar' |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + let(:my_search) do |
| 84 | + search do |
| 85 | + query do |
| 86 | + bool_query(self) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 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' } }] } }) |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe '#collapse' do |
| 99 | + |
| 100 | + let(:s) do |
| 101 | + search do |
| 102 | + query do |
| 103 | + match title: 'test' |
| 104 | + end |
| 105 | + collapse :user do |
| 106 | + max_concurrent_group_searches 4 |
| 107 | + inner_hits 'last_tweet' do |
| 108 | + size 10 |
| 109 | + from 5 |
| 110 | + sort do |
| 111 | + by :date, order: 'desc' |
| 112 | + by :likes, order: 'asc' |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + let(:inner_hits_hash) do |
| 120 | + { name: 'last_tweet', |
| 121 | + size: 10, |
| 122 | + from: 5, |
| 123 | + sort: [ { date: { order: 'desc' } }, |
| 124 | + { likes: { order: 'asc' } }] |
| 125 | + } |
| 126 | + end |
| 127 | + |
| 128 | + let(:expected_hash) do |
| 129 | + { query: { match: { title: 'test' } }, |
| 130 | + collapse: { field: :user, |
| 131 | + max_concurrent_group_searches: 4, |
| 132 | + inner_hits: inner_hits_hash } } |
| 133 | + end |
| 134 | + |
| 135 | + it 'sets the field name' do |
| 136 | + expect(s.to_hash[:collapse][:field]).to eq(:user) |
| 137 | + end |
| 138 | + |
| 139 | + it 'sets the max_concurrent_group_searches option' do |
| 140 | + expect(s.to_hash[:collapse][:max_concurrent_group_searches]).to eq(4) |
| 141 | + end |
| 142 | + |
| 143 | + it 'sets the inner_hits' do |
| 144 | + expect(s.to_hash[:collapse][:inner_hits]).to eq(inner_hits_hash) |
| 145 | + end |
| 146 | + |
| 147 | + it 'constructs the correct hash' do |
| 148 | + expect(s.to_hash).to eq(expected_hash) |
| 149 | + end |
| 150 | + end |
| 151 | + |
43 | 152 | describe '#collapse' do
|
44 | 153 |
|
45 | 154 | let(:s) do
|
|
0 commit comments