pg_search 1.0.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +16 -2
- data/.gitignore +1 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +17 -23
- data/CHANGELOG.md +7 -0
- data/Gemfile +0 -4
- data/LICENSE +1 -1
- data/README.md +39 -80
- data/Rakefile +8 -2
- data/lib/pg_search.rb +0 -4
- data/lib/pg_search/configuration.rb +0 -4
- data/lib/pg_search/configuration/association.rb +21 -8
- data/lib/pg_search/features/tsearch.rb +6 -29
- data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +0 -12
- data/lib/pg_search/normalizer.rb +1 -8
- data/lib/pg_search/scope_options.rb +4 -6
- data/lib/pg_search/version.rb +1 -1
- data/pg_search.gemspec +14 -12
- data/spec/integration/associations_spec.rb +9 -12
- data/spec/integration/pg_search_spec.rb +32 -50
- data/spec/lib/pg_search/configuration/association_spec.rb +96 -34
- data/spec/lib/pg_search/features/trigram_spec.rb +1 -1
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +17 -34
- data/spec/lib/pg_search/normalizer_spec.rb +32 -56
- data/spec/spec_helper.rb +2 -2
- data/spec/support/database.rb +21 -33
- metadata +38 -18
- data/lib/pg_search/compatibility.rb +0 -11
- data/lib/pg_search/extensions/arel.rb +0 -10
- data/lib/pg_search/migration/associated_against_generator.rb +0 -11
- data/lib/pg_search/migration/templates/add_pg_search_associated_against_support_functions.rb.erb +0 -21
- data/sql/array_agg.sql +0 -5
- data/sql/uninstall_array_agg.sql +0 -1
- data/sql/uninstall_unnest.sql +0 -1
- data/sql/unnest.sql +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a541ca99e3c5e843e83463598bcde7b5feb486c3
|
4
|
+
data.tar.gz: 1160f97900fbe7db8d4d0a1b1198340a3c0ccc99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a874920bde65876f83b3a7522bdc2ff0e11debc4852d9cebd186cb791c5ed3b6400dd0c891b7cd4cf3970251c06af4543cfe9e68503a423585e81247b6938857
|
7
|
+
data.tar.gz: d332008d1cc19b96004451112d9d7923d2af4a6fe1af6d5cfbd4535e1b8d1a80d9d20b3c977d2f409572ade279c17b4ae8697a9abc3ecc6e9738cdf87f433ae2
|
data/.codeclimate.yml
CHANGED
data/.rubocop.yml
CHANGED
@@ -23,7 +23,25 @@ Style/AlignParameters:
|
|
23
23
|
Style/HashSyntax:
|
24
24
|
Enabled: false
|
25
25
|
|
26
|
+
Style/NumericPredicate:
|
27
|
+
Enabled: false
|
28
|
+
|
26
29
|
Style/PercentLiteralDelimiters:
|
27
30
|
PreferredDelimiters:
|
28
31
|
'%w': '[]'
|
29
32
|
'%W': '[]'
|
33
|
+
|
34
|
+
Style/GuardClause:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/VariableNumber:
|
38
|
+
EnforcedStyle: snake_case
|
39
|
+
|
40
|
+
Bundler/OrderedGems:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Bundler/DuplicatedGem:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/EmptyMethod:
|
47
|
+
EnforcedStyle: expanded
|
data/.travis.yml
CHANGED
@@ -2,42 +2,36 @@ language: ruby
|
|
2
2
|
sudo: false
|
3
3
|
|
4
4
|
rvm:
|
5
|
-
- "2.3.
|
6
|
-
- "2.2"
|
7
|
-
- "2.1"
|
8
|
-
-
|
9
|
-
- "1.9"
|
10
|
-
- jruby-19mode
|
11
|
-
- jruby-9.0.0.0
|
5
|
+
- "2.3.3"
|
6
|
+
- "2.2.6"
|
7
|
+
- "2.1.10"
|
8
|
+
- jruby-9.1.6.0
|
12
9
|
|
13
10
|
env:
|
14
11
|
- ACTIVE_RECORD_BRANCH="master"
|
12
|
+
- ACTIVE_RECORD_BRANCH="5-0-stable"
|
15
13
|
- ACTIVE_RECORD_BRANCH="4-2-stable"
|
16
|
-
-
|
17
|
-
- ACTIVE_RECORD_BRANCH="4-0-stable"
|
14
|
+
- ACTIVE_RECORD_VERSION="~> 5.0.0"
|
18
15
|
- ACTIVE_RECORD_VERSION="~> 4.2.0"
|
19
|
-
- ACTIVE_RECORD_VERSION="~> 4.1.0"
|
20
|
-
- ACTIVE_RECORD_VERSION="~> 4.0.0"
|
21
|
-
- ACTIVE_RECORD_VERSION="~> 3.2.0"
|
22
|
-
- ACTIVE_RECORD_VERSION="~> 3.1.0"
|
23
16
|
|
24
17
|
matrix:
|
25
18
|
allow_failures:
|
26
19
|
- env: ACTIVE_RECORD_BRANCH="master"
|
20
|
+
- env: ACTIVE_RECORD_BRANCH="5-0-stable"
|
27
21
|
- env: ACTIVE_RECORD_BRANCH="4-2-stable"
|
28
|
-
- env: ACTIVE_RECORD_BRANCH="4-1-stable"
|
29
|
-
- env: ACTIVE_RECORD_BRANCH="4-0-stable"
|
30
22
|
exclude:
|
31
|
-
- rvm: "1.
|
23
|
+
- rvm: "2.1.10"
|
32
24
|
env: ACTIVE_RECORD_BRANCH="master"
|
33
|
-
- rvm: "2.
|
34
|
-
env: ACTIVE_RECORD_BRANCH="
|
35
|
-
- rvm: "2.1"
|
36
|
-
env:
|
37
|
-
- rvm: jruby-
|
38
|
-
env: ACTIVE_RECORD_BRANCH="master"
|
39
|
-
- rvm: jruby-9.0.0.0
|
25
|
+
- rvm: "2.1.10"
|
26
|
+
env: ACTIVE_RECORD_BRANCH="5-0-stable"
|
27
|
+
- rvm: "2.1.10"
|
28
|
+
env: ACTIVE_RECORD_VERSION="~> 5.0.0"
|
29
|
+
- rvm: jruby-9.1.6.0
|
40
30
|
env: ACTIVE_RECORD_BRANCH="master"
|
31
|
+
- rvm: jruby-9.1.6.0
|
32
|
+
env: ACTIVE_RECORD_BRANCH="5-0-stable"
|
33
|
+
- rvm: jruby-9.1.6.0
|
34
|
+
env: ACTIVE_RECORD_VERSION="~> 5.0.0"
|
41
35
|
|
42
36
|
before_script:
|
43
37
|
- "psql -c 'create database pg_search_test;' -U postgres >/dev/null"
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# pg_search changelog
|
2
2
|
|
3
|
+
## 2.0.0
|
4
|
+
* Drop support for PostgreSQL < 9.2.
|
5
|
+
* Drop support for Active Record < 4.2.
|
6
|
+
* Drop support for Ruby < 2.2.
|
7
|
+
* Improve performance of has_one and belongs_to associations. (Peter Postma)
|
8
|
+
|
3
9
|
## 1.0.6
|
10
|
+
|
4
11
|
* Add support for highlighting the matching portion of a search result. (Jose Galisteo)
|
5
12
|
* Add `:update_if` option to control when PgSearch::Document gets updated. (Adam Becker)
|
6
13
|
* Add `:additional_attributes` option for adding additional attributes to PgSearch::Document
|
data/Gemfile
CHANGED
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2010
|
1
|
+
Copyright (c) 2010-2016 Case Commons, Inc. <http://casecommons.org>
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -18,54 +18,24 @@ Read the blog post introducing PgSearch at http://blog.pivotal.io/labs/labs/pg-s
|
|
18
18
|
|
19
19
|
## REQUIREMENTS
|
20
20
|
|
21
|
-
* Ruby
|
22
|
-
*
|
23
|
-
* PostgreSQL
|
24
|
-
* [PostgreSQL
|
25
|
-
features](https://github.com/Casecommons/pg_search/wiki/Installing-Postgres-Contrib-Modules)
|
26
|
-
|
21
|
+
* Ruby 2.1+
|
22
|
+
* ActiveRecord 4.2+
|
23
|
+
* PostgreSQL 9.2+
|
24
|
+
* [PostgreSQL extensions](https://github.com/Casecommons/pg_search/wiki/Installing-PostgreSQL-Extensions) for certain features
|
27
25
|
|
28
26
|
## INSTALL
|
29
27
|
|
30
|
-
```
|
28
|
+
```
|
31
29
|
$ gem install pg_search
|
32
30
|
```
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
In Gemfile
|
32
|
+
or add this line to your Gemfile:
|
37
33
|
|
38
34
|
```ruby
|
39
35
|
gem 'pg_search'
|
40
36
|
```
|
41
37
|
|
42
|
-
### Rails
|
43
|
-
|
44
|
-
The newest versions of PgSearch no longer support Rails 3.0. However, the 0.5
|
45
|
-
series still works. It's not actively maintained, but submissions are welcome
|
46
|
-
for backports and bugfixes.
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
gem 'pg_search', "~> 0.5.7"
|
50
|
-
```
|
51
|
-
|
52
|
-
The 0.5 branch lives at
|
53
|
-
https://github.com/Casecommons/pg_search/tree/0.5-stable
|
54
|
-
|
55
|
-
### Rails 2
|
56
|
-
|
57
|
-
The newest versions of PgSearch no longer support Rails 2. However, the 0.2
|
58
|
-
series still works. It's not actively maintained, but submissions are welcome
|
59
|
-
for backports and bugfixes.
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
gem 'pg_search', "~> 0.2.0"
|
63
|
-
```
|
64
|
-
|
65
|
-
The 0.2 branch lives at
|
66
|
-
https://github.com/Casecommons/pg_search/tree/0.2-stable
|
67
|
-
|
68
|
-
### Other Active Record projects
|
38
|
+
### Non-Rails projects
|
69
39
|
|
70
40
|
In addition to installing and requiring the gem, you may want to include the
|
71
41
|
PgSearch rake tasks in your Rakefile. This isn't necessary for Rails projects,
|
@@ -75,19 +45,6 @@ which gain the Rake tasks via a Railtie.
|
|
75
45
|
load "pg_search/tasks.rb"
|
76
46
|
```
|
77
47
|
|
78
|
-
### Ruby 1.8.7 or earlier
|
79
|
-
|
80
|
-
The newest versions of PgSearch no longer support Ruby 1.8.7. However, the 0.6
|
81
|
-
series still works. It's not actively maintained, but submissions are welcome
|
82
|
-
for backports and bugfixes.
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
gem 'pg_search', "~> 0.6.4"
|
86
|
-
```
|
87
|
-
|
88
|
-
The 0.6 branch lives at
|
89
|
-
https://github.com/Casecommons/pg_search/tree/0.6-stable
|
90
|
-
|
91
48
|
## USAGE
|
92
49
|
|
93
50
|
To add PgSearch to an Active Record model, simply include the PgSearch module.
|
@@ -287,7 +244,17 @@ To regenerate the documents for a given class, run:
|
|
287
244
|
PgSearch::Multisearch.rebuild(Product)
|
288
245
|
```
|
289
246
|
|
290
|
-
|
247
|
+
The ```rebuild``` method will delete all the documents for the given class
|
248
|
+
before regenerating them. In some situations this may not be desirable,
|
249
|
+
such as when you're using single-table inheritance and ```searchable_type```
|
250
|
+
is your base class. You can prevent ```rebuild``` from deleting your records
|
251
|
+
like so:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
PgSearch::Multisearch.rebuild(Product, false)
|
255
|
+
```
|
256
|
+
|
257
|
+
Rebuild is also available as a Rake task, for convenience.
|
291
258
|
|
292
259
|
$ rake pg_search:multisearch:rebuild[BlogPost]
|
293
260
|
|
@@ -439,15 +406,6 @@ It is possible to search columns on associated models. Note that if you do
|
|
439
406
|
this, it will be impossible to speed up searches with database indexes.
|
440
407
|
However, it is supported as a quick way to try out cross-model searching.
|
441
408
|
|
442
|
-
In PostgreSQL 8.3 and earlier, you must install a utility function into your
|
443
|
-
database. To generate and run a migration for this, run:
|
444
|
-
|
445
|
-
$ rails g pg_search:migration:associated_against
|
446
|
-
$ rake db:migrate
|
447
|
-
|
448
|
-
This migration is safe to run against newer versions of PostgreSQL as well. It
|
449
|
-
will essentially do nothing.
|
450
|
-
|
451
409
|
You can pass a Hash into the :associated_against option to set up searching
|
452
410
|
through associations. The keys are the names of the associations and the value
|
453
411
|
works just like an :against option for the other model. Right now, searching
|
@@ -505,11 +463,10 @@ end
|
|
505
463
|
|
506
464
|
The currently implemented features are
|
507
465
|
|
508
|
-
* :tsearch - [Full text search](http://www.postgresql.org/docs/current/static/textsearch-intro.html)
|
509
|
-
(built-in with 8.3 and later, available as a contrib package for some earlier versions)
|
466
|
+
* :tsearch - [Full text search](http://www.postgresql.org/docs/current/static/textsearch-intro.html), which is built-in to PostgreSQL
|
510
467
|
* :trigram - [Trigram search](http://www.postgresql.org/docs/current/static/pgtrgm.html), which
|
511
|
-
requires the trigram
|
512
|
-
* :dmetaphone - [Double Metaphone search](http://www.postgresql.org/docs/
|
468
|
+
requires the trigram extension
|
469
|
+
* :dmetaphone - [Double Metaphone search](http://www.postgresql.org/docs/current/static/fuzzystrmatch.html#AEN177521), which requires the fuzzystrmatch extension
|
513
470
|
|
514
471
|
|
515
472
|
#### :tsearch (Full Text Search)
|
@@ -736,7 +693,7 @@ one_close = Person.create!(:name => 'leigh heinz')
|
|
736
693
|
Person.search('ash hines') # => [exact, one_exact_one_close, one_exact]
|
737
694
|
```
|
738
695
|
|
739
|
-
##### :highlight
|
696
|
+
##### :highlight
|
740
697
|
|
741
698
|
Adding .with_pg_search_highlight after the pg_search_scope you can access to
|
742
699
|
`pg_highlight` attribute for each object.
|
@@ -749,8 +706,10 @@ class Person < ActiveRecord::Base
|
|
749
706
|
:against => :bio,
|
750
707
|
:using => {
|
751
708
|
:tsearch => {
|
752
|
-
:
|
753
|
-
|
709
|
+
:highlight => {
|
710
|
+
:start_sel => '<b>',
|
711
|
+
:stop_sel => '</b>'
|
712
|
+
}
|
754
713
|
}
|
755
714
|
}
|
756
715
|
end
|
@@ -773,10 +732,10 @@ Currently, this is not a true double-metaphone, as only the first metaphone is
|
|
773
732
|
used for searching.
|
774
733
|
|
775
734
|
Double Metaphone support is currently available as part of the [fuzzystrmatch
|
776
|
-
|
735
|
+
extension](http://www.postgresql.org/docs/current/static/fuzzystrmatch.html)
|
777
736
|
that must be installed before this feature can be used. In addition to the
|
778
|
-
|
779
|
-
|
737
|
+
extension, you must install a utility function into your database. To generate
|
738
|
+
and run a migration for this, run:
|
780
739
|
|
781
740
|
$ rails g pg_search:migration:dmetaphone
|
782
741
|
$ rake db:migrate
|
@@ -810,9 +769,9 @@ Trigram search works by counting how many three-letter substrings (or
|
|
810
769
|
Trigram search has some ability to work even with typos and misspellings in
|
811
770
|
the query or text.
|
812
771
|
|
813
|
-
Trigram support is currently available as part of the
|
814
|
-
|
815
|
-
|
772
|
+
Trigram support is currently available as part of the
|
773
|
+
[pg_trgm extension](http://www.postgresql.org/docs/current/static/pgtrgm.html) that must be installed before this
|
774
|
+
feature can be used.
|
816
775
|
|
817
776
|
```ruby
|
818
777
|
class Website < ActiveRecord::Base
|
@@ -902,15 +861,14 @@ Image.combined_search('reasonable') # found with tsearch
|
|
902
861
|
Image.combined_search('foo') # found with trigram
|
903
862
|
```
|
904
863
|
|
905
|
-
### Ignoring accent marks
|
864
|
+
### Ignoring accent marks
|
906
865
|
|
907
866
|
Most of the time you will want to ignore accent marks when searching. This
|
908
867
|
makes it possible to find words like "piñata" when searching with the query
|
909
868
|
"pinata". If you set a pg_search_scope to ignore accents, it will ignore
|
910
869
|
accents in both the searchable text and the query terms.
|
911
870
|
|
912
|
-
Ignoring accents uses the [unaccent
|
913
|
-
package](http://www.postgresql.org/docs/current/static/unaccent.html) that
|
871
|
+
Ignoring accents uses the [unaccent extension](http://www.postgresql.org/docs/current/static/unaccent.html) that
|
914
872
|
must be installed before this feature can be used.
|
915
873
|
|
916
874
|
```ruby
|
@@ -930,8 +888,8 @@ SpanishQuestion.gringo_search("Cüåñtô") # => [how_many]
|
|
930
888
|
```
|
931
889
|
|
932
890
|
Advanced users may wish to add indexes for the expressions that pg_search
|
933
|
-
generates. Unfortunately, the unaccent function supplied by this
|
934
|
-
|
891
|
+
generates. Unfortunately, the unaccent function supplied by this extension
|
892
|
+
is not indexable (as of PostgreSQL 9.1). Thus, you may want to write
|
935
893
|
your own wrapper function and use it instead. This can be configured by
|
936
894
|
calling the following code, perhaps in an initializer.
|
937
895
|
|
@@ -968,7 +926,7 @@ To use this functionality you'll need to do a few things:
|
|
968
926
|
tsearch: {
|
969
927
|
dictionary: 'english',
|
970
928
|
tsvector_column: 'tsvector_content_tsearch'
|
971
|
-
}
|
929
|
+
},
|
972
930
|
trigram: {} # trigram does not use tsvectors
|
973
931
|
}
|
974
932
|
```
|
@@ -1123,4 +1081,5 @@ Please read our [CONTRIBUTING guide](https://github.com/Casecommons/pg_search/bl
|
|
1123
1081
|
|
1124
1082
|
## LICENSE
|
1125
1083
|
|
1126
|
-
|
1084
|
+
Copyright © 2010–2016 [Case Commons, Inc](http://casecommons.org).
|
1085
|
+
Licensed under the MIT license, see [LICENSE](/LICENSE) file.
|
data/Rakefile
CHANGED
@@ -5,6 +5,12 @@ require 'rspec/core/rake_task'
|
|
5
5
|
RSpec::Core::RakeTask.new(:spec)
|
6
6
|
|
7
7
|
require "rubocop/rake_task"
|
8
|
-
RuboCop::RakeTask.new
|
8
|
+
RuboCop::RakeTask.new do |t|
|
9
|
+
t.options = %w[--display-cop-names]
|
10
|
+
end
|
9
11
|
|
10
|
-
task :
|
12
|
+
task :codeclimate do
|
13
|
+
sh 'bin/codeclimate-test-reporter' if ENV['CODECLIMATE_REPO_TOKEN']
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => %w[spec codeclimate rubocop]
|
data/lib/pg_search.rb
CHANGED
@@ -3,9 +3,7 @@ require "active_support/concern"
|
|
3
3
|
require "active_support/core_ext/module/attribute_accessors"
|
4
4
|
require "active_support/core_ext/string/strip"
|
5
5
|
|
6
|
-
require "pg_search/compatibility"
|
7
6
|
require "pg_search/configuration"
|
8
|
-
require "pg_search/extensions/arel"
|
9
7
|
require "pg_search/features"
|
10
8
|
require "pg_search/multisearch"
|
11
9
|
require "pg_search/multisearchable"
|
@@ -92,8 +90,6 @@ module PgSearch
|
|
92
90
|
end
|
93
91
|
end
|
94
92
|
|
95
|
-
class NotSupportedForPostgresqlVersion < StandardError; end
|
96
|
-
|
97
93
|
class PgSearchRankNotSelected < StandardError
|
98
94
|
def message
|
99
95
|
"You must chain .with_pg_search_rank after the pg_search_scope to access the pg_search_rank attribute on returned records" # rubocop:disable Metrics/LineLength
|
@@ -28,20 +28,33 @@ module PgSearch
|
|
28
28
|
private
|
29
29
|
|
30
30
|
def selects
|
31
|
-
|
31
|
+
if singular_association?
|
32
|
+
selects_for_singular_association
|
33
|
+
else
|
34
|
+
selects_for_multiple_association
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def selects_for_singular_association
|
39
|
+
columns.map do |column|
|
40
|
+
"#{column.full_name}::text AS #{column.alias}"
|
41
|
+
end.join(", ")
|
42
|
+
end
|
32
43
|
|
44
|
+
def selects_for_multiple_association
|
33
45
|
columns.map do |column|
|
34
|
-
|
35
|
-
when 0..90000
|
36
|
-
"array_to_string(array_agg(#{column.full_name}::text), ' ') AS #{column.alias}"
|
37
|
-
else
|
38
|
-
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
|
39
|
-
end
|
46
|
+
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
|
40
47
|
end.join(", ")
|
41
48
|
end
|
42
49
|
|
43
50
|
def relation(primary_key)
|
44
|
-
@model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
|
51
|
+
result = @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
|
52
|
+
result = result.group(primary_key) unless singular_association?
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
def singular_association?
|
57
|
+
[:has_one, :belongs_to].include?(@model.reflect_on_association(@name).macro)
|
45
58
|
end
|
46
59
|
end
|
47
60
|
end
|