Module: Xapit::Membership::ClassMethods

Defined in:
lib/xapit/membership.rb

Instance Method Summary collapse

Instance Method Details

#xapit(*args) {|@xapit_index_blueprint| ... } ⇒ Object

Simply call “xapit” on a class and pass a block to define the indexed attributes.

class Article < ActiveRecord::Base
  xapit do |index|
    index.text :name, :content
    index.field :category_id
    index.facet :author_name, "Author"
    index.sortable :id, :category_id
  end
end

First we index “name” and “content” attributes for full text searching. The “category_id” field is indexed for :conditions searching. The “author_name” is indexed as a facet with “Author” being the display name of the facet. See the facets section below for details. Finally the “id” and “category_id” attributes are indexed as sortable attributes so they can be included in the :order option in a search.

Because the indexing happens in Ruby these attributes do no have to be database columns. They can be simple Ruby methods. For example, the “author_name” attribute mentioned above can be defined like this.

def author_name
  author.name
end

This way you can create a completely custom facet by simply defining your own method

You can also pass any find options to the xapit method to determine what gets indexed and improve performance with eager loading or a different batch size.

xapit(:batch_size => 100, :include => :author, :conditions => { :visible => true })

If you pass in a block you can customize how the text words will be devided (instead of by simply white space).

xapit do |index|
  index.text(:keywords) { |words| words.split(', ') }
end

You can specify a :weight option to give a text attribute more importance. This will cause search terms matching that attribute to have a higher rank. The default weight is 1. Decimal (0.5) weight values are not supported.

index.text :name, :weight => 10

Yields:

  • (@xapit_index_blueprint)


46
47
48
49
50
51
# File 'lib/xapit/membership.rb', line 46

def xapit(*args)
  @xapit_index_blueprint = IndexBlueprint.new(self, *args)
  yield(@xapit_index_blueprint)
  include AdditionalMethods
  include XapitSync::Membership if defined? XapitSync
end