Commit b2b95d0243b018e5df2e4a3b6ff02e5655518aac
Committed by
 Rodrigo Souto
 Rodrigo Souto
1 parent
560a321b
Exists in
master
and in
29 other branches
pg_search plugin : fix search with space between words
Showing
2 changed files
with
11 additions
and
1 deletions
 
Show diff stats
plugins/pg_search/lib/ext/active_record.rb
| @@ -2,8 +2,11 @@ require_dependency 'active_record' | @@ -2,8 +2,11 @@ require_dependency 'active_record' | ||
| 2 | 2 | ||
| 3 | class ActiveRecord::Base | 3 | class ActiveRecord::Base | 
| 4 | def self.pg_search_plugin_search(query) | 4 | def self.pg_search_plugin_search(query) | 
| 5 | + query.gsub!(/\|/,' ') | ||
| 6 | + formatted_query = query.split.map{|w| w += ":*"}.join('|') | ||
| 7 | + | ||
| 5 | if defined?(self::SEARCHABLE_FIELDS) | 8 | if defined?(self::SEARCHABLE_FIELDS) | 
| 6 | - where("to_tsvector('simple', #{pg_search_plugin_fields}) @@ to_tsquery('#{query}:*')") | 9 | + where("to_tsvector('simple', #{pg_search_plugin_fields}) @@ to_tsquery('#{formatted_query}')") | 
| 7 | else | 10 | else | 
| 8 | raise "No searchable fields defined for #{self.name}" | 11 | raise "No searchable fields defined for #{self.name}" | 
| 9 | end | 12 | end | 
plugins/pg_search/test/unit/pg_search_plugin_test.rb
| @@ -14,6 +14,13 @@ class PgSearchPluginTest < ActiveSupport::TestCase | @@ -14,6 +14,13 @@ class PgSearchPluginTest < ActiveSupport::TestCase | ||
| 14 | assert_includes search(Profile, 'water'), profile | 14 | assert_includes search(Profile, 'water'), profile | 
| 15 | end | 15 | end | 
| 16 | 16 | ||
| 17 | + should 'locate one or more profiles' do | ||
| 18 | + profile1 = fast_create(Profile, :identifier => 'administrator') | ||
| 19 | + profile2 = fast_create(Profile, :identifier => 'debugger') | ||
| 20 | + assert_includes search(Profile, 'admin deb'), profile1 | ||
| 21 | + assert_includes search(Profile, 'admin deb'), profile2 | ||
| 22 | + end | ||
| 23 | + | ||
| 17 | # TODO This feature is available only on Postgresql 9.0 | 24 | # TODO This feature is available only on Postgresql 9.0 | 
| 18 | # http://www.postgresql.org/docs/9.0/static/unaccent.html | 25 | # http://www.postgresql.org/docs/9.0/static/unaccent.html | 
| 19 | # should 'ignore accents' do | 26 | # should 'ignore accents' do |