|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe 'MeiliSearch::Index - Sorted search' do |
| 4 | + before(:all) do |
| 5 | + @documents = [ |
| 6 | + { |
| 7 | + objectId: 123, |
| 8 | + title: 'Pride and Prejudice', |
| 9 | + year: 1813, |
| 10 | + author: 'Jane Austen', |
| 11 | + genre: 'romance' |
| 12 | + }, |
| 13 | + { |
| 14 | + objectId: 456, |
| 15 | + title: 'Le Petit Prince', |
| 16 | + year: 1943, |
| 17 | + author: 'Antoine de Saint-Exupéry', |
| 18 | + genre: 'adventure' |
| 19 | + }, |
| 20 | + { |
| 21 | + objectId: 1, |
| 22 | + title: 'Alice In Wonderland', |
| 23 | + year: 1865, |
| 24 | + author: 'Lewis Carroll', |
| 25 | + genre: 'adventure' |
| 26 | + }, |
| 27 | + { |
| 28 | + objectId: 2, |
| 29 | + title: 'Le Rouge et le Noir', |
| 30 | + year: 1830, |
| 31 | + author: 'Stendhal', |
| 32 | + genre: 'romance' |
| 33 | + }, |
| 34 | + { |
| 35 | + objectId: 1344, |
| 36 | + title: 'The Hobbit', |
| 37 | + year: 1937, |
| 38 | + author: 'J. R. R. Tolkien', |
| 39 | + genre: 'adventure' |
| 40 | + }, |
| 41 | + { |
| 42 | + objectId: 4, |
| 43 | + title: 'Harry Potter and the Half-Blood Prince', |
| 44 | + year: 2005, |
| 45 | + author: 'J. K. Rowling', |
| 46 | + genre: 'fantasy' |
| 47 | + }, |
| 48 | + { |
| 49 | + objectId: 2056, |
| 50 | + title: 'Harry Potter and the Deathly Hallows', |
| 51 | + year: 2007, |
| 52 | + author: 'J. K. Rowling', |
| 53 | + genre: 'fantasy' |
| 54 | + }, |
| 55 | + { |
| 56 | + objectId: 42, |
| 57 | + title: 'The Hitchhiker\'s Guide to the Galaxy', |
| 58 | + year: 1978, |
| 59 | + author: 'Douglas Adams' |
| 60 | + }, |
| 61 | + { |
| 62 | + objectId: 190, |
| 63 | + title: 'A Game of Thrones', |
| 64 | + year: 1996, |
| 65 | + author: 'George R. R. Martin', |
| 66 | + genre: 'fantasy' |
| 67 | + } |
| 68 | + ] |
| 69 | + client = MeiliSearch::Client.new($URL, $MASTER_KEY) |
| 70 | + clear_all_indexes(client) |
| 71 | + @index = client.create_index('books') |
| 72 | + response = @index.add_documents(@documents) |
| 73 | + @index.wait_for_pending_update(response['updateId']) |
| 74 | + response = @index.update_sortable_attributes(['year', 'author']) |
| 75 | + @index.wait_for_pending_update(response['updateId']) |
| 76 | + response = @index.update_ranking_rules([ |
| 77 | + 'sort', |
| 78 | + 'words', |
| 79 | + 'typo', |
| 80 | + 'proximity', |
| 81 | + 'attribute', |
| 82 | + 'exactness' |
| 83 | + ]) |
| 84 | + @index.wait_for_pending_update(response['updateId']) |
| 85 | + end |
| 86 | + |
| 87 | + after(:all) do |
| 88 | + @index.delete |
| 89 | + end |
| 90 | + |
| 91 | + it 'does a custom search with one sort' do |
| 92 | + response = @index.search('prince', { sort: ['year:desc'] }) |
| 93 | + expect(response['hits'].count).to eq(2) |
| 94 | + expect(response['hits'].first['objectId']).to eq(4) |
| 95 | + end |
| 96 | + |
| 97 | + it 'does a custom search by sorting on strings' do |
| 98 | + response = @index.search('prince', { sort: ['author:asc'] }) |
| 99 | + expect(response['hits'].count).to eq(2) |
| 100 | + expect(response['hits'].first['objectId']).to eq(456) |
| 101 | + end |
| 102 | + |
| 103 | + it 'does a custom search with multiple sort' do |
| 104 | + response = @index.search('pr', { sort: ['year:desc', 'author:asc'] }) |
| 105 | + expect(response['hits'].count).to eq(3) |
| 106 | + expect(response['hits'].first['objectId']).to eq(4) |
| 107 | + end |
| 108 | + |
| 109 | + it 'does a placeholder search with multiple sort' do |
| 110 | + response = @index.search('', { sort: ['year:desc', 'author:asc'] }) |
| 111 | + expect(response['hits'].count).to eq(@documents.count) |
| 112 | + expect(response['hits'].first['objectId']).to eq(2056) |
| 113 | + end |
| 114 | +end |
0 commit comments