Elasticsearch,Tire和嵌套查询/与mongoid的关联

时间:2013-01-09 19:24:39

标签: elasticsearch tire

我正在使用ElasticSearch with Tire来索引和搜索mongoid模型,我一直在寻找索引和搜索嵌入式关联的“正确”方法。我有一个功能齐全的设置,但由于某种原因,我可以搜索除嵌入式模型中的字段之外的所有内容(ResourceTag)

删除了不必要的代码

class Resource
  include Mongoid::Document
  include Tire::Model::Search
  include Tire::Model::Callbacks
  include Mongoid::Timestamps

  embeds_many :tags, :class_name => "ResourceTag"

  accepts_nested_attributes_for :tags, :allow_destroy => true

  # Tire
  mapping do
    indexes :_id
    indexes :version,             analyzer: 'snowball', boost: 100
    indexes :created_at,          type: 'date', index: :not_analyzed
    indexes :updated_at,          type: 'date', index: :not_analyzed    
    indexes :assignment do
      indexes :_id
      indexes :name,                analyzer: 'snowball', boost: 100
      indexes :current_state,                analyzer: 'snowball', boost: 100
    do
    indexes :resource_files do
      indexes :_id
      indexes :name,                analyzer: 'snowball', boost: 100
    end
    indexes :tags do
      indexes :_id
      indexes :name,                analyzer: 'snowball', boost: 100
    end
  end

  def to_indexed_json
    self.to_json(
      :include => {
        :assignment => {
          :methods => :formatted_current_state
        }, 
        :resource_files => {
          :methods => [:is_video, :is_image]
        },
        :tags => nil
      }
    )
  end
end

class ResourceTag
  include Mongoid::Document

  # Attributes
  attr_accessible :name

  # Fields
  field :name, :type => String

  # Validations
  validates_presence_of :name
  validates_uniqueness_of :name, :case_sensitive => false

  # Relations
  embedded_in :resource

 end

我不确定我的代码有什么问题,但我确信我之前已经开始工作了,现在它搞砸了:s

我正在寻找一个带有mongoid和嵌入式文档的轮胎的例子,但是没有运气,我已经没想到了。

感谢您的任何想法和贡献。

0 个答案:

没有答案