|
1 | 1 | require 'spec_helper' |
| 2 | +require 'support/models/book' |
| 3 | +require 'support/models/product' |
| 4 | +require 'support/models/color' |
2 | 5 |
|
3 | 6 | describe 'multi-search' do |
4 | 7 | def reset_indexes |
@@ -43,6 +46,65 @@ def reset_indexes |
43 | 46 | end |
44 | 47 | end |
45 | 48 |
|
| 49 | + context 'with arbitrary keys' do |
| 50 | + context 'when index_name is not present' do |
| 51 | + it 'assumes key is index and errors' do |
| 52 | + expect do |
| 53 | + MeiliSearch::Rails.multi_search( |
| 54 | + 'test_group' => { q: 'Steve' } |
| 55 | + ) |
| 56 | + end.to raise_error(MeiliSearch::ApiError) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context 'when :index_name is present' do |
| 61 | + it 'searches the correct index' do |
| 62 | + results = MeiliSearch::Rails.multi_search( |
| 63 | + 'books' => { q: 'Steve', index_name: Book.index.uid }, |
| 64 | + 'products' => { q: 'palm', index_name: Product.index.uid, limit: 1 }, |
| 65 | + 'colors' => { q: 'bl', index_name: Color.index.uid } |
| 66 | + ) |
| 67 | + |
| 68 | + expect(results).to contain_exactly( |
| 69 | + a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs'), |
| 70 | + a_hash_including('name' => 'palm pixi plus'), |
| 71 | + a_hash_including('name' => 'blue', 'short_name' => 'blu'), |
| 72 | + a_hash_including('name' => 'black', 'short_name' => 'bla') |
| 73 | + ) |
| 74 | + end |
| 75 | + |
| 76 | + it 'allows searching the same index n times' do |
| 77 | + index_name = Color.index.uid |
| 78 | + |
| 79 | + results = MeiliSearch::Rails.multi_search( |
| 80 | + 'dark_colors' => { q: 'black', index_name: index_name }, |
| 81 | + 'bright_colors' => { q: 'blue', index_name: index_name }, |
| 82 | + 'nature_colors' => { q: 'green', index_name: index_name } |
| 83 | + ) |
| 84 | + |
| 85 | + expect(results).to contain_exactly( |
| 86 | + a_hash_including('name' => 'blue', 'short_name' => 'blu'), |
| 87 | + a_hash_including('name' => 'black', 'short_name' => 'bla'), |
| 88 | + a_hash_including('name' => 'green', 'short_name' => 'gre') |
| 89 | + ) |
| 90 | + end |
| 91 | + |
| 92 | + context 'when :class_name is also present' do |
| 93 | + it 'loads results from the correct models' do |
| 94 | + results = MeiliSearch::Rails.multi_search( |
| 95 | + 'books' => { q: 'Steve', index_name: Book.index.uid, class_name: 'Book' }, |
| 96 | + 'products' => { q: 'palm', limit: 1, index_name: Product.index.uid, class_name: 'Product' }, |
| 97 | + 'colors' => { q: 'bl', index_name: Color.index.uid, class_name: 'Color' } |
| 98 | + ) |
| 99 | + |
| 100 | + expect(results).to contain_exactly( |
| 101 | + steve_jobs, palm_pixi_plus, blue, black |
| 102 | + ) |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
46 | 108 | context 'with index name keys' do |
47 | 109 | it 'returns hashes' do |
48 | 110 | results = MeiliSearch::Rails.multi_search( |
|
0 commit comments