| Module | Sphincter::Search |
| In: |
lib/sphincter/search.rb
|
Search extension for ActiveRecord::Base that automatically extends ActiveRecord::Base.
| Results | = | Struct.new :records, :total, :per_page |
Struct to hold search results.
|
Adds an index with options.
add_index will automatically add Sphincter::AssociationSearcher to any has_many associations referenced by this model‘s belongs_to associations. If this model belongs_to another model, and that model has_many of this model, then you will be able to other.models.search and recieve only records in the association.
Currently, only has_many associations without conditions will have AssociationSearcher appended.
Options are:
| :name: | Name of index. Defaults to ActiveRecord::Base::table_name. |
| :fields: | Array of fields to index. Foreign key columns for belongs_to associations are automatically added. Fields from associations may be included by using "association.field". |
| :conditions: | Array of SQL conditions that will be ANDed together to predicate inclusion in the search index. |
Example:
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :blog
has_many :comments
add_index :fields => %w[title body user.name, comments.body],
:conditions => ['published = 1']
end
When including fields from associations, MySQL‘s GROUP_CONCAT() function is used. By default this will create a string up to 1024 characters long. A larger string can be used by changing the value of MySQL‘s group_concat_max_len variable. To do this, add the following to your sphincter.RAILS_ENV.yml files:
mysql:
sql_query_pre:
- SET NAMES utf8
- SET SESSION group_concat_max_len = VALUE
Searches for query with options.
Allowed options are:
| :between: | Hash of Sphinx range filter conditions. Hash keys are sphinx group_column or date_column names. Values can be Date/Time/DateTime or Integers. |
| :conditions: | Hash of Sphinx value filter conditions. Hash keys are sphinx group_column or date_column names. Values can be a single value or an Array of values. |
| :index: | Name of Sphinx index to search. Defaults to ActiveRecord::Base::table_name. |
| :page: | Page offset of records to return, for easy use with paginators. |
| :per_page: | Size of a page. Default page size is controlled by the configuration. |
Returns a Sphincter::Search::Results object.