From 38a422eb951bb94838f9c927d388084b62a8adc8 Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:31:09 +0100 Subject: [PATCH] Fix facet search error --- lib/meilisearch-rails.rb | 9 +++++++++ spec/integration_spec.rb | 15 +++++++++++++++ spec/support/active_record_classes.rb | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/meilisearch-rails.rb b/lib/meilisearch-rails.rb index 09a240f5..af8117b7 100644 --- a/lib/meilisearch-rails.rb +++ b/lib/meilisearch-rails.rb @@ -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 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 0b9c4f71..3eb3a78c 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -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) } diff --git a/spec/support/active_record_classes.rb b/spec/support/active_record_classes.rb index abdbe850..f77e0ee8 100644 --- a/spec/support/active_record_classes.rb +++ b/spec/support/active_record_classes.rb @@ -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