psql_autocomplete 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/psql_autocomplete/version.rb +1 -1
- data/lib/psql_autocomplete.rb +20 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 610efb95add64e7302adbb746e7012bd0b80b47e
|
4
|
+
data.tar.gz: 19b7befff50ba4fb43d0e7c3c99706be75739bcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 899817802739de3f29196b0d96ec6bf23a1e8cf4b9bcb51669fba09d58bb7ac5ae98d6518e09431c5eff73bf56e2463f8107bfc64e15ef80dd055fe851f6d077
|
7
|
+
data.tar.gz: 8a08c76f956c0026f159d5424186f0d56c581c1172ef237a0c4e1633da37eae39f88cc67e3310bec21d964382ed7c757f820545dd8f55f6d6c3019b865b2eb44
|
data/README.md
CHANGED
@@ -31,8 +31,13 @@ class Contact < ActiveRecord::Base
|
|
31
31
|
extend PsqlAutocomplete
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
# => "
|
34
|
+
Contact.autocomplete_query('John', [:first_name, :last_name])
|
35
|
+
# => "(coalesce(lower(regexp_replace(first_name, '[:'']', '', 'g')), '') || ' ' || coalesce(lower(regexp_replace(last_name, '[:'']', '', 'g')), ''))::tsvector @@ $$'john':*$$::tsquery"
|
36
|
+
|
37
|
+
Contact.autocomplete_query('John', [:first_name, :last_name], unaccent: true)
|
38
|
+
# => => "(unaccent(coalesce(lower(regexp_replace(first_name, '[:'']', '', 'g')), '')) || ' ' || unaccent(coalesce(lower(regexp_replace(last_name, '[:'']', '', 'g')), '')))::tsvector @@ unaccent($$'john':*$$)::tsquery"
|
39
|
+
|
40
|
+
# You will need to `create extension unaccent` for this option to work
|
36
41
|
|
37
42
|
# Actual search
|
38
43
|
class Contact < ActiveRecord::Base
|
@@ -43,7 +48,7 @@ class Contact < ActiveRecord::Base
|
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
|
-
|
51
|
+
Contact.autocomplete('Jules')
|
47
52
|
# => [#<Contact:0x0x007fd2be761220>, #<Contact:0x007fd2be760938>, ...]
|
48
53
|
```
|
49
54
|
|
data/lib/psql_autocomplete.rb
CHANGED
@@ -4,27 +4,39 @@ require 'psql_autocomplete/version'
|
|
4
4
|
# on the postgresql full text search engine
|
5
5
|
module PsqlAutocomplete
|
6
6
|
# Returns a postgres FTS query injectable in a where clause
|
7
|
-
def autocomplete_query(sentence, fields)
|
8
|
-
"(#{tsvector(fields)})::tsvector @@
|
7
|
+
def autocomplete_query(sentence, fields, unaccent: false)
|
8
|
+
"(#{tsvector(fields, unaccent)})::tsvector @@ " \
|
9
|
+
"#{tsquery(sentence, unaccent)}::tsquery"
|
9
10
|
end
|
10
11
|
|
11
12
|
private
|
12
13
|
|
13
|
-
def tsvector(fields)
|
14
|
-
|
14
|
+
def tsvector(fields, unaccent)
|
15
|
+
sanitize = method(:sql_sanitize_column).curry[unaccent]
|
16
|
+
fields.map(&sanitize).join(" || ' ' || ")
|
15
17
|
end
|
16
18
|
|
17
|
-
def sql_sanitize_column(field)
|
18
|
-
"coalesce(lower(regexp_replace(#{field}, '[:'']', '', 'g')), '')"
|
19
|
+
def sql_sanitize_column(unaccent, field)
|
20
|
+
res = "coalesce(lower(regexp_replace(#{field}, '[:'']', '', 'g')), '')"
|
21
|
+
|
22
|
+
return res unless unaccent
|
23
|
+
|
24
|
+
"unaccent(#{res})"
|
19
25
|
end
|
20
26
|
|
21
|
-
def tsquery(sentence)
|
22
|
-
sentence.
|
27
|
+
def tsquery(sentence, unaccent)
|
28
|
+
query = sentence.
|
23
29
|
downcase.
|
24
30
|
split(' ').
|
25
31
|
map(&:strip).
|
26
32
|
compact.
|
27
33
|
map { |q| "'#{q.gsub("'", "''")}':*" }.
|
28
34
|
join(' & ')
|
35
|
+
|
36
|
+
query = "$$#{query}$$"
|
37
|
+
|
38
|
+
return query unless unaccent
|
39
|
+
|
40
|
+
"unaccent(#{query})"
|
29
41
|
end
|
30
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: psql_autocomplete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JobTeaser
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|