Rails / Tire - 防止属性被索引

时间:2014-11-24 19:38:17

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

我尝试使用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

2 个答案:

答案 0 :(得分:0)

您需要将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

答案 1 :(得分:0)

如果更改映射,则应重新重新索引整个数据。 如果是测试数据,请删除索引。创建索引并再次索引数据。我想你试图添加:include_in_all =&gt;索引数据后为false。