Reputation: 3705
I have some ruby class
class MyClass
include Tire::Model::Persistence
attr_accessor :date
mapping do
index_name Proc.new{|o| "my_class_#{o.date_index}" } # How to?
end
def initialize(d)
@date = d
end
def date_index
@date.strftime("%m%y")
end
end
How can I set the index_name dynamically, after initializing class?
Ruby (1.9.3) Rails(3.2.3) Tire (0.4.2)
Upvotes: 0
Views: 683
Reputation: 14419
This is still an unresolved problem with a number of edge-cases. There are many ways how to look at indices in elasticsearch.
First, it's perfectly possible to define a dynamic index name like this:
Article.index_name { "articles-#{Time.now.year}" }
See https://github.com/karmi/tire/blob/master/lib/tire/model/naming.rb#L10-35
Second, a much more flexible, powerful and future-proof approach is to use aliases feature in elasticsearch.
See https://github.com/karmi/tire/blob/master/test/integration/index_aliases_test.rb#L66 for inspiration on what's possible.
Then, you create something like mydocs_current
index alias (a “virtual index”), and point it to a specific physical index, let's say mydocs_2012_06
. You then rotate this index in cron jobs, etc.
When searching, you can either use the Tire.search
DSL or inject a different index/alias name into the model class on thy fly.
Upvotes: 3