System:
- Rails 4
- Ruby 2
- Elasticsearch 1.6.0
I'm using Elasticsearch to filter records and calculate statistics for my Active Record models. I'd like my Elasticsearch indices to mirror my Postgres database so that existing records are imported into my indices, new records are indexed as they are created, etc.
I have a concern that two of my models include like so:
# 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 every field. If I then execute Bar.import force: true
and then Bar.statistics
, I get correct, non-nil values in my extended stats aggregation.
Something else that might be of note: When I opened foo.rb in my Rails console using Pry and then exited, I ran into a Cannot define multiple included blocks for a Concern
error (though loading the application works fine).
Am I missing something to make my records auto-import into ES?