meilisearch / meilisearch-rails

Meilisearch integration for Ruby on Rails
https://www.meilisearch.com
MIT License
295 stars 47 forks source link

Meilisearch is not indexing records in a Rails model #304

Closed AliOsm closed 11 months ago

AliOsm commented 11 months ago

Description I have the following Rails model:

# == Schema Information
#
# Table name: hadiths
#
#  id              :bigint           not null, primary key
#  data            :jsonb            not null
#  index           :integer          not null
#  neighbors       :integer          not null, is an Array
#  searchable_text :text             not null
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#  hadith_book_id  :bigint           not null
#  user_id         :bigint           not null
#
# Indexes
#
#  index_hadiths_on_hadith_book_id  (hadith_book_id)
#  index_hadiths_on_index           (index) UNIQUE
#  index_hadiths_on_user_id         (user_id)
#
# Foreign Keys
#
#  fk_rails_59c225b3da  (user_id => users.id)
#  fk_rails_7da2544a2e  (hadith_book_id => hadith_books.id)
#
class Hadith < ApplicationRecord
  include MeiliSearch::Rails

  extend Pagy::Meilisearch
  ActiveRecord_Relation.include Pagy::Meilisearch

  belongs_to :user
  belongs_to :hadith_book

  counter_culture :user
  counter_culture :hadith_book

  meilisearch do
    attribute %i[searchable_text hadith_book_id]

    attribute :author do
      hadith_book.author
    end

    attribute :source do
      hadith_book.source
    end

    searchable_attributes %i[searchable_text]
    filterable_attributes %i[author source hadith_book_id]
  end

  def neighbors
    Hadith.where(index: read_attribute(:neighbors)[0..50])
  end
end

When I create records in it, I can't index them in meilisearch index. I tried using Hadith.reindex! and Hadith.first.index!, then umber of documents is always 0.

Also, I tried to restart the meilisearch service, re-creating the index, and re-deploying the app, nothing worked.

On development it works fine, but in production it is not working!

This issue happens, while other Rails models can be indexed as usual.

Expected behavior Documents to be indexed.

Current behavior No document is indexed.

Environment (please complete the following information):

AliOsm commented 11 months ago

One difference, in development I'm using the latest meilisearch docker image, while in production I'm using v1.1.1, could this be the issue?

AliOsm commented 11 months ago

I tried with v1.1.1 on development and it works fine.

AliOsm commented 11 months ago

Other observation is when I run Hadith.reindex! it takes minutes before finish, which means there is a process happens behind the scene, but I can't see any logs.

AliOsm commented 11 months ago

I just found the issue. I created a meilisearch ruby client and tried to add a document to the index by hand as follows:

require 'meilisearch'

client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'key')
index = client.index('Hadith')
index.add_documents([{id: 1, searchable_text: 'test', author: 'test', hadith_book_id: 1, source: 'lk'}])

Then, when I checked the tasks I found the following issue:

client.tasks

# The primary key inference failed as the engine found 2 fields ending with `id` in their names: 'id' and 'hadith_book_id'. Please specify the primary key manually using the `primaryKey` query parameter.

So, I changed hadith_book_id to hadith_book by defining the attribute as a block and the issue resolved.

I think I can use primary_key: :id with meilisearch block, but this works for now :)