Pregunta

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)

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top