【问题标题】:Rails/Tire – Prevent attribute from being indexedRails/Tire – 防止属性被索引
【发布时间】:2014-11-24 19:38:17
【问题描述】:

我正在尝试使用 Rails (3.2.8)、Tire 和 ElasticSearch 创建自定义映射。

我希望“标题”和“描述”成为唯一可搜索/索引的属性......但是,我似乎无法完成这项工作:

下面是我的模型:

class BlogPost < ActiveRecord::Base
  attr_accessible :title, :description, :attachment, :attachment_thumbnail

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

  tire.mapping do
    indexes :id, :type => 'integer', :index => :not_analyzed, :include_in_all => false
    indexes :attachment, :type => 'string', :index => :not_analyzed, :include_in_all => false
    indexes :attachment_thumbnail, :type => 'string', :index => :not_analyzed, :include_in_all => false
    indexes :title, analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball', boost: 100
  end

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 15) do
      query do
        boolean do
          must { string params[:query], default_operator: "AND" } if params[:query].present?
        end
      end
    end
  end

end

【问题讨论】:

    标签: ruby-on-rails elasticsearch ruby-on-rails-3.2 tire


    【解决方案1】:

    您需要将index 设置为no

    indexes :id, :type => 'integer', :index => :no, :include_in_all => false
    indexes :attachment, :type => 'string', :index => :no, :include_in_all => false
    indexes :attachment_thumbnail, :type => 'string', :index => :no, :include_in_all => false
    indexes :title, analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball', boost: 100
    

    使用not_analysed 只会防止字段被分解为标记 - 它仍会包含在索引中。

    请参阅core types 了解更多信息。

    【讨论】:

      【解决方案2】:

      如果您更改映射,您应该重新索引整个数据。 如果是测试数据,删除索引。创建索引并再次索引数据。我猜你试图在索引数据后添加 :include_in_all => false。

      【讨论】:

      • 如果您真的不希望将字段添加到索引中,为什么要首先添加它们?使用 include_in_all 选项使得这些字段不可搜索,但可以在查询过滤器中使用。如果在索引数据后将该选项添加到映射中的字段,则必须重新索引数据才能生效。否则旧映射照原样存在。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      相关资源
      最近更新 更多