Rails elasticsearch轮胎嵌套映射

时间:2013-07-22 21:12:27

标签: ruby-on-rails indexing elasticsearch tire

我正在尝试将嵌套代码编入索引到我的产品型号。 产品索引良好,但不是与产品关联的嵌套标签。 我能怎么做?我的映射是否正确?

Product Class

  include Tire::Model::Search
  include Tire::Model::Callbacks

mapping do
    indexes :id, type: 'integer', index: :not_analyzed
    indexes :name, type: 'string', analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball'
    indexes :price, type: 'float'
    indexes :category, type: 'string'
    indexes :location, type: 'string'
    indexes :online, type: 'boolean'
    indexes :created_at, type: 'date', index: :not_analyzed
    indexes :updated_at, type: 'date', index: :not_analyzed

    indexes :tags do
      indexes :id, type: 'integer', index: :not_analyzed
      indexes :name, type: 'string', analyzer: 'snowball', boost: 100
    end
  end

  def to_indexed_json
    {
      id: id,
      name: name,
      description: description,
      price: price,
      category: category,
      location: location,
      online: online,
      created_at: created_at,
      updated_at: updated_at,
      include: { tags: { only: [:name] } }
    }.to_json
  end

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案:

  def to_indexed_json
    {
      name: name,
      description: description,
      price: price,
      category: category,
      location: location,
      online: online,
      created_at: created_at,
      updated_at: updated_at,
      tags: tags
    }.to_json
  end

并且不需要将id,updated_at和created_at包含在映射中,因为它会自动编入索引。谢谢轮胎!