0

我有一些红宝石课

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

初始化类后,如何动态设置 index_name?

红宝石 (1.9.3) 轨道 (3.2.3) 轮胎 (0.4.2)

4

1 回答 1

3

对于许多边缘情况,这仍然是一个未解决的问题。在 elasticsearch 中查看索引的方法有很多种。

首先,完全可以像这样定义一个动态索引名称:

Article.index_name { "articles-#{Time.now.year}" }

https://github.com/karmi/tire/blob/master/lib/tire/model/naming.rb#L10-35

其次,一种更灵活、更强大且面向未来的方法是在 elasticsearch 中使用别名功能

有关可能的灵感,请参阅https://github.com/karmi/tire/blob/master/test/integration/index_aliases_test.rb#L66 。

然后,您创建mydocs_current索引别名(“虚拟索引”)之类的东西,并将其指向特定的物理索引,比如说mydocs_2012_06. 然后,您在 cron 作业等中轮换此索引。

搜索时,您可以使用Tire.searchDSL 或将不同的索引/别名注入模型类。

于 2012-06-07T09:44:14.960 回答