Created: 2011-07-11 19:14
Updated: 2017-09-20 10:54
License: mit

PG Search Scope ;

PostgreSQL full text search using Rails 3 and 4 scopes

Basic usage

Include gem pg_search_scope in your Gemfile

In model, use search_scope_for class method to create full text search scope.


search_scope_for :name

In migration, use add_fulltext_index / remove_fulltext_index the same way as add_index / remove_index

Advanced usage

You can set additional search options:

:as - Scope name

:normalization - Controls rank behaviour, see

: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


search_scope_for :name, :address,
                 :wildcard => :last
search_by_name_and_address("Ivan, Aurora st.", :select_rank => true)

To do



Copyright (c) 2011 Ivan Efremov, Ilia Ablamonov, Cloud Castle Inc. See LICENSE for details.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more