2014-11-24 127 views
1

我正嘗試使用Rails(3.2.8),Tire和ElasticSearch創建自定義映射。Rails /輪胎 - 防止屬性被索引

我想「標題」和「說明」是唯一的搜索/索引的屬性...但是,我似乎無法使這項工作:

下面是我的模型:

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 

回答

0

您需要設置indexno

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只能防止現場被分解成托克ns - 它仍將包含在索引中。

有關更多信息,請參閱core types

0

如果更改映射,則應該重新對整個數據重新進行索引。 如果是測試數據刪除索引。創建索引並重新索引數據。我想你試圖在索引數據後添加:include_in_all => false。

+0

如果你真的不希望字段被添加到索引中,爲什麼要添加它們呢? include_in_all選項用於使這些字段不可搜索,但可用於查詢過濾器。如果您在索引數據之後將該選項添加到映射字段中,則必須重新對數據進行索引以使其生效。否則舊的映射照原樣存在。 – 2014-12-16 08:24:28