4

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, type: 'string', analyzer: 'keyword', boost: 10
  end
end

次のクラスは から継承しLink、さらに 2 つのフィールドを追加します。

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

検索は Base クラスを介して行われます: Link.search('google.com'). これを機能させる機会はありますか?現時点では、(追加の) フィールドはLink::DeliciousTire/ElasticSearch によって完全に無視されます。

4

1 に答える 1

4

mapping次のようにメソッドを上書きして修正しました。

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
于 2011-12-21T14:08:35.757 に答える