PostgreSQL full text search using Rails 3 and 4 scopes
Include gem pg_search_scope
in your Gemfile
In model, use search_scope_for
class method to create full text search scope.
Examples:
search_scope_for :name
-->
search_by_name("Ivan")
In migration, use add_fulltext_index
/ remove_fulltext_index
the same way as add_index
/ remove_index
You can set additional search options:
:as - Scope name
:normalization - Controls rank behaviour, see http://www.postgresql.org/docs/9.0/static/textsearch-controls.html#TEXTSEARCH-RANKING
:wildcard - Controls search words modification:
true - add :* to ends of each search word
false - do not modify search words
:last - add :* to end of last word
:operator - Boolean operator (:and or :or) which combines search query
:select_rank - Include rank in select statement, as {scope_name}_rank
:language - Search language, e.g. 'simple' (without magic), 'english'
:rank_function - Ranking function. Valid values are 'ts_rank' and 'ts_rank_cd'
:rank_columns - If you want to sort table by rank only by specific fields - input column names here
If you use :language
option, you need to use the same option for add_fulltext_index
Examples:
search_scope_for :name, :address,
:wildcard => :last
-->
search_by_name_and_address("Ivan, Aurora st.", :select_rank => true)
...
Copyright (c) 2011 Ivan Efremov, Ilia Ablamonov, Cloud Castle Inc. See LICENSE for details.