Ruby on rails Tire/ElasticSearch单表继承支持
: 我的应用程序在各种第三方服务(如Delicious、Twitter)中搜索链接……我有以下基本类:Ruby on rails Tire/ElasticSearch单表继承支持,ruby-on-rails,ruby,ruby-on-rails-3,elasticsearch,tire,Ruby On Rails,Ruby,Ruby On Rails 3,elasticsearch,Tire,: 我的应用程序在各种第三方服务(如Delicious、Twitter)中搜索链接……我有以下基本类: class Link include Mongoid::Document include Tire::Model::Search include Tire::Model::Callbacks field :href, type: String field :name, type: String mapping do indexes :href, type:
class Link
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
field :href, type: String
field :name, type: String
mapping do
indexes :href, type: 'string', analyzer: 'url'
indexes :name, type: 'string', analyzer: 'keyword', boost: 10
end
end
下面的类继承自Link
,并添加了另外两个字段:
class Link::Delicious < Link
field :tags, type: Array
field :time, type: Time
mapping do
indexes :tags, type: 'string', analyzer: 'keyword'
indexes :time, type: 'date'
end
end
类链接::美味
搜索将通过基类完成:
Link.search('google.com')
。有没有机会让它工作起来?目前,Tire/ElasticSearch完全忽略了Link::Delicious
中的(附加)字段。通过覆盖映射
方法进行了修复,如下所示:
class Link
# …
class << self
def mapping_with_super(*args, &block)
# Creating only one index
index_name('links')
document_type('link')
superclass.mapping_without_super.each do |name, options|
indexes(name, options)
end if superclass.respond_to?(:mapping)
mapping_without_super(args, &block)
end
alias_method_chain :mapping, :super
end
end
类链接
# …
班