Rails/Tire – Prevent attribute from being indexed

149 views Asked by At

I'm trying to create a custom mapping using Rails (3.2.8), Tire, and ElasticSearch.

I'd like "title" and "description" to be the only searchable/indexed attributes... but, I just can't seem to make this work:

Below is my model:

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

There are 2 answers

0
Olly Cruickshank On BEST ANSWER

You need to set index to 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

Using not_analysed will only prevent the field being broken down into tokens - it will still be included in the index.

See core types for more info.

1
Vamsi Krishna On

If you change the mapping, you should reindex entire data again. If it is a test data remove the indices. Create the indices and index the data again. I guess you tried to add :include_in_all => false after indexing the data.