Search code examples
ruby-on-railssearchkick

Find similar records based on race_id and animal_id


I have a model called Adoption:

class Adoption < ApplicationRecord
  belongs_to :animal
  belongs_to :race

  searchkick language: 'french'

  def search_data
    {
      # name: name,
      # description: description,
      race_id: race_id,
      animal_id: animal_id
    }
  end
end

So, now I want to find similar adoptions. I've created two adoption records with same data. In rails console, I do these commands: Adoption.reindex, then: adoption = Adoption.find(1), and finally: similars = adoption.similar(fields: %w[race_id^5 animal_id], limit: 3).records. And it doesn't find any similar adoptions. But if I uncomment name in a search_data method, reindex and add name to the array of fields, it finds the adoption. So, what is wrong with my code? Any help will be appreciated. Thanks.


Solution

  • Here is the solution

    similars = adoption.similar(fields: [:race_id, :animal_id], limit: 3).records