pg_search_scope 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -36,6 +36,8 @@ You can set additional search options:
36
36
 
37
37
  :language - Search language, e.g. 'simple' (without magic), 'english'
38
38
 
39
+ :rank_function - Ranking function. Valid values are 'ts_rank' and 'ts_rank_cd'
40
+
39
41
  If you use `:language` option, you need to use the same option for `add_fulltext_index`
40
42
 
41
43
  Examples:
@@ -1,48 +1,51 @@
1
1
  module PgSearchScope
2
2
  module ModelHelper
3
3
  DEFAULT_OPTIONS = {
4
- :as => nil,
5
- :wildcard => true,
6
- :operator => :and,
7
- :normalization => 0,
8
- :select_rank => false,
9
- :language => 'simple'
4
+ :as => nil,
5
+ :wildcard => true,
6
+ :operator => :and,
7
+ :normalization => 0,
8
+ :select_rank => false,
9
+ :language => 'simple',
10
+ :rank_function => :ts_rank
10
11
  }
11
12
  OPERATORS = {
12
- :and => '&',
13
- :or => '|'
13
+ :and => '&',
14
+ :or => '|'
14
15
  }
15
16
 
16
- # Creates fulltext search scope
17
- #
18
- # == Options
19
- #
20
- # * <tt>:as</tt> - Scope name
21
- #
22
- # * <tt>:normalization</tt> - Controls rank behaviour, see http://www.postgresql.org/docs/9.0/static/textsearch-controls.html#TEXTSEARCH-RANKING
23
- #
24
- # * <tt>:wildcard</tt> - Controls search words modification:
25
- # true - add :* to ends of each search word
26
- # false - do not modify search words
27
- # :last - add :* to end of last word
28
- #
29
- # * <tt>:operator</tt> - Boolean operator (:and or :or) which combines search query
30
- #
31
- # * <tt>:select_rank</tt> - Include rank in select statement, as {scope_name}_rank
32
- #
33
- # * <tt>:language</tt> - Search language, e.g. 'simple' (without magic), 'english'
34
- #
35
- # == Usage
36
- #
37
- # search_scope_for :name
38
- # -->
39
- # search_by_name("Ivan")
40
- #
41
- # search_scope_for :name, :address,
42
- # :wildcard => :last
43
- # -->
44
- # search_by_name_and_address("Ivan, Aurora st.", :select_rank => true)
45
- #
17
+ # Creates fulltext search scope
18
+ #
19
+ # == Options
20
+ #
21
+ # * <tt>:as</tt> - Scope name
22
+ #
23
+ # * <tt>:normalization</tt> - Controls rank behaviour, see http://www.postgresql.org/docs/9.0/static/textsearch-controls.html#TEXTSEARCH-RANKING
24
+ #
25
+ # * <tt>:wildcard</tt> - Controls search words modification:
26
+ # true - add :* to ends of each search word
27
+ # false - do not modify search words
28
+ # :last - add :* to end of last word
29
+ #
30
+ # * <tt>:operator</tt> - Boolean operator (:and or :or) which combines search query
31
+ #
32
+ # * <tt>:select_rank</tt> - Include rank in select statement, as {scope_name}_rank
33
+ #
34
+ # * <tt>:language</tt> - Search language, e.g. 'simple' (without magic), 'english'
35
+ # * <tt>:rank_function</tt> - Ranking function. Valid values are 'ts_rank' and 'ts_rank_cd'
36
+ #
37
+ # == Usage
38
+ #
39
+ # search_scope_for :name
40
+ # -->
41
+ # search_by_name("Ivan")
42
+ #
43
+ # search_scope_for :name, :address,
44
+ # :wildcard => :last
45
+ # -->
46
+ # search_by_name_and_address("Ivan, Aurora st.", :select_rank => true)
47
+ #
48
+
46
49
  def search_scope_for *column_names
47
50
  scope_options = DEFAULT_OPTIONS.merge column_names.extract_options!
48
51
 
@@ -52,7 +55,7 @@ module PgSearchScope
52
55
  options = scope_options.merge(options || {})
53
56
  search_string ||= ''
54
57
 
55
- terms = search_string.scan(/'*([\p{Lu}\p{Ll}\d\.']+)/u).map {|s,_| s.gsub /'/, "''"}
58
+ terms = search_string.scan(/'*([\p{Lu}\p{Ll}\d\.'@]+)/u).map { |s, _| s.gsub /'/, "''" }
56
59
 
57
60
  if terms.present?
58
61
  prefix = arel_table.table_alias || arel_table.name
@@ -68,7 +71,7 @@ module PgSearchScope
68
71
  tsvector = "to_tsvector('#{options[:language]}', #{document})"
69
72
  tsquery = "to_tsquery('#{options[:language]}', '#{terms.join(" #{OPERATORS[options[:operator]]} ")}')"
70
73
 
71
- rank = "ts_rank(#{tsvector}, #{tsquery}, #{options[:normalization]})"
74
+ rank = "#{scope_options[:rank_function]}(#{tsvector}, #{tsquery}, #{options[:normalization]})"
72
75
 
73
76
  search_scope = scoped
74
77
 
@@ -1,3 +1,3 @@
1
1
  module PgSearchScope
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_search_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-13 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ''
15
15
  email:
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: '0'
50
50
  requirements: []
51
51
  rubyforge_project: pg_search_scope
52
- rubygems_version: 1.8.11
52
+ rubygems_version: 1.8.21
53
53
  signing_key:
54
54
  specification_version: 3
55
55
  summary: PostgreSQL full text search using Rails 3 scopes