Searching across multiple models produces an exception
adambutler opened this issue · 2 comments
Describe the bug
Searching across multiple indexes is producing an unexpected error:
Searchkick.search("*", models: [CommunityMember, User]).results
Produces:
Searchkick::Error: Unknown model for index: community_members_20240723145721041. Pass the `models` option to the search method.
from /Users/adambutler/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/searchkick-5.3.1/lib/searchkick/results.rb:237:in `block in with_hit_and_missing_records'
The problem seems to be in Searchkick::Results#with_hit_and_missing_records
. You can see that options[:index_mapping]
contains two keys :community_members
and :users
:
options[:index_mapping]
=> {:community_members=>
[CommunityMember(id: integer, community_id: integer, user_id: integer)],
:users=>
[User(id: integer, email: string)]}
However index_alias
is "community_members"
not :community_members
thus this line unexpectedly produces an empty array.
Array((options[:index_mapping] || {})[index_alias])
=> []
To reproduce
Use this code to reproduce when possible:
bundler/inline
working in my dev environment. I will come back to this later if needed.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord", require: "active_record"
gem "activejob", require: "active_job"
gem "sqlite3"
gem "searchkick", git: "https://github.com/ankane/searchkick.git"
# uncomment one
# gem "elasticsearch"
# gem "opensearch-ruby"
end
puts "Searchkick version: #{Searchkick::VERSION}"
puts "Server version: #{Searchkick.server_version}"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveJob::Base.queue_adapter = :inline
ActiveRecord::Schema.define do
create_table :products do |t|
t.string :name
end
end
class Product < ActiveRecord::Base
searchkick
end
Product.reindex
Product.create!(name: "Test")
Product.search_index.refresh
p Product.search("test", fields: [:name]).response
Additional context
Add any other context.
Hi @adambutler, please use the bug report template to reproduce.
I found the issue was that my index names were symbols instead of strings:
- searchkick index_name: :community_members
+ searchkick index_name: "community_members"