加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 轮胎/ ElasticSearch单表继承支持

发布时间:2020-12-17 02:30:45 所属栏目:百科 来源:网络整理
导读:Cross post from GitHub: 我的应用程序搜索各种第三方服务中的链接,如Delicious,Twitter …我有以下基类: class Link include Mongoid::Document include Tire::Model::Search include Tire::Model::Callbacks field :href,type: String field :name,type:
Cross post from GitHub:

我的应用程序搜索各种第三方服务中的链接,如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: 'string',analyzer: 'url'
    indexes :name,analyzer: 'keyword',boost: 10
  end
end

以下类继承自Link并添加另外两个字段:

class Link::Delicious < Link
  field :tags,type: Array
  field :time,type: Time

  mapping do
    indexes :tags,analyzer: 'keyword'
    indexes :time,type: 'date'
  end
end

搜索将通过Base类完成:Link.search(‘google.com’).有没有机会让这个工作?目前,Link :: Delicious中的(附加)字段被Tire / ElasticSearch完全忽略.

解决方法

修复了覆盖映射方法,如下所示:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读