pg_search 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Hutchins
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-05 00:00:00.000000000 Z
12
+ date: 2013-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -67,20 +67,6 @@ dependencies:
67
67
  - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: rdoc
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - '>='
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - '>='
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
70
  - !ruby/object:Gem::Dependency
85
71
  name: pry
86
72
  requirement: !ruby/object:Gem::Requirement
@@ -101,14 +87,14 @@ dependencies:
101
87
  requirements:
102
88
  - - '>='
103
89
  - !ruby/object:Gem::Version
104
- version: '0'
90
+ version: '2.14'
105
91
  type: :development
106
92
  prerelease: false
107
93
  version_requirements: !ruby/object:Gem::Requirement
108
94
  requirements:
109
95
  - - '>='
110
96
  - !ruby/object:Gem::Version
111
- version: '0'
97
+ version: '2.14'
112
98
  - !ruby/object:Gem::Dependency
113
99
  name: with_model
114
100
  requirement: !ruby/object:Gem::Requirement
@@ -123,20 +109,6 @@ dependencies:
123
109
  - - '>='
124
110
  - !ruby/object:Gem::Version
125
111
  version: '0'
126
- - !ruby/object:Gem::Dependency
127
- name: will_paginate
128
- requirement: !ruby/object:Gem::Requirement
129
- requirements:
130
- - - '>='
131
- - !ruby/object:Gem::Version
132
- version: '0'
133
- type: :development
134
- prerelease: false
135
- version_requirements: !ruby/object:Gem::Requirement
136
- requirements:
137
- - - '>='
138
- - !ruby/object:Gem::Version
139
- version: '0'
140
112
  description: PgSearch builds Active Record named scopes that take advantage of PostgreSQL's
141
113
  full text search
142
114
  email:
@@ -150,11 +122,12 @@ files:
150
122
  - .gitignore
151
123
  - .rspec
152
124
  - .travis.yml
153
- - CHANGELOG.rdoc
125
+ - CHANGELOG.md
126
+ - CONTRIBUTING.md
154
127
  - Gemfile
155
128
  - Guardfile
156
129
  - LICENSE
157
- - README.rdoc
130
+ - README.md
158
131
  - Rakefile
159
132
  - lib/pg_search.rb
160
133
  - lib/pg_search/configuration.rb
data/CHANGELOG.rdoc DELETED
@@ -1,178 +0,0 @@
1
- = PgSearch
2
-
3
- == 0.7.0
4
-
5
- * Start requiring Ruby 1.9.2 or later
6
-
7
- == 0.6.4
8
-
9
- * Fix issue with using more than two features in the same scope
10
-
11
- == 0.6.3
12
-
13
- * Fix issues and deprecations for Active Record 4.0.0.rc1
14
-
15
- == 0.6.2
16
-
17
- * Add workaround for issue with how ActiveRecord relations handle Arel OR
18
- nodes.
19
-
20
- == 0.6.1
21
-
22
- * Fix issue with Arel::InfixOperation that prevented #count from working,
23
- breaking pagination.
24
-
25
- == 0.6.0
26
-
27
- * Drop support for Active Record 3.0.
28
-
29
- * Address warnings in Ruby 2.0.
30
-
31
- * Remove all usages of sanitize_sql_array for future Rails 4 compatibility.
32
-
33
- * Start using Arel internally to build SQL strings (not yet complete).
34
-
35
- * Disable eager loading, fixes issue #14.
36
-
37
- * Support named schemas in pg_search:multisearch:rebuild. (Victor Olteanu)
38
-
39
- == 0.5.7
40
-
41
- * Fix issue with eager loading now that the Scope class has been removed. (Piotr Murach)
42
-
43
- == 0.5.6
44
-
45
- * PgSearch#multisearchable accepts :if and :unless for conditional inclusion
46
- in search documents table. (Francois Harbec)
47
-
48
- * Stop using array_to_string() in SQL since it is not indexable.
49
-
50
- == 0.5.5
51
-
52
- * Fix bug with single table inheritance.
53
-
54
- * Allow option for specifying an alternate function for unaccent().
55
-
56
- == 0.5.4
57
-
58
- * Fix bug in associated_against join clause when search scope is chained after other scopes.
59
-
60
- * Fix autoloading of PgSearch::VERSION constant.
61
-
62
- == 0.5.3
63
-
64
- * Prevent multiple attempts to create pg_search_document within a single transaction. (JT Archie & Trace Wax)
65
-
66
- == 0.5.2
67
-
68
- * Don't save twice if pg_search_document is missing on update.
69
-
70
- == 0.5.1
71
-
72
- * Add ability to override multisearch rebuild SQL.
73
-
74
- == 0.5
75
-
76
- * Convert migration rake tasks into generators.
77
-
78
- * Use rake task arguments for multisearch rebuild instead of environment
79
- variable.
80
-
81
- * Always cast columns to text.
82
-
83
- == 0.4.2
84
-
85
- * Fill in timestamps correctly when rebuilding multisearch documents. (Barton McGuire)
86
-
87
- * Fix various issues with rebuilding multisearch documents. (Eugen Neagoe)
88
-
89
- * Fix syntax error in pg_search_dmetaphone() migration. (Casey Foster)
90
-
91
- * Rename PgSearch#rank to PgSearch#pg_search_rank and always return a Float.
92
-
93
- * Fix issue with :associated_against and non-text columns.
94
-
95
- == 0.4.1
96
-
97
- * Fix Active Record 3.2 deprecation warnings. (Steven Harman)
98
-
99
- * Fix issue with undefined logger when PgSearch::Document.search is already defined.
100
-
101
- == 0.4
102
-
103
- * Add ability to search again tsvector columns. (Kris Hicks)
104
-
105
- == 0.3.4
106
-
107
- * Fix issue with {:using => {:tsearch => {:prefix => true}}} and hyphens.
108
-
109
- * Get tests running against PostgreSQL 9.1 by using CREATE EXTENSION
110
-
111
- == 0.3.3
112
-
113
- * Backport array_agg() aggregate function to PostgreSQL 8.3 and earlier.
114
- This fixes :associated_against searches.
115
-
116
- * Backport unnest() function to PostgreSQL 8.3 and earlier.
117
- This fixes {:using => :dmetaphone} searches.
118
-
119
- * Disable {:using => {:tsearch => {:prefix => true}}} in PostgreSQL 8.3 and earlier.
120
-
121
- == 0.3.2
122
-
123
- * Fix :prefix search in PostgreSQL 8.x
124
-
125
- * Disable {:ignoring => :accents} in PostgreSQL 8.x
126
-
127
- == 0.3.1
128
-
129
- * Fix syntax error in generated dmetaphone migration. (Max De Marzi)
130
-
131
- == 0.3
132
-
133
- * Drop Active Record 2.0 support.
134
-
135
- * Add PgSearch.multisearch for cross-model searching.
136
-
137
- * Fix PostgreSQL warnings about truncated identifiers
138
-
139
- * Support specifying a method of rank normalisation when using tsearch. (Arthur Gunn)
140
-
141
- * Add :any_word option to :tsearch which uses OR between query terms instead of AND. (Fernando Espinosa)
142
-
143
- == 0.2.1
144
-
145
- * Backport support for searching against tsvector columns (Kris Hicks)
146
-
147
- == 0.2
148
-
149
- * Set dictionary to :simple by default for :tsearch. Before it was unset,
150
- which would fall back to PostgreSQL's default dictionary, usually
151
- "english".
152
-
153
- * Fix a bug with search strings containing a colon ":"
154
-
155
- * Improve performance of :associated_against by only doing one INNER JOIN per
156
- association
157
-
158
- == 0.1.1
159
-
160
- * Fix a bug with dmetaphone searches containing " w " (which dmetaphone maps
161
- to an empty string)
162
-
163
- == 0.1
164
-
165
- * Change API to {:ignoring => :accents} from {:normalizing => :diacritics}
166
-
167
- * Improve documentation
168
-
169
- * Fix bug where :associated_against would not work without an :against
170
- present
171
-
172
- == 0.0.2
173
-
174
- * Fix gem ownership.
175
-
176
- == 0.0.1
177
-
178
- * Initial release.
data/README.rdoc DELETED
@@ -1,669 +0,0 @@
1
- = pg_search
2
-
3
- * http://github.com/casecommons/pg_search/
4
-
5
- {<img src="https://secure.travis-ci.org/Casecommons/pg_search.png?branch=master" alt="Build Status" />}[http://travis-ci.org/Casecommons/pg_search]
6
- {<img src="https://gemnasium.com/Casecommons/pg_search.png" alt="Dependency Status" />}[https://gemnasium.com/Casecommons/pg_search]
7
- {<img src="https://codeclimate.com/github/Casecommons/pg_search.png" />}[https://codeclimate.com/github/Casecommons/pg_search]
8
- {<img src="https://coveralls.io/repos/Casecommons/pg_search/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/Casecommons/pg_search]
9
- {<img src="https://badge.fury.io/rb/pg_search.png" alt="Gem Version" />}[http://badge.fury.io/rb/pg_search]
10
-
11
- == DESCRIPTION
12
-
13
- PgSearch builds named scopes that take advantage of PostgreSQL's full text search
14
-
15
- Read the blog post introducing PgSearch at http://pivotallabs.com/pg-search/
16
-
17
- == REQUIREMENTS
18
-
19
- * Ruby 1.9.2 or later
20
- * Active Record 3.1 or later
21
- * PostgreSQL
22
- * {PostgreSQL contrib packages for certain features}[https://github.com/Casecommons/pg_search/wiki/Installing-Postgres-Contrib-Modules]
23
-
24
- == INSTALL
25
-
26
- gem install pg_search
27
-
28
- === Rails 3.1 or later, Ruby 1.9.2 or later
29
-
30
- In Gemfile
31
-
32
- gem 'pg_search'
33
-
34
- === Rails 3.0
35
-
36
- The newest versions of PgSearch no longer support Rails 3.0. However, the 0.5 series still works. It's not actively maintained, but submissions are welcome for backports and bugfixes.
37
-
38
- gem 'pg_search', "~> 0.5.7"
39
-
40
- The 0.5 branch lives at https://github.com/Casecommons/pg_search/tree/0.5-stable
41
-
42
- === Rails 2
43
-
44
- The newest versions of PgSearch no longer support Rails 2. However, the 0.2 series still works. It's not actively maintained, but submissions are welcome for backports and bugfixes.
45
-
46
- gem 'pg_search', "~> 0.2.0"
47
-
48
- The 0.2 branch lives at https://github.com/Casecommons/pg_search/tree/0.2-stable
49
-
50
- === Other Active Record projects
51
-
52
- In addition to installing and requiring the gem, you may want to include the PgSearch rake tasks in your Rakefile. This isn't necessary for Rails projects, which gain the Rake tasks via a Railtie.
53
-
54
- load "pg_search/tasks.rb"
55
-
56
- === Ruby 1.8.7 or earlier
57
-
58
- The newest versions of PgSearch no longer support Ruby 1.8.7. However, the 0.6 series still works. It's not actively maintained, but submissions are welcome for backports and bugfixes.
59
-
60
- gem 'pg_search', "~> 0.6.4"
61
-
62
- The 0.6 branch lives at https://github.com/Casecommons/pg_search/tree/0.6-stable
63
-
64
- == USAGE
65
-
66
- To add PgSearch to an Active Record model, simply include the PgSearch module.
67
-
68
- class Shape < ActiveRecord::Base
69
- include PgSearch
70
- end
71
-
72
- === Multi-search vs. search scopes
73
-
74
- pg_search supports two different techniques for searching, multi-search and search scopes.
75
-
76
- The first technique is multi-search, in which records of many different Active Record classes can be mixed together into one global search index across your entire application. Most sites that want to support a generic search page will want to use this feature.
77
-
78
- The other technique is search scopes, which allow you to do more advanced searching against only one Active Record class. This is more useful for building things like autocompleters or filtering a list of items in a faceted search.
79
-
80
- === Multi-search
81
-
82
- ==== Setup
83
-
84
- Before using multi-search, you must generate and run a migration to create the pg_search_documents database table.
85
-
86
- $ rails g pg_search:migration:multisearch
87
- $ rake db:migrate
88
-
89
- ==== multisearchable
90
-
91
- To add a model to the global search index for your application, call multisearchable in its class definition.
92
-
93
- class EpicPoem < ActiveRecord::Base
94
- include PgSearch
95
- multisearchable :against => [:title, :author]
96
- end
97
-
98
- class Flower < ActiveRecord::Base
99
- include PgSearch
100
- multisearchable :against => :color
101
- end
102
-
103
- If this model already has existing records, you will need to reindex this model to get existing records into the pg_search_documents table. See the rebuild task below.
104
-
105
- Whenever a record is created, updated, or destroyed, an Active Record callback will fire, leading to the creation of a corresponding PgSearch::Document record in the pg_search_documents table. The :against option can be one or several methods which will be called on the record to generate its search text.
106
-
107
- You can also pass a Proc or method name to call to determine whether or not a particular record should be included.
108
-
109
- class Convertible < ActiveRecord::Base
110
- include PgSearch
111
- multisearchable :against => [:make, :model],
112
- :if => :available_in_red?
113
- end
114
-
115
- class Jalopy < ActiveRecord::Base
116
- include PgSearch
117
- multisearchable :against => [:make, :model],
118
- :if => lambda { |record| record.model_year > 1970 }
119
- end
120
-
121
- Note that the Proc or method name is called in an after_save hook. This means that you should be careful when using Time or other objects. In the following example, if the record was last saved before the published_at timestamp, it won't get listed in global search at all until it is touched again after the timestamp.
122
-
123
- class AntipatternExample
124
- include PgSearch
125
- multisearchable :against => [:contents],
126
- :if => :published?
127
-
128
- def published?
129
- published_at < Time.now
130
- end
131
- end
132
-
133
- problematic_record = AntipatternExample.create!(
134
- :contents => "Using :if with a timestamp",
135
- :published_at => 10.minutes.from_now
136
- )
137
-
138
- problematic_record.published? # => false
139
- PgSearch.multisearch("timestamp") # => No results
140
-
141
- sleep 20.minutes
142
-
143
- problematic_record.published? # => true
144
- PgSearch.multisearch("timestamp") # => No results
145
-
146
- problematic_record.save!
147
-
148
- problematic_record.published? # => true
149
- PgSearch.multisearch("timestamp") # => Includes problematic_record
150
-
151
- ==== Multi-search associations
152
-
153
- Two associations are built automatically. On the original record, there is a has_one :pg_search_document association pointing to the PgSearch::Document record, and on the PgSearch::Document record there is a belongs_to :searchable polymorphic association pointing back to the original record.
154
-
155
- odyssey = EpicPoem.create!(:title => "Odyssey", :author => "Homer")
156
- search_document = odyssey.pg_search_document #=> PgSearch::Document instance
157
- search_document.searchable #=> #<EpicPoem id: 1, title: "Odyssey", author: "Homer">
158
-
159
- ==== Searching in the global search index
160
-
161
- To fetch the PgSearch::Document entries for all of the records that match a given query, use PgSearch.multisearch.
162
-
163
- odyssey = EpicPoem.create!(:title => "Odyssey", :author => "Homer")
164
- rose = Flower.create!(:color => "Red")
165
- PgSearch.multisearch("Homer") #=> [#<PgSearch::Document searchable: odyssey>]
166
- PgSearch.multisearch("Red") #=> [#<PgSearch::Document searchable: rose>]
167
-
168
- ==== Chaining method calls onto the results
169
-
170
- PgSearch.multisearch returns an ActiveRecord::Relation, just like scopes do, so you can chain scope calls to the end. This works with gems like Kaminari that add scope methods. Just like with regular scopes, the database will only receive SQL requests when necessary.
171
-
172
- PgSearch.multisearch("Bertha").limit(10)
173
- PgSearch.multisearch("Juggler").where(:searchable_type => "Occupation")
174
- PgSearch.multisearch("Alamo").page(3).per_page(30)
175
- PgSearch.multisearch("Diagonal").find_each do |document|
176
- puts document.searchable.updated_at
177
- end
178
-
179
- ==== Configuring multi-search
180
-
181
- PgSearch.multisearch can be configured using the same options as `pg_search_scope` (explained in more detail below). Just set the PgSearch.multisearch_options in an initializer:
182
-
183
- PgSearch.multisearch_options = {
184
- :using => [:tsearch, :trigram],
185
- :ignoring => :accents
186
- }
187
-
188
- ==== Rebuilding search documents for a given class
189
-
190
- If you change the :against option on a class, add multisearchable to a class that already has records in the database, or remove multisearchable from a class in order to remove it from the index, you will find that the pg_search_documents table could become out-of-sync with the actual records in your other tables.
191
-
192
- The index can also become out-of-sync if you ever modify records in a way that does not trigger Active Record callbacks. For instance, the #update_attribute instance method and the .update_all class method both skip callbacks and directly modify the database.
193
-
194
- To remove all of the documents for a given class, you can simply delete all of the PgSearch::Document records.
195
-
196
- PgSearch::Document.delete_all(:searchable_type => "Animal")
197
-
198
- To regenerate the documents for a given class, run:
199
-
200
- PgSearch::Multisearch.rebuild(Product)
201
-
202
- This is also available as a Rake task, for convenience.
203
-
204
- $ rake pg_search:multisearch:rebuild[BlogPost]
205
-
206
- A second optional argument can be passed to specify the PostgreSQL schema search path to use, for multi-tenant databases that have multiple pg_search_documents tables. The following will set the schema search path to "my_schema" before reindexing.
207
-
208
- $ rake pg_search:multisearch:rebuild[BlogPost,my_schema]
209
-
210
- For models that are multisearchable :against methods that directly map to Active Record attributes, an efficient single SQL statement is run to update the pg_search_documents table all at once. However, if you call any dynamic methods in :against, the following strategy will be used:
211
-
212
- PgSearch::Document.delete_all(:searchable_type => "Ingredient")
213
- Ingredient.find_each { |record| record.update_pg_search_document }
214
-
215
- You can also provide a custom implementation for rebuilding the documents by adding a class method called `rebuild_pg_search_documents` to your model.
216
-
217
- class Movie < ActiveRecord::Base
218
- belongs_to :director
219
-
220
- def director_name
221
- director.name
222
- end
223
-
224
- multisearchable against: [:name, :director_name]
225
-
226
- # Naive approach
227
- def self.rebuild_pg_search_documents
228
- find_each { |record| record.update_pg_search_document }
229
- end
230
-
231
- # More sophisticated approach
232
- def self.rebuild_pg_search_documents
233
- connection.execute <<-SQL
234
- INSERT INTO pg_search_documents (searchable_type, searchable_id, content, created_at, updated_at)
235
- SELECT 'Movie' AS searchable_type,
236
- movies.id AS searchable_id,
237
- (movies.name || ' ' || directors.name) AS content,
238
- now() AS created_at,
239
- now() AS updated_at
240
- FROM movies
241
- LEFT JOIN directors
242
- ON directors.id = movies.director_id
243
- SQL
244
- end
245
- end
246
-
247
- ==== Disabling multi-search indexing temporarily
248
-
249
- If you have a large bulk operation to perform, such as importing a lot of records from an external source, you might want to speed things up by turning off indexing temporarily. You could then use one of the techniques above to rebuild the search documents off-line.
250
-
251
- PgSearch.disable_multisearch do
252
- Movie.import_from_xml_file(File.open("movies.xml"))
253
- end
254
-
255
- === pg_search_scope
256
-
257
- You can use pg_search_scope to build a search scope. The first parameter is a scope name, and the second parameter is an options hash. The only required option is :against, which tells pg_search_scope which column or columns to search against.
258
-
259
- ==== Searching against one column
260
-
261
- To search against a column, pass a symbol as the :against option.
262
-
263
- class BlogPost < ActiveRecord::Base
264
- include PgSearch
265
- pg_search_scope :search_by_title, :against => :title
266
- end
267
-
268
- We now have an ActiveRecord scope named search_by_title on our BlogPost model. It takes one parameter, a search query string.
269
-
270
- BlogPost.create!(:title => "Recent Developments in the World of Pastrami")
271
- BlogPost.create!(:title => "Prosciutto and You: A Retrospective")
272
- BlogPost.search_by_title("pastrami") # => [#<BlogPost id: 2, title: "Recent Developments in the World of Pastrami">]
273
-
274
- ==== Searching against multiple columns
275
-
276
- Just pass an Array if you'd like to search more than one column.
277
-
278
- class Person < ActiveRecord::Base
279
- include PgSearch
280
- pg_search_scope :search_by_full_name, :against => [:first_name, :last_name]
281
- end
282
-
283
- Now our search query can match either or both of the columns.
284
-
285
- person_1 = Person.create!(:first_name => "Grant", :last_name => "Hill")
286
- person_2 = Person.create!(:first_name => "Hugh", :last_name => "Grant")
287
-
288
- Person.search_by_full_name("Grant") # => [person_1, person_2]
289
- Person.search_by_full_name("Grant Hill") # => [person_1]
290
-
291
- ==== Dynamic search scopes
292
-
293
- Just like with Active Record named scopes, you can pass in a Proc object that returns a hash of options. For instance, the following scope takes a parameter that dynamically chooses which column to search against.
294
-
295
- Important: The returned hash must include a :query key. Its value does not necessary have to be dynamic. You could choose to hard-code it to a specific value if you wanted.
296
-
297
- class Person < ActiveRecord::Base
298
- include PgSearch
299
- pg_search_scope :search_by_name, lambda do |name_part, query|
300
- raise ArgumentError unless [:first, :last].include?(name_part)
301
- {
302
- :against => name_part,
303
- :query => query
304
- }
305
- end
306
- end
307
-
308
- person_1 = Person.create!(:first_name => "Grant", :last_name => "Hill")
309
- person_2 = Person.create!(:first_name => "Hugh", :last_name => "Grant")
310
-
311
- Person.search_by_name :first, "Grant" # => [person_1]
312
- Person.search_by_name :last, "Grant" # => [person_2]
313
-
314
- ==== Searching through associations
315
-
316
- It is possible to search columns on associated models. Note that if you do this, it will be impossible to speed up searches with database indexes. However, it is supported as a quick way to try out cross-model searching.
317
-
318
- In PostgreSQL 8.3 and earlier, you must install a utility function into your database. To generate and run a migration for this, run:
319
-
320
- $ rails g pg_search:migration:associated_against
321
- $ rake db:migrate
322
-
323
- This migration is safe to run against newer versions of PostgreSQL as well. It will essentially do nothing.
324
-
325
- You can pass a Hash into the :associated_against option to set up searching through associations. The keys are the names of the associations and the value works just like an :against option for the other model. Right now, searching deeper than one association away is not supported. You can work around this by setting up a series of :through associations to point all the way through.
326
-
327
- class Cracker < ActiveRecord::Base
328
- has_many :cheeses
329
- end
330
-
331
- class Cheese < ActiveRecord::Base
332
- end
333
-
334
- class Salami < ActiveRecord::Base
335
- include PgSearch
336
-
337
- belongs_to :cracker
338
- has_many :cheeses, :through => :cracker
339
-
340
- pg_search_scope :tasty_search, :associated_against => {
341
- :cheeses => [:kind, :brand],
342
- :cracker => :kind
343
- }
344
- end
345
-
346
- salami_1 = Salami.create!
347
- salami_2 = Salami.create!
348
- salami_3 = Salami.create!
349
-
350
- limburger = Cheese.create!(:kind => "Limburger")
351
- brie = Cheese.create!(:kind => "Brie")
352
- pepper_jack = Cheese.create!(:kind => "Pepper Jack")
353
-
354
- Cracker.create!(:kind => "Black Pepper", :cheeses => [brie], :salami => salami_1)
355
- Cracker.create!(:kind => "Ritz", :cheeses => [limburger, pepper_jack], :salami => salami_2)
356
- Cracker.create!(:kind => "Graham", :cheeses => [limburger], :salami => salami_3)
357
-
358
- Salami.tasty_search("pepper") # => [salami_1, salami_2]
359
-
360
- === Searching using different search features
361
-
362
- By default, pg_search_scope uses the built-in {PostgreSQL text search}[http://www.postgresql.org/docs/current/static/textsearch-intro.html]. If you pass the :using option to pg_search_scope, you can choose alternative search techniques.
363
-
364
- class Beer < ActiveRecord::Base
365
- include PgSearch
366
- pg_search_scope :search_name, :against => :name, :using => [:tsearch, :trigram, :dmetaphone]
367
- end
368
-
369
- The currently implemented features are
370
-
371
- * :tsearch - {Full text search}[http://www.postgresql.org/docs/current/static/textsearch-intro.html] (built-in with 8.3 and later, available as a contrib package for some earlier versions)
372
- * :trigram - {Trigram search}[http://www.postgresql.org/docs/current/static/pgtrgm.html], which requires the trigram contrib package
373
- * :dmetaphone - {Double Metaphone search}[http://www.postgresql.org/docs/9.0/static/fuzzystrmatch.html#AEN120188], which requires the fuzzystrmatch contrib package
374
-
375
- ==== :tsearch (Full Text Search)
376
-
377
- PostgreSQL's built-in full text search supports weighting, prefix searches, and stemming in multiple languages.
378
-
379
- ===== Weighting
380
- Each searchable column can be given a weight of "A", "B", "C", or "D". Columns with earlier letters are weighted higher than those with later letters. So, in the following example, the title is the most important, followed by the subtitle, and finally the content.
381
-
382
- class NewsArticle < ActiveRecord::Base
383
- include PgSearch
384
- pg_search_scope :search_full_text, :against => {
385
- :title => 'A',
386
- :subtitle => 'B',
387
- :content => 'C'
388
- }
389
- end
390
-
391
- You can also pass the weights in as an array of arrays, or any other structure that responds to #each and yields either a single symbol or a symbol and a weight. If you omit the weight, a default will be used.
392
-
393
- class NewsArticle < ActiveRecord::Base
394
- include PgSearch
395
- pg_search_scope :search_full_text, :against => [
396
- [:title, 'A'],
397
- [:subtitle, 'B'],
398
- [:content, 'C']
399
- ]
400
- end
401
-
402
- class NewsArticle < ActiveRecord::Base
403
- include PgSearch
404
- pg_search_scope :search_full_text, :against => [
405
- [:title, 'A'],
406
- {:subtitle => 'B'},
407
- :content
408
- ]
409
- end
410
-
411
- ===== :prefix (PostgreSQL 8.4 and newer only)
412
-
413
- PostgreSQL's full text search matches on whole words by default. If you want to search for partial words, however, you can set :prefix to true. Since this is a :tsearch-specific option, you should pass it to :tsearch directly, as shown in the following example.
414
-
415
- class Superhero < ActiveRecord::Base
416
- include PgSearch
417
- pg_search_scope :whose_name_starts_with,
418
- :against => :name,
419
- :using => {
420
- :tsearch => {:prefix => true}
421
- }
422
- end
423
-
424
- batman = Superhero.create :name => 'Batman'
425
- batgirl = Superhero.create :name => 'Batgirl'
426
- robin = Superhero.create :name => 'Robin'
427
-
428
- Superhero.whose_name_starts_with("Bat") # => [batman, batgirl]
429
-
430
- ===== :dictionary
431
-
432
- PostgreSQL full text search also support multiple dictionaries for stemming. You can learn more about how dictionaries work by reading the {PostgreSQL documention}[http://www.postgresql.org/docs/current/static/textsearch-dictionaries.html]. If you use one of the language dictionaries, such as "english", then variants of words (e.g. "jumping" and "jumped") will match each other. If you don't want stemming, you should pick the "simple" dictionary which does not do any stemming. If you don't specify a dictionary, the "simple" dictionary will be used.
433
-
434
- class BoringTweet < ActiveRecord::Base
435
- include PgSearch
436
- pg_search_scope :kinda_matching,
437
- :against => :text,
438
- :using => {
439
- :tsearch => {:dictionary => "english"}
440
- }
441
- pg_search_scope :literally_matching,
442
- :against => :text,
443
- :using => {
444
- :tsearch => {:dictionary => "simple"}
445
- }
446
- end
447
-
448
- sleepy = BoringTweet.create! :text => "I snoozed my alarm for fourteen hours today. I bet I can beat that tomorrow! #sleepy"
449
- sleeping = BoringTweet.create! :text => "You know what I like? Sleeping. That's what. #enjoyment"
450
- sleeper = BoringTweet.create! :text => "Have you seen Woody Allen's movie entitled Sleeper? Me neither. #boycott"
451
-
452
- BoringTweet.kinda_matching("sleeping") # => [sleepy, sleeping, sleeper]
453
- BoringTweet.literally_matching("sleeping") # => [sleeping]
454
-
455
- ===== :normalization
456
-
457
- PostgreSQL supports multiple algorithms for ranking results against queries. For instance, you might want to consider overall document size or the distance between multiple search terms in the original text. This option takes an integer, which is passed directly to PostgreSQL. According to the latest {PostgreSQL documentation}[http://www.postgresql.org/docs/current/static/textsearch-controls.html], the supported algorithms are:
458
-
459
- 0 (the default) ignores the document length
460
- 1 divides the rank by 1 + the logarithm of the document length
461
- 2 divides the rank by the document length
462
- 4 divides the rank by the mean harmonic distance between extents
463
- 8 divides the rank by the number of unique words in document
464
- 16 divides the rank by 1 + the logarithm of the number of unique words in document
465
- 32 divides the rank by itself + 1
466
-
467
- This integer is a bitmask, so if you want to combine algorithms, you can add their numbers together. (e.g. to use algorithms 1, 8, and 32, you would pass 1 + 8 + 32 = 41)
468
-
469
- class BigLongDocument < ActiveRecord::Base
470
- include PgSearch
471
- pg_search_scope :regular_search,
472
- :against => :text
473
-
474
- pg_search_scope :short_search,
475
- :against => :text,
476
- :using => {
477
- :tsearch => {:normalization => 2}
478
- }
479
-
480
- long = BigLongDocument.create!(:text => "Four score and twenty years ago")
481
- short = BigLongDocument.create!(:text => "Four score")
482
-
483
- BigLongDocument.regular_search("four score") #=> [long, short]
484
- BigLongDocument.short_search("four score") #=> [short, long]
485
-
486
- ===== :any_word
487
-
488
- Setting this attribute to true will perform a search which will return all models containing any word in the search terms.
489
-
490
- class Number < ActiveRecord::Base
491
- include PgSearch
492
- pg_search_scope :search_any_word,
493
- :against => :text,
494
- :using => {
495
- :tsearch => {:any_word => true}
496
- }
497
-
498
- pg_search_scope :search_all_words,
499
- :against => :text
500
- end
501
-
502
- one = Number.create! :text => 'one'
503
- two = Number.create! :text => 'two'
504
- three = Number.create! :text => 'three'
505
-
506
- Number.search_any_word('one two three') # => [one, two, three]
507
- Number.search_all_words('one two three') # => []
508
-
509
- ==== :dmetaphone (Double Metaphone soundalike search)
510
-
511
- {Double Metaphone}[http://en.wikipedia.org/wiki/Double_Metaphone] is an algorithm for matching words that sound alike even if they are spelled very differently. For example, "Geoff" and "Jeff" sound identical and thus match. Currently, this is not a true double-metaphone, as only the first metaphone is used for searching.
512
-
513
- Double Metaphone support is currently available as part of the {fuzzystrmatch contrib package}[http://www.postgresql.org/docs/current/static/fuzzystrmatch.html] that must be installed before this feature can be used. In addition to the contrib package, you must install a utility function into your database. To generate and run a migration for this, run:
514
-
515
- $ rails g pg_search:migration:dmetaphone
516
- $ rake db:migrate
517
-
518
- The following example shows how to use :dmetaphone.
519
-
520
- class Word < ActiveRecord::Base
521
- include PgSearch
522
- pg_search_scope :that_sounds_like,
523
- :against => :spelling,
524
- :using => :dmetaphone
525
- end
526
-
527
- four = Word.create! :spelling => 'four'
528
- far = Word.create! :spelling => 'far'
529
- fur = Word.create! :spelling => 'fur'
530
- five = Word.create! :spelling => 'five'
531
-
532
- Word.that_sounds_like("fir") # => [four, far, fur]
533
-
534
- ==== :trigram (Trigram search)
535
-
536
- Trigram search works by counting how many three-letter substrings (or "trigrams") match between the query and the text. For example, the string "Lorem ipsum" can be split into the following trigrams:
537
-
538
- [" Lo", "Lor", "ore", "rem", "em ", "m i", " ip", "ips", "psu", "sum", "um ", "m "]
539
-
540
- Trigram search has some ability to work even with typos and misspellings in the query or text.
541
-
542
- Trigram support is currently available as part of the {pg_trgm contrib package}[http://www.postgresql.org/docs/current/static/pgtrgm.html] that must be installed before this feature can be used.
543
-
544
-
545
- class Website < ActiveRecord::Base
546
- include PgSearch
547
- pg_search_scope :kinda_spelled_like,
548
- :against => :name,
549
- :using => :trigram
550
- end
551
-
552
- yahooo = Website.create! :name => "Yahooo!"
553
- yohoo = Website.create! :name => "Yohoo!"
554
- gogle = Website.create! :name => "Gogle"
555
- facebook = Website.create! :name => "Facebook"
556
-
557
- Website.kinda_spelled_like("Yahoo!") # => [yahooo, yohoo]
558
-
559
- === Ignoring accent marks (PostgreSQL 9.0 and newer only)
560
-
561
- Most of the time you will want to ignore accent marks when searching. This makes it possible to find words like "piñata" when searching with the query "pinata". If you set a pg_search_scope to ignore accents, it will ignore accents in both the searchable text and the query terms.
562
-
563
- Ignoring accents uses the {unaccent contrib package}[http://www.postgresql.org/docs/current/static/unaccent.html] that must be installed before this feature can be used.
564
-
565
-
566
- class SpanishQuestion < ActiveRecord::Base
567
- include PgSearch
568
- pg_search_scope :gringo_search,
569
- :against => :word,
570
- :ignoring => :accents
571
- end
572
-
573
- what = SpanishQuestion.create(:word => "Qué")
574
- how_many = SpanishQuestion.create(:word => "Cuánto")
575
- how = SpanishQuestion.create(:word => "Cómo")
576
-
577
- SpanishQuestion.gringo_search("Que") # => [what]
578
- SpanishQuestion.gringo_search("Cüåñtô") # => [how_many]
579
-
580
- Advanced users may wish to add indexes for the expressions that pg_search generates. Unfortunately, the unaccent function supplied by this contrib package is not indexable (as of PostgreSQL 9.1). Thus, you may want to write your own wrapper function and use it instead. This can be configured by calling the following code, perhaps in an initializer.
581
-
582
- PgSearch.unaccent_function = "my_unaccent"
583
-
584
- === Using tsvector columns
585
-
586
- 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.
587
-
588
- To use this functionality you'll need to do a few things:
589
-
590
- * 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.
591
- * 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
592
- * Should you have any pre-existing data in the table, update the newly-created tsvector columns with the expression that your trigger function uses.
593
- * Add the option to pg_search_scope, e.g:
594
-
595
- pg_search_scope :fast_content_search,
596
- :against => :content,
597
- :using => {
598
- dmetaphone: {
599
- tsvector_column: 'tsvector_content_dmetaphone'
600
- },
601
- tsearch: {
602
- dictionary: 'english',
603
- tsvector_column: 'tsvector_content_tsearch'
604
- }
605
- trigram: {} # trigram does not use tsvectors
606
- }
607
-
608
- Please note that the :against column is only used when the tsvector_column is not present for the search type.
609
-
610
- === Configuring ranking and ordering
611
-
612
- ==== :ranked_by (Choosing a ranking algorithm)
613
-
614
- By default, pg_search ranks results based on the :tsearch similarity between the searchable text and the query. To use a different ranking algorithm, you can pass a :ranked_by option to pg_search_scope.
615
-
616
- pg_search_scope :search_by_tsearch_but_rank_by_trigram,
617
- :against => :title,
618
- :using => [:tsearch],
619
- :ranked_by => ":trigram"
620
-
621
- Note that :ranked_by using a String to represent the ranking expression. This allows for more complex possibilities. Strings like ":tsearch", ":trigram", and ":dmetaphone" are automatically expanded into the appropriate SQL expressions.
622
-
623
- # Weighted ranking to balance multiple approaches
624
- :ranked_by => ":dmetaphone + (0.25 * :trigram)"
625
-
626
- # A more complex example, where books.num_pages is an integer column in the table itself
627
- :ranked_by => "(books.num_pages * :trigram) + (:tsearch / 2.0)"
628
-
629
- ==== :order_within_rank (Breaking ties)
630
-
631
- PostgreSQL does not guarantee a consistent order when multiple records have the same value in the ORDER BY clause. This can cause trouble with pagination.
632
-
633
- Imagine a case where 12 records all have the same ranking value. If you use a pagination library such as {kaminari}[https://github.com/amatsuda/kaminari] or {will_paginate}[https://github.com/mislav/will_paginate] to return results in pages of 10, then you would expect to see 10 of the records on page 1, and the remaining 2 records at the top of the next page, ahead of lower-ranked results.
634
-
635
- But since there is no consistent ordering, PostgreSQL might choose to rearrange the order of those 12 records between different SQL statements. You might end up getting some of the same records from page 1 on page 2 as well, and likewise there may be records that don't show up at all.
636
-
637
- pg_search fixes this problem by adding a second expression to the ORDER BY clause, after the :ranked_by expression explained above. By default, the tiebreaker order is ascending by id.
638
-
639
- ORDER BY [complicated :ranked_by expression...], id ASC
640
-
641
- This might not be desirable for your application, especially if you do not want old records to outrank new records. By passing an :order_within_rank, you can specify an alternate tiebreaker expression. A common example would be descending by updated_at, to rank the most recently updated records first.
642
-
643
- pg_search_scope :search_and_break_ties_by_latest_update,
644
- :against => [:title, :content],
645
- :order_within_rank => "blog_posts.updated_at DESC"
646
-
647
- ==== PgSearch#pg_search_rank (Reading a record's rank as a Float)
648
-
649
- It may be useful or interesting to see the rank of a particular record. This can be helpful for debugging why one record outranks another. You could also use it to show some sort of relevancy value to end users of an application. Just call .pg_search_rank on a record returned by a pg_search_scope.
650
-
651
- shirt_brands = ShirtBrand.search_by_name("Penguin")
652
- shirt_brands[0].pg_search_rank #=> 0.0759909
653
- shirt_brands[1].pg_search_rank #=> 0.0607927
654
-
655
- == ATTRIBUTIONS
656
-
657
- PgSearch would not have been possible without inspiration from
658
- {texticle}[https://github.com/tenderlove/texticle]. Thanks to
659
- {Aaron Patterson}[http://tenderlovemaking.com/]!
660
-
661
- == CONTRIBUTIONS AND FEEDBACK
662
-
663
- Welcomed! Feel free to join and contribute to our {public Pivotal Tracker project}[https://www.pivotaltracker.com/projects/228645] where we manage new feature ideas and bugs.
664
-
665
- We also have a {Google Group}[http://groups.google.com/group/casecommons-dev] for discussing pg_search and other Case Commons open source projects.
666
-
667
- == LICENSE
668
-
669
- MIT