Elasticsearch - Automatically import records from an Active Record model
System:
- Rails 4
- Ruby 2
- Elasticsearch 1.6.0
I am using Elasticsearch to filter records and calculate statistics for my Active Record models. I would like the Elasticsearch indexes to reflect my Postgres database, to import existing records into my indexes, new records are indexed as they are created, etc.
I have a problem that my two models include:
# app/models/concerns/foo.rb
module Foo
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
self.import force: true
end
class_methods do
def statistics(filter = {})
# ES extended_stats aggregations
end
end
end
# app/models/bar.rb
# Same for another class
class Bar < ActiveRecord::Base
include Foo
end
However, when I open the Rails console and call Bar.statistics
, I get nil
in each field. If I then execute Bar.import force: true
then Bar.statistics
, I get the correct non-zero values ββin my aggregation with extended statistics.
Something else that might be noteworthy: when I opened foo.rb in my Rails console using Pry and then exited, I ran into an error Cannot define multiple included blocks for a Concern
(although loading the app works fine).
Am I missing something to make my records automatically imported into ES?
source to share
No one has answered this question yet
Check out similar questions: