Skip to content

Commit

Permalink
Merge #320
Browse files Browse the repository at this point in the history
320: Fix facet search error r=brunoocasali a=ellnix

# Pull Request

## Related issue
Fixes #317

## What does this PR do?
- ~~Updates SafeIndex to pass `**opts` along to methods defined on a `MeiliSearch::Index`~~
- Ruby 2 is different in how it passes the last `**options` parameter so to maintain compatibility with it, I created a `facet_search` method on `SafeIndex`


Co-authored-by: ellnix <[email protected]>
  • Loading branch information
meili-bors[bot] and ellnix authored Feb 12, 2024
2 parents 6a3c04b + 38a422e commit 53febb3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ def initialize(index_uid, raise_on_failure, options)
end
end

# Maually define facet_search due to complications with **opts in ruby 2.*
def facet_search(*args, **opts)
SafeIndex.log_or_throw(:facet_search, @raise_on_failure) do
return MeiliSearch::Rails.black_hole unless MeiliSearch::Rails.active?

@index.facet_search(*args, **opts)
end
end

# special handling of wait_for_task to handle null task_id
def wait_for_task(task_uid)
return if task_uid.nil? && !@raise_on_failure # ok
Expand Down
15 changes: 15 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,21 @@
expect(results.facets_distribution['genre'].size).to eq(3)
end

it 'does not error on facet_search' do
genres = %w[Legend Fiction Crime].cycle
authors = %w[A B C].cycle

5.times do
Book.create! name: Faker::Book.title, author: authors.next, genre: genres.next
end

expect do
Book.index.facet_search('genre', 'Fic', filter: 'author = A')
Book.index.facet_search('genre', filter: 'author = A')
Book.index.facet_search('genre')
end.not_to raise_error
end

context 'with Marshal serialization' do
let(:found_books) { Book.search('*') }
let(:marshaled_books) { Marshal.dump(found_books) }
Expand Down
2 changes: 1 addition & 1 deletion spec/support/active_record_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class Book < ActiveRecord::Base
meilisearch synchronous: true, index_uid: safe_index_uid('SecuredBook'), sanitize: true do
searchable_attributes [:name]
typo_tolerance min_word_size_for_typos: { one_typo: 5, twoTypos: 8 }
filterable_attributes [:genre]
filterable_attributes %i[genre author]
faceting max_values_per_facet: 3

add_index safe_index_uid('BookAuthor') do
Expand Down

0 comments on commit 53febb3

Please sign in to comment.