Search code examples
ruby-on-railselasticsearchruby-on-rails-3.2tire

Rails/Tire – Prevent attribute from being indexed


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

Solution

  • 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.