まず、タイヤの永続性を有効にするいくつかの手順をスキップしました。このトピックに関するChrisBerkhoutの投稿のおかげで、私のモデルは次のようになりました。
class Person
include Tire::Model::Persistence
include Tire::Model::Search
include Tire::Model::Callbacks
index_name ES_INDEX_NAME
# Refresh ES so any changes are immediately visible
refresh = lambda { Tire::Index.new(ES_INDEX_NAME).refresh }
after_save &refresh
after_destroy &refresh
property :firstName
property :lastName
property :updated_at
before_save { |n| n.updated_at = Time.now }
def self.touch_es
# Ensure a mapping in a fresh index, so that Note.all can sort on updated_at
n = Person.new
n.save
n.destroy
end
def self.all
# Override so that Note.all comes back sorted on updated_at, rather than _id
search { sort { by :updated_at, 'desc' } }
end
def self.q(q)
search { sort { by :updated_at, 'desc' }; query { string q } }
end
end
そして、インデックスを設定するために、lib/tasksにes.rakeタスクを追加する必要がありました。
namespace :es do
desc "Delete the ElasticSearch index for the current environment"
task :drop => :environment do
Tire::Index.new(ES_INDEX_NAME).delete
end
desc "Create the ElasticSearch index for the current environment"
task :create => :environment do
Tire::Index.new(ES_INDEX_NAME).create
Person.touch_es
end
end
そして、インデックス名を定義するためのconfig /initializersのイニシャライザーes.rb:
ES_INDEX_NAME = "#{Rails.application.class.parent_name.downcase}_#{Rails.env}"
御馳走になります!
ActiveAdminをESパーシステンスで動作させることに関しては、ActiveModelまたはORMに依存しないバージョンが機能していますが、AAはActiveRecordに関連付けられていることがわかります。