pg_search 0.3.4 → 0.4

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/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  = PgSearch
2
2
 
3
+ == 0.4
4
+
5
+ * Add ability to search again tsvector columns. (Kris Hicks)
6
+
3
7
  == 0.3.4
4
8
 
5
9
  * Fix issue with {:using => {:tsearch => {:prefix => true}}} and hyphens.
data/README.rdoc CHANGED
@@ -452,11 +452,37 @@ Ignoring accents uses the {unaccent contrib package}[http://www.postgresql.org/d
452
452
  SpanishQuestion.gringo_search("Que") # => [what]
453
453
  SpanishQuestion.gringo_search("Cüåñtô") # => [how_many]
454
454
 
455
+ === Using tsvector columns
456
+
457
+ PostgreSQL allows you the ability to search against a column with type tsvector instead of using an expression; this speeds up searching dramatically as it offloads creation of the tsvector that the tsquery is evaluated against.
458
+
459
+ To use this functionality you'll need to do a few things:
460
+
461
+ * Create a column of type tsvector that you'd like to search against. If you want to search using multiple search methods, for example tsearch and dmetaphone, you'll need a column for each.
462
+ * Create a trigger function that will update the column(s) using the expression appropriate for that type of search. See: http://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
463
+ * Should you have any pre-existing data in the table, update the newly-created tsvector columns with the expression that your trigger function uses.
464
+ * Add the option to pg_search_scope, e.g:
465
+
466
+ pg_search_scope :fast_content_search,
467
+ :against => :content,
468
+ :using => {
469
+ dmetaphone: {
470
+ tsvector_column: 'tsvector_content_dmetaphone'
471
+ },
472
+ tsearch: {
473
+ dictionary: 'english',
474
+ tsvector_column: 'tsvector_content_tsearch'
475
+ }
476
+ trigram: {} # trigram does not use tsvectors
477
+ }
478
+
479
+ Please note that the :against column is only used when the tsvector_column is not present for the search type.
480
+
455
481
  == REQUIREMENTS
456
482
 
457
- * ActiveRecord 2 or 3
458
- * Postgresql
459
- * Postgresql contrib modules for certain features
483
+ * ActiveRecord 3
484
+ * PostgreSQL
485
+ * PostgreSQL contrib packages for certain features
460
486
 
461
487
  == ATTRIBUTIONS
462
488
 
@@ -62,10 +62,14 @@ module PgSearch
62
62
  end
63
63
 
64
64
  def tsdocument
65
- @columns.map do |search_column|
66
- tsvector = "to_tsvector(:dictionary, #{@normalizer.add_normalization(search_column.to_sql)})"
67
- search_column.weight.nil? ? tsvector : "setweight(#{tsvector}, #{connection.quote(search_column.weight)})"
68
- end.join(" || ")
65
+ if @options[:tsvector_column]
66
+ @options[:tsvector_column].to_s
67
+ else
68
+ @columns.map do |search_column|
69
+ tsvector = "to_tsvector(:dictionary, #{@normalizer.add_normalization(search_column.to_sql)})"
70
+ search_column.weight.nil? ? tsvector : "setweight(#{tsvector}, #{connection.quote(search_column.weight)})"
71
+ end.join(" || ")
72
+ end
69
73
  end
70
74
 
71
75
  # From http://www.postgresql.org/docs/8.3/static/textsearch-controls.html
@@ -1,3 +1,3 @@
1
1
  module PgSearch
2
- VERSION = "0.3.4"
2
+ VERSION = "0.4"
3
3
  end
@@ -597,6 +597,46 @@ describe "an ActiveRecord model which includes PgSearch" do
597
597
  end
598
598
  end
599
599
 
600
+ context "using a tsvector column" do
601
+ with_model :ModelWithPgSearchUsingTsVectorColumn do
602
+ table do |t|
603
+ t.text 'content'
604
+ t.tsvector 'content_tsvector'
605
+ end
606
+
607
+ model { include PgSearch }
608
+ end
609
+
610
+ let!(:expected) { ModelWithPgSearchUsingTsVectorColumn.create!(:content => 'tiling is grouty') }
611
+ let!(:unexpected) { ModelWithPgSearchUsingTsVectorColumn.create!(:content => 'longcat is looooooooong') }
612
+
613
+ before do
614
+ ActiveRecord::Base.connection.execute <<-SQL
615
+ UPDATE #{ModelWithPgSearchUsingTsVectorColumn.table_name}
616
+ SET content_tsvector = to_tsvector('english'::regconfig, "#{ModelWithPgSearchUsingTsVectorColumn.table_name}"."content")
617
+ SQL
618
+
619
+ ModelWithPgSearchUsingTsVectorColumn.class_eval do
620
+ pg_search_scope :search_by_content_with_tsvector,
621
+ :against => :content,
622
+ :using => {
623
+ :tsearch => {
624
+ :tsvector_column => 'content_tsvector',
625
+ :dictionary => 'english'
626
+ }
627
+ }
628
+ end
629
+ end
630
+
631
+ it "should not use to_tsvector in the query" do
632
+ ModelWithPgSearchUsingTsVectorColumn.search_by_content_with_tsvector("tiles").to_sql.should_not =~ /to_tsvector/
633
+ end
634
+
635
+ it "should find the expected result" do
636
+ ModelWithPgSearchUsingTsVectorColumn.search_by_content_with_tsvector("tiles").map(&:id).should == [expected.id]
637
+ end
638
+ end
639
+
600
640
  context "ignoring accents" do
601
641
  before do
602
642
  ModelWithPgSearch.class_eval do
data/spec/spec_helper.rb CHANGED
@@ -19,6 +19,11 @@ rescue PGError => e
19
19
  raise e
20
20
  end
21
21
 
22
+ if ENV["LOGGER"]
23
+ require "logger"
24
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
25
+ end
26
+
22
27
  def install_extension_if_missing(name, query, expected_result)
23
28
  connection = ActiveRecord::Base.connection
24
29
  result = connection.select_value(query)
metadata CHANGED
@@ -1,60 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: pg_search
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 4
10
- version: 0.3.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Case Commons, LLC
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-25 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-12-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70335492023760 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 5
29
- segments:
30
- - 3
31
- version: "3"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: activesupport
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70335492023760
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70335492023220 !ruby/object:Gem::Requirement
38
28
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 5
43
- segments:
44
- - 3
45
- version: "3"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
46
33
  type: :runtime
47
- version_requirements: *id002
48
- description: PgSearch builds ActiveRecord named scopes that take advantage of PostgreSQL's full text search
49
- email:
34
+ prerelease: false
35
+ version_requirements: *70335492023220
36
+ description: PgSearch builds ActiveRecord named scopes that take advantage of PostgreSQL's
37
+ full text search
38
+ email:
50
39
  - casecommons-dev@googlegroups.com
51
40
  executables: []
52
-
53
41
  extensions: []
54
-
55
42
  extra_rdoc_files: []
56
-
57
- files:
43
+ files:
58
44
  - .autotest
59
45
  - .gitignore
60
46
  - .rspec
@@ -97,38 +83,30 @@ files:
97
83
  - sql/unnest.sql
98
84
  homepage: https://github.com/Casecommons/pg_search
99
85
  licenses: []
100
-
101
86
  post_install_message:
102
87
  rdoc_options: []
103
-
104
- require_paths:
88
+ require_paths:
105
89
  - lib
106
- required_ruby_version: !ruby/object:Gem::Requirement
90
+ required_ruby_version: !ruby/object:Gem::Requirement
107
91
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- hash: 3
112
- segments:
113
- - 0
114
- version: "0"
115
- required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
97
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- hash: 3
121
- segments:
122
- - 0
123
- version: "0"
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
124
102
  requirements: []
125
-
126
103
  rubyforge_project:
127
104
  rubygems_version: 1.8.10
128
105
  signing_key:
129
106
  specification_version: 3
130
- summary: PgSearch builds ActiveRecord named scopes that take advantage of PostgreSQL's full text search
131
- test_files:
107
+ summary: PgSearch builds ActiveRecord named scopes that take advantage of PostgreSQL's
108
+ full text search
109
+ test_files:
132
110
  - spec/associations_spec.rb
133
111
  - spec/pg_search/document_spec.rb
134
112
  - spec/pg_search/multisearch_spec.rb