Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using attribute option with custom primary key will break fetching #339

Open
ellnix opened this issue Feb 28, 2024 · 0 comments
Open

Using attribute option with custom primary key will break fetching #339

ellnix opened this issue Feb 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ellnix
Copy link
Collaborator

ellnix commented Feb 28, 2024

Description
If you provide a custom primary key method and also specify attributes other than the real primary key of the model in the meilisearch block, the documents in the meilisearch instance will not contain the model's real primary key and therefore cannot map to database records.

Expected behavior
MeiliSearch still includes ID in its documents and fetching does not fail.

Reproduction script

require 'bundler/inline'

gemfile do
  gem "rails", "~> 7.1.3"
  gem "sqlite3", "~> 1.4"
  gem "meilisearch-rails", "~> 0.11.1"
end

MeiliSearch::Rails.configuration = {
  meilisearch_url: ENV.fetch('MEILISEARCH_HOST', 'http://localhost:7700'),
  meilisearch_api_key: ENV.fetch('MEILISEARCH_API_KEY', 'masterKey')
}

MeiliSearch::Rails.client.delete_index 'dogs'
MeiliSearch::Rails.client.delete_index 'cats'
sleep 0.1

FileUtils.rm('data.sqlite3') if File.exist?('data.sqlite3')

require 'active_record'

ActiveRecord::Base.establish_connection(
  'adapter' => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
  'database' => 'data.sqlite3',
  'pool' => 5,
  'timeout' => 5000
)

ActiveRecord::Schema.define do
  create_table(:dogs) { |t| t.string :name }
  create_table(:cats) { |t| t.string :name }
end

# With custom primary key
class Dog < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch index_uid: 'dogs', primary_key: :ms_id do
    attribute :name
  end

  def ms_id
    "dog_#{id}"
  end
end

# Without custom primary key
class Cat < ActiveRecord::Base
  include MeiliSearch::Rails

  meilisearch index_uid: 'cats' do
    attribute :name
  end
end

Dog.create name: 'spikey'
Cat.create name: 'fluffy'

sleep 0.5

p Dog.search 'spikey' # Will return nothing
# []

p Dog.index.search('spikey')['hits'] # Documents without IDs
# [{"ms_id"=>"dog_2", "name"=>"spikey"}]

p Cat.search 'fluffy' # Returns a cat correctly
# [#<Cat id: 1, name: "fluffy">]

p Cat.index.search('fluffy')['hits'] # Documents still have IDs
# [{"id"=>"1", "name"=>"fluffy"}]

@ellnix ellnix added the bug Something isn't working label Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant