pg_search_scope 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/lib/pg_search_scope/model_helper.rb +43 -40
- data/lib/pg_search_scope/version.rb +1 -1
- metadata +3 -3
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
13
|
+
:and => '&',
|
14
|
+
:or => '|'
|
14
15
|
}
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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 = "
|
74
|
+
rank = "#{scope_options[:rank_function]}(#{tsvector}, #{tsquery}, #{options[:normalization]})"
|
72
75
|
|
73
76
|
search_scope = scoped
|
74
77
|
|
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.
|
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-
|
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.
|
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
|